简体   繁体   中英

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.

I made some workaround to solve this problem.

First, I Used background modes -

a) App downloads content from the network. b) App downloads content in response to push notifications.

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

  • App Launched
  • User logged in
  • App went in BG
  • Server has been notified that app is in BG.
  • Server starts timer.
  • Elapsed time exceeds 5 mins on server for this device (As app still in BG) .
  • Server sends notification.
  • App receives remote push notification in 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)

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.

Works perfectly fine as expected.

CASE 2 : App in BackGround and DEVICE LOCKED

  • App Launched
    • User logged in
    • App went in BG
    • Server has been notified that app is in BG.
    • Server starts timer.
    • Device is lOCKED
    • Elapsed time exceeds 5 mins on server for this device (As app still in 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.
    • 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 {

NSLog(@"user info %@",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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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