我在 XMPP 聊天应用程序中使用静默推送通知。当应用程序未运行或未处于后台模式时,我想使用它连接到 XMPP 服务器。我正在接收静默推送通知,并且我为后台连接启用了 xmppStream 的以下标志。
xmppStream.enableBackgroundingOnSocket = YES;
但是当应用程序未运行并且无法连接到 XMPP 服务器时,仍不会调用 socketDidConnect 方法。
这样做的目的是将消息接收状态发送到 XMPP 服务器。所以请指导我。
我不想为此使用 VoIP。因此,请为此提供替代解决方案。
最佳答案
请按照以下步骤操作。这可以使用 VoIP 和 PushKit 框架实现。
A) 在 didFinishLaunchingWithOptions 方法中添加以下代码。
PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
B) 添加PKPushRegistryDelegate的所有委托(delegate)方法
- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type { NSLog(@"VoIP - did Invalidate Push Token for type - %@", type); }
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
NSLog(@"VoIP - got Push with payload: %@ and Type: %@", payload.dictionaryPayload, type);
NSDictionary *dictAps = [payload.dictionaryPayload valueForKey:@"aps"];
if ([dictAps valueForKey:@"content-available"] != nil) {
NSLog(@"Silent VoIP");
//Fetch payload info and create local notification and fire that local notification.
UILocalNotification *voipNotification = [[UILocalNotification alloc] init];
voipNotification.alertTitle = @"Silent VoIP";
voipNotification.alertBody = [dictAps valueForKey:@"alert"];
voipNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:voipNotification];
//Call xmpp connection method here.
if (xmppStream == nil) { [self setupStream]; }
if ([xmppStream isDisconnected]) { [self connect]; }
}
}
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
NSLog(@"VoIP - device Token: %@", credentials.token);
NSString *newToken = credentials.token.description;
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"VoIP token is: %@", newToken);
[obj_DataModel setVoIPToken:newToken]; //Store token in somewhere for further use.
}
{"aps":{"alert":"Testing...","badge":1,"sound":"default","content-available":1}}
我希望这对你有帮助。如果您有任何疑问,请问我。
关于ios - iPhone中的XMPP后台连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45813799/