Wednesday, October 29, 2014

iOS 8.0 Pushing Local Notification or Remote Notificaiton with Custom Action

To show the notication action an app defined, categorised and registered, the app needs to receive a remote notification or schedule a local notification. In the remote notification case, we need to include the category identifier in the payload. 

  {
    "aps" :  {
        "alert" : "You’re invited!",
        "category" : "INVITE_CATEGORY",
    }
}

For local notifications, the code is something like below 

UILocalNotification *notification = [[UILocalNotification alloc] init]; . . .
notification.category = @"INVITE_CATEGORY";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

Handling Notification actions 
If the user doesn’t tap on specific action (actions being available in iOS 8.0), then the system calls the didReceiveLocalNotification or didReceiveRemoteNotiicaion as usual. This is the case when the application is in foreground. IF the app was in background and user doesn the above actions, then didFinishLaunchingWithOptions will be called passing in the notificaiotn details in the launch options dictionary. 

To handle the actions from a notification that is available in iOS 8.0, application needs to implement any of the below depending on the type of notification applicaiotn needs. 
application:handleActionWithIdentifier:(NSString*)identifier 

Code is something like below 

- (void)application:(UIApplication *) application
handleActionWithIdentifier: (NSString *) identifier
// either forLocalNotification: (NSDictionary *) notification or
forRemoteNotification: (NSDictionary *) notification
  completionHandler: (void (^)()) completionHandler {
    
    if ([identifier isEqualToString: @"ACCEPT_IDENTIFIER"]) {
        [self handleAcceptActionWithNotification:notification];
    }
    
    // Must be called when finished
    completionHandler();

}

references: 
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html

No comments:

Post a Comment