终止应用程序时XMPP没有正确断开连接?

时间:2016-08-01 12:46:47

标签: ios iphone xmpp

XMPP在终止应用程序时没有正确断开连接?

B'我需要启用APNS一旦应用程序被用户终止,就像whatsapp工作一样。

如果我在连接XMPP服务器的2分钟内关闭应用程序,则APNS快速启用。但如果我在5-6分钟后关闭应用程序比APNS没有快速启用。可能需要1-2分钟或更长时间。在5-6分钟的情况下,如果i锁定设备比APNS快速启用。

我使用下面的代码进行推送配置并断开XMPP。

-(void)configurePushNotifications
{
    NSString *jabberID = [NSString stringWithFormat:@"test123"];

    NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"id" stringValue:strDeviceToken];

    NSXMLElement *push = [NSXMLElement elementWithName:@"push" xmlns:@"p1:push"];

    NSXMLElement *ping = [NSXMLElement elementWithName:@"ping" xmlns:@"urn:xmpp:ping"];

    NSXMLElement *keepalive = [NSXMLElement elementWithName:@"keepalive"];
    [keepalive addAttributeWithName:@"max" integerValue:60];

    NSXMLElement *session = [NSXMLElement elementWithName:@"session"];
    [session addAttributeWithName:@"duration" integerValue:60];

    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body addAttributeWithName:@"send" stringValue:@"all"];
    [body addAttributeWithName:@"groupchat" stringValue:@"true"];
    [body addAttributeWithName:@"from" stringValue:@"username"];

    NSXMLElement *status = [NSXMLElement elementWithName:@"status"];
    [status addAttributeWithName:@"type" stringValue:@"xa"];
    [status setStringValue:[NSString stringWithFormat:@"New message from %@",jabberID]];

    NSXMLElement *offline = [NSXMLElement elementWithName:@"offline" stringValue:@"true"];

    [push addChild:keepalive];
    [push addChild:session];
    [push addChild:body];
    [push addChild:status];
    [push addChild:offline];

    NSXMLElement *notification = [NSXMLElement elementWithName:@"notification"];
    [notification addChild:[NSXMLElement elementWithName:@"type" stringValue:@"applepush"]];
    [notification addChild:[NSXMLElement elementWithName:@"id" stringValue:strDeviceToken]];

    [push addChild:notification];

    NSXMLElement *appid = [NSXMLElement elementWithName:@"appid" stringValue:@"com.test.test"];

    [push addChild:appid];

    [iq addChild:ping];
    [iq addChild:push];
    [[self xmppStream] sendElement:iq];
}

- (void)goOffline
{
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];

    [[self xmppStream] sendElement:presence];
}

- (void)goOnline
{
    XMPPPresence *presence = [XMPPPresence presence]; // type="available" is implicit

    NSString *domain = [xmppStream.myJID domain];

    //Google set their presence priority to 24, so we do the same to be compatible.

    if([domain isEqualToString:@"gmail.com"]
       || [domain isEqualToString:@"gtalk.com"]
       || [domain isEqualToString:@"talk.google.com"])
    {
        NSXMLElement *priority = [NSXMLElement elementWithName:@"priority" stringValue:@"24"];
        [presence addChild:priority];
    }

    [[self xmppStream] sendElement:presence];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    [self goOffline];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self goOnline];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

任何人都可以帮助我吗?

0 个答案:

没有答案