简体   繁体   English

当应用程序在后台且设备被锁定时处理推送通知

[英]Handling push notification when app is in background and device is locked

I am working on developing an Enterprise application where maintaining users session synced with device session is a key and required feature. 我正在开发一个企业应用程序,其中维护用户会话与设备会话同步是一项关键且必需的功能。

The basic requirement was to end user session from server as soon as user kills the app, but as there is know way we can get app termination event in code (except starting a background task which can run maximum for 10 mins), server remains unaware about app kill. 基本要求是,一旦用户杀死应用程序,就要从服务器终止最终用户会话,但是由于我们知道可以通过代码获取应用程序终止事件(启动后台任务最多可以运行10分钟),因此服务器仍然没有意识到关于应用杀灭。

I made some workaround to solve this problem. 我提出了一些解决方法来解决此问题。

First, I Used background modes - 首先,我使用了背景模式-

a) App downloads content from the network. a)应用程序从网络下载内容。 b) App downloads content in response to push notifications. b)应用程序下载内容以响应推送通知。

The idea is when user send app in background by switching to another app or by home button or locks device, app is sending a request to the server that app is going in background, and server get to know that app went in background. 这个想法是,当用户通过切换到另一个应用程序或通过主页按钮或锁定设备在后台发送应用程序时,该应用程序正在向服务器发送一个请求,说明该应用程序正在后台运行,而服务器知道该应用程序已在后台运行。

 -(void)applicationWillResignActive:(UIApplication *)application
 {
    // notify server that app is in background
 }

As soon as server receives the request, it starts the timer for this specific device, which keep on counting apps time spent in background. 服务器一接收到请求,就会为该特定设备启动计时器,该计时器将继续计算应用程序在后台花费的时间。 For every five minutes server sends a request to the device which is in background to get its STATUS. 服务器每隔五分钟发送一次请求到处于后台的设备,以获取其状态。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

    NSDictionary *aps = [userInfo objectForKey:@"aps"];
    NSLog(@"NOTIFICATION IN BG %@",[aps objectForKey:@"message"]);

    NSString *message = [aps objectForKey:@"message"];

    if([message isEqualToString:@"Please update your status"]){
        // NOTIFY SERVER THAT APP IS IN BACKGROUND
    }

    if(application.applicationState != UIApplicationStateBackground){
        [self application:application didReceiveRemoteNotification:userInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

As soon as app again comes in foreground or become active app notifies server that it has become active. 一旦应用再次出现在前台或变为活动状态,应用程序就会通知服务器它已变为活动状态。

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // notify server that app is in Foreground // server resets the background timer to 0 for corresponding device
}

This approach helps in tracking if app is terminated or not, if server does not get any response from the notification that it sent, it clears the user session from server. 此方法有助于跟踪应用程序是否终止,如果服务器未从其发送的通知中获得任何响应,它将清除服务器中的用户会话。

NOW, There are two scenarios in 1st one it works perfectly as expected and in second it doesent. 现在,有两种情况在第一种情况下,它可以按预期完美地工作,而在第二种情况下,它确实可以实现。

CASE 1 : App in BackGround but DEVICE NOT LOCKED 案例1 :BackGround中的应用但设备未锁定

  • App Launched 应用启动
  • User logged in 用户登录
  • App went in BG 应用进入BG
  • Server has been notified that app is in BG. 服务器已收到通知,告知该应用程序在BG中。
  • Server starts timer. 服务器启动计时器。
  • Elapsed time exceeds 5 mins on server for this device (As app still in BG) . 该设备在服务器上经过的时间超过5分钟(因为应用仍在BG中)。
  • Server sends notification. 服务器发送通知。
  • App receives remote push notification in BG. 应用程序在BG中接收远程推送通知。
  • App notifies its status to server that is in BG (Hence server came to know that it is not terminated and user session should not be cleared off) 应用程序将其状态通知给BG中的服务器(因此服务器知道它没有终止并且不应清除用户会话)

This cycle of remote notification and reply to server from device from BackGround goes on till the time either app becomes active again or it is terminated. 从BackGround从设备进行的远程通知和从服务器回复服务器的周期一直持续到应用再次激活或终止。

Works perfectly fine as expected. 如预期般完美运作。

CASE 2 : App in BackGround and DEVICE LOCKED 案例2 :BackGround中的应用程序和设备已锁定

  • App Launched 应用启动
    • User logged in 用户登录
    • App went in BG 应用进入BG
    • Server has been notified that app is in BG. 服务器已收到通知,告知该应用程序在BG中。
    • Server starts timer. 服务器启动计时器。
    • Device is lOCKED 设备已被选中
    • Elapsed time exceeds 5 mins on server for this device (As app still in BG) . 该设备在服务器上经过的时间超过5分钟(因为应用仍在BG中)。
    • Server sends notification. 服务器发送通知。
    • Device receives Notification, Device displays banner for notification. 设备收到通知,设备显示通知标题。
    • BUT App which is inactive mode does not receives remote push notification in BG. 非活动模式的BUT App在BG中未收到远程推送通知。
    • As a Result App is not able to reply to the server 结果是应用程序无法回复服务器

And whole cycle goes for a toss. 整个周期都在折腾。

THE ULTIMATE QUESTION IS HOW TO MAKE IT WORK WHEN DEVICE IS LOCKED. 当设备被锁定时,最终问题是如何进行的。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { -(void)应用程序:(UIApplication *)应用程序didReceiveRemoteNotification:(NSDictionary *)userInfo {

NSLog(@"user info %@",userInfo ); NSLog(@“用户信息%@”,userInfo); } }

by this delegate method you can receive dictionary from server.And can do your work accordingly.This method will call in your app after click on notification in notification tray. 通过此委托方法,您可以从服务器接收字典。您可以相应地执行工作。单击通知托盘中的通知后,此方法将在您的应用中调用。

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

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