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
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
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.