ios - iPhone中的XMPP后台连接

标签 ios objective-c apple-push-notifications xmpp xmppframework

我在 XMPP 聊天应用程序中使用静默推送通知。当应用程序未运行或未处于后台模式时,我想使用它连接到 XMPP 服务器。我正在接收静默推送通知,并且我为后台连接启用了 xmppStream 的以下标志。

xmppStream.enableBackgroundingOnSocket = YES;

但是当应用程序未运行并且无法连接到 XMPP 服务器时,仍不会调用 socketDidConnect 方法。

这样做的目的是将消息接收状态发送到 XMPP 服务器。所以请指导我。

我不想为此使用 VoIP。因此,请为此提供替代解决方案。

最佳答案

请按照以下步骤操作。这可以使用 VoIP 和 PushKit 框架实现。

  • 为您的应用启用 VoIP 服务证书。检查下图。

  • enter image description here
  • 为您的应用启用 IP 语音。检查下图。

  • enter image description here
  • 现在添加 PushKit.framework
  • 在 AppDelegate.h 文件中导入 PushKit 框架并添加其委托(delegate) PKPushRegistryDelegate
  • AppDelegate.m 文件执行以下操作。

    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/

    相关文章:

    iphone - 如何滚动标签上的文本(如选框)

    ios - 找出用户在 iOS 显示的 UIAlertView 上选择了哪个按钮的方法,请求允许应用程序接收推送通知的权限

    iphone - 在 SpriteKit 游戏中计算速度

    objective-c - 在 Objective-C 中异常/返回 NO/nil 的最佳实践是什么?

    ios - HUD 加载程序在 UITableViewController 中无法正确显示

    ios - 根据它使用的 UITableViewCell 类型更改 heightForRowAtIndexPath?

    ios - GCM iOS Production 未收到通知

    ios - APNs/火力地堡 : when is it needed?

    iphone - 关于以编程方式创建 UITextField 的问题

    ios - TableView 分割动画转场