2

We want to implement Online/Offline using Pubnub framework in iOS but app doesn't work in background more than 10 minutes. We have tried to implement this by enable location feature and its working fine. But client does not want to enable location service for this purpose. So how we will manage to make run enable in background infinite ? (QUE 1)

And if we want to put Online/Offline/Away status in our code

Where we should show, Online: When user open the app Offline: When user close the app from background OR delete the app Away: When user tap on home button and app is in background.

How we will implement this (Specially AWAY & Offline state)?(QUE 2)

Is there any other way to manage Online/Offline/Away ? (Without Pubnub ) (QUE 3) If Yes, please describe the way.

How we will manage Online/Offline/Away using web service? When we call that web services? (QUE 4)

We are using almost 3-4 year old base code. So should we add any other parameter in plist to enable app in background for more time ? (QUE 5)

Please guide us.

4

2 回答 2

2

使用PubNub Presence,您拥有在应用程序中提供此功能的最佳方式,但由于 iOS 处理应用程序进入后台的方式,您可能有机会也可能没有机会更新此状态(取决于应用程序的运行配置方式)。

但非常简单,通过为您的 PubNub 密钥启用 Presence,当客户端订阅频道join时,将向所有其他正在收听该频道上的状态的订阅者发送一个事件。

当应用程序进入后台时,如果您有机会这样做,您可以在客户端订阅的所有频道上调用取消leave订阅,并且将向该/那些频道的所有订阅者发送一个事件以监听出席事件。

如果应用程序被杀死或进入您没有机会调用的后台unsubscribe,当应用程序离线/断开连接的时间超过配置的心跳周期时,将向所有人发送一个timeout(相当于一个事件)leave收听出席事件的订阅者。

心跳默认为 5 分钟,但您可以将其配置为较低的设置,如 60 秒(对于需要此设置但从不低于 15 秒的用例,或更短的设置)。

如果您希望您的服务器收听 Presence 事件,那么您将需要使用我们的 Presence Webhooks(即将发布的文档)。请参阅此 StackOverflow 线程,了解如何实现 PubNub Presence Webhooks 以及如何配置您的 PubNub 密钥以使用它们

于 2015-08-31T18:26:46.657 回答
0

有两种情况:

  • 用户将 App 置于后台模式
  • 用户退出应用程序。

您需要做的是在这些事件发生时得到通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(appWillResignActive) 
                                             name:UIApplicationWillResignActiveNotification 
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(appWillTerminate) 
                                             name:UIApplicationWillTerminateNotification 
                                           object:nil];

现在,在相应的方法中,使用 PubNub 的 API 设置状态

-(void)appWillResignActive
{
    // Sample dictionary
    NSDictionary *dicState = @{ @"userName" : senderName, // Optional
                                @"status"   : @"Away", // Or whatever
                                @"isTyping" : @FALSE // Optional
                              };

    [AppDel.client setState: dicState
                    forUUID: senderId // current user's UUID
                  onChannel: KPubNubChannelName // channel name
             withCompletion: ^(PNClientStateUpdateStatus *status) 
     {
         NSLog(@"%@", status);
     }];
}

一旦 PubNub 上的状态更新,该频道的所有订阅者都会收到通知,即会调用以下方法:

- (void)client:(PubNub *)client didReceivePresenceEvent:(PNPresenceEventResult *)event 
{
    if (![event.data.channel isEqualToString:event.data.subscription])
    {
        // Presence event has been received on channel group stored in event.data.subscription.
    }
    else 
    {
        // Presence event has been received on channel stored in event.data.channel.
    }

    if (![event.data.presenceEvent isEqualToString:@"state-change"]) {

        NSLog(@"%@ \"%@'ed\"\nat: %@ on %@ (Occupancy: %@)", event.data.presence.uuid, 
              event.data.presenceEvent, event.data.presence.timetoken, event.data.channel, 
              event.data.presence.occupancy);
    }
    else {

        NSLog(@"%@ changed state at: %@ on %@ to: %@", event.data.presence.uuid, 
              event.data.presence.timetoken, event.data.channel, event.data.presence.state);
    }
} 

当它被调用时,您必须相应地更新您的数据源,这将反映状态更新。

于 2018-01-19T13:31:55.340 回答