Monday, August 05, 2013

iOS handle Remote Push Notification

There are three cases to handle remote push notification.
  1. Your app was just launched
    handle callback didFinishLaunchingWithOptions application launched by clicking "View" when app is not running
  2. Your app was just brought from background to foreground
    by clicking "View" when app is running in background.
    handle callback didReceiveRemoteNotification
  3. Your app was already running in the foreground
    handle callback didReceiveRemoteNotification
Both (2) and (3) are handled by didReceiveRemoteNotification, use the following code to distinguish forground and background.



- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive )
        // app was already in the foreground
    else
        // app was just brought from background to foreground
    ...
}

Note: if the app is not foreground, user click "Cancel" when receive notification, there will be NO callback. Only the badge will update.
So the client side should be able to pull data from server. Don't rely on notification sending data to client.

-------- situee.blogspot.com ----------------

Reference:
[1] http://stackoverflow.com/questions/5056689/didreceiveremotenotification-when-in-background
[2] http://stackoverflow.com/questions/14616261/receiving-push-notifications-while-in-background

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete