简体   繁体   English

如何在后台保持iphone ios websocket连接的活动状态?

[英]How to keep iphone ios websocket connection alive while in the background?

I created a chat application using SocketRocket,when I quit the app(enter background mode) I want to receive the chat message! 我使用SocketRocket创建了一个聊天应用程序,当我退出该应用程序(进入后台模式)时,我想接收聊天消息! so i could do that by calling it a VoIP app but some people says Apple will reject it from the App Store unless it also truly does VoIP. 因此我可以通过将其称为VoIP应用程序来做到这一点,但有人说,除非苹果也真正使用VoIP,否则苹果将拒绝它在App Store中的销售。 So how can I do that and if the only way to do that is by calling it a VoIP app how does whatsapp handles this situation?? 那么,我该怎么办?如果唯一的方法就是将其称为VoIP应用程序,那么whatsapp如何处理这种情况?

Here's what i did to keep the app alive in the background: 这是我为使应用程序在后台运行而所做的工作:

- (void)applicationDidEnterBackground:(UIApplication *)application
{


 NSLog(@"Application entered background state.");
    NSAssert(self.backgroundTask == UIBackgroundTaskInvalid, nil);
    self.didShowDisconnectionWarning = NO;

self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Background task expired");
        if (self.backgroundTimer)
        {
            [self.backgroundTimer invalidate];
            self.backgroundTimer = nil;
        }
        [application endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    });
}];

dispatch_async(dispatch_get_main_queue(), ^{
    self.backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timerUpdate:) userInfo:nil repeats:YES];
});

}


- (void) timerUpdate:(NSTimer*)timer {
    UIApplication *application = [UIApplication sharedApplication];

    NSLog(@"Timer update, background time left: %f", application.backgroundTimeRemaining);

    if ([application backgroundTimeRemaining] < 60 && !self.didShowDisconnectionWarning)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif) {
            localNotif.alertBody = @"Background session will expire in one minute.";
            localNotif.alertAction = @"OK";
            localNotif.soundName = UILocalNotificationDefaultSoundName;
            [application presentLocalNotificationNow:localNotif];
        }
        self.didShowDisconnectionWarning = YES;
    }
    if ([application backgroundTimeRemaining] < 10)
    {
        // Clean up here
        [self.backgroundTimer invalidate];
        self.backgroundTimer = nil;

        [application endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }

}

I believe your only option is to use remote notifications. 我相信您唯一的选择是使用远程通知。 You need your app to register for them, and your server to detect when your app is not running (ie connected via the socket), and then send a notification instead. 您需要您的应用程序为它们注册,并且您的服务器需要检测您的应用程序何时未运行(即通过套接字连接),然后发送通知。

You can't keep alive your TCP socket in background , How I did achieve this feature whenever applicationDidEnterBackground called disconnect the socket and applicationDidBecomeActive Connect the socket so that server knows client socket is disconnect then save message to Offline Table . 您不能在后台保持TCP套接字的活跃,每当applicationDidEnterBackground调用断开套接字和applicationDidBecomeActive的连接时,我怎么做到了? There was schedular on web server that runs periodically to send push notification. Web服务器上有时间表,该时间表定期运行以发送推送通知。 whenever user receive push notification and user taps on it you need to create a flow to launch the right conversation based on the parameter you gets in Remote notification dictionary. 每当用户收到推送通知并在其上点击时,您都需要根据在远程通知字典中获取的参数创建一个流程以启动正确的对话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM