Interactive notifications
Interactive notifications allow the users to take actions when a notification is arrived without opening the application. When an interactive notification arrived, the device shows the action buttons along with the notification message. Currently, the interactive notifications are supported on iOS devices with version 8 onwards. If an interactive notification is sent to iOS devices with version lesser than 8, the notification actions are not displayed.
Sending interactive push notification
Prepare the notification and send notification. See Sending push notifications.
We can set a string to indicate the category of notification with the notification object. Based on the category value, the notification action buttons are displayed.
To set the category in event source notifications, there are two options:
- Create notification JSON object and set category in that object:
var notification = { badge:1, category: 'poll', ....};
- Create notification object using the WL.Server.createDefaultNotification API and set category on the notification object:
notification.APNS.category= 'poll';
See WL.Server.createDefaultNotification and WL.Server.notifyAllDevices APIs in WL.Server class.
In Broadcast, Tag-based and Uni-cast notifications set the type while you create the notification object:
notification.settings.apns.category = 'poll';
See WL.Server.sendMessage API in WL.Server class.
Handling interactive push notifications in hybrid iOS application
We must follow these steps to receive interactive notifications:
- In the main JavaScript, define the following method to return the registered categories for the interactive notifications.
WL.Client.Push.getInteractivePushCategories = function(){ var categories = [{ //Category identifier, this is used while sending the notification. id : "poll", //Optional array of actions to show the action buttons along with the message. actions: [ { //Action identifier id : "poll_ok", //Action title to be displayed as part of the notification button. title : "OK", //Optional mode to run the action in foreground or background. 1-foreground. 2-background. Default is foreground. mode: 1, //Optional property to mark the action button in red color. Default is false. destructive: false, //Optional property to set if authentication is required or not before running the action.(Screen lock). //For foreground, this property is always true. requireAuthentication: true }, { id : "poll_nok", title : "NOK", mode: 1, destructive: false, requireAuthentication: true ], //Optional list of actions that is needed to show in the case alert. //If it is not specified, then the first four actions will be shown. defaultContextActions: ['poll_ok','poll_nok'], //Optional list of actions that is needed to show in the notification center, lock screen. //If it is not specified, then the first two actions will be shown. minimalContextActions: ['poll_ok','poll_nok'] }]; return categories; };See WL.Client.Push.getInteractivePushCategories API in WL.Client.Push class.
- The notification callback method contains two extra properties we can use them to take actions.
- category: The name of the category set while sending the notification.
- action-id: If the user clicks the action button, then this represents the ID of the action.
Handling interactive push notifications in native iOS application
We must follow these steps to receive interactive notifications:
- Enable the application capability to perform background tasks on receiving the remote notifications. This step is required if some of the actions are background-enabled.
- In the AppDelegate (application: didRegisterForRemoteNotificationsWithDeviceTokenapplication:), set the categories before you set the deviceToken on WLPush Object.
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ UIUserNotificationType userNotificationTypes = UIUserNotificationTypeNone | UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge; UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; acceptAction.identifier = @"OK"; acceptAction.title = @"OK"; UIMutableUserNotificationAction *rejetAction = [[UIMutableUserNotificationAction alloc] init]; rejetAction.identifier = @"NOK"; rejetAction.title = @"NOK"; UIMutableUserNotificationCategory *cateogory = [[UIMutableUserNotificationCategory alloc] init]; cateogory.identifier = @"poll"; [cateogory setActions:@[acceptAction,rejetAction] forContext:UIUserNotificationActionContextDefault]; [cateogory setActions:@[acceptAction,rejetAction] forContext:UIUserNotificationActionContextMinimal]; NSSet *catgories = [NSSet setWithObject:cateogory]; [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:catgories]]; }
- Implement new callback method on AppDelegate:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (ˆ)())completionHandler
- This new callback method is invoked when user clicks the action button.
- The implementation of this method must perform the action that is associated with the specified identifier and execute the block in the completionHandler parameter.
Parent topic: Push notification