Thursday, March 26, 2015

iOS Silent Push notifications

This is a good feature especially as it helps apps to decide whether there is any thing valid in the message that is coming from the server. 

This needs both the client and server changes. the app specific changes are below 

1. Include the background run requirement as content update available. Below is the mode. “App downloads content in response to push notifications”

This is easy because we can just include this from Capabilities tab of the project. which will add this to the plist file. 

Change the code to include the new call back like below. 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
    NSLog(@"--- Application received remote notification ---");
    NSLog(@"-- User Info Received :%@",userInfo);
    
    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.repeatInterval = NSDayCalendarUnit;
    [notification setAlertBody:@"Hello world"];
    [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    [notification setTimeZone:[NSTimeZone  defaultTimeZone]];
    [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
    
}

2. Modification to the server. 
On the server side, in general the “content-available:1” to be added in the payload. 

So the pay load could be something like this 

{
    "aps": {
        "content-available": "1",
        "sound": ""
    },
    "count": "1",
    "type": "1"
}

Below are few notes from the testings done

- If  app is running in background, when server sends a silent push, app receives it in background and app can do some task in background for a finite amount of time. And it can post a local notification or sit silent if no update to be given to the user 
- If the app was not running in the background or foreground, then OS invokes the app to give this notification payload and app can do brief operations.  
- IF the app was killed by user previously, the notification won’t be delivered to the app.  
- If the app was killed due to memory on device or device restarted for some reason etc, the app will be given the notificaton payload even if user don’t launch the app. 
- If the app was running in the foreground, then notification is directly delivered to the app. 


The page in the reference below, having good amount of nice content explaining the same thing. Nice pictures too on it !! 

References:

No comments:

Post a Comment