Since 2015 April, we have had a payload feature[see here] in the Sensorberg Management Platform. In this post we will highlight the usage of the payload object for actionable notifications on iOS devices and show different usages of the payload object.
We would like to show an actionable notification with custom content to our users based on the payload which we have entered user into the portal platform. Using this article you can see how to set an actionable notification with a custom payload and how to handle it.
Add content in payload of your campaign
“onEnter” is the object which contains the content for one actionable notification.
“confirmationAction” can have one of following 2 options : open, share
“actInForeground” is a boolean value
The following 3 language objects (“de”, “en”, “ko”) are localised strings for notification. Of course you can add more languages with ISO 639-1 language codes.
Ex : custom payload
{
"onEnter": {
"confirmAction": "open",
"actInForeground": true,
"de": {
"okButtonTitle": "Zur Website",
"cancelButtonTitle": "Schließen",
"title": "Hallo!!",
"message": "Wilkommen in Sensorberg!"
},
"en": {
"okButtonTitle": "Open Website",
"cancelButtonTitle": "Close",
"title": "Hi!!",
"message": "Welcome to Sensorberg!"
},
"ko": {
"okButtonTitle": "사이트 열기",
"cancelButtonTitle": "닫기",
"title": "안녕하세요!!",
"message": "Sensorberg에 오신것을 환영합니다!"
}
}
}
Get content of payload from SBCampaignAction
To get SBMCampaignAction, you need to subscribe “SBEventPerformAction” with Tolo.
SUBSCRIBE(SBEventPerformAction){ SBMCampaignAction *action = event.campaign; }
And check for a payload in the SBMCampaignAction object
if (action.payload) { … }
Get the “onEnter” content from the payload
NSDictionary *actionDict = [action.payload @“onEnter”]
Register UserNotificationSettings.
NSDictionary *localizedDict = [actionDict objectForKey:@"en"];
UIMutableUserNotificationAction *okAction = [UIMutableUserNotificationAction new];
okAction.identifier = @"okActionIdentifier";
okAction.title = [localizedDict objectForKey:@"okButtonTitle"];
okAction.authenticationRequired = NO;
okAction.activationMode = [[actionDict objectForKey:@"actInForeground"] boolValue] ?UIUserNotificationActivationModeForeground : UIUserNotificationActivationModeBackground;
UIMutableUserNotificationAction *cancelAction = [UIMutableUserNotificationAction new];
cancelAction.identifier = @"cancelButtonIdentifer";
cancelAction.title = [localizedDict objectForKey:@"cancelButtonTitle"];
cancelAction.authenticationRequired = NO;
// cancel action should not activate app.
cancelAction.activationMode = UIUserNotificationActivationModeBackground;
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:@"myOnEnterActionCategoryIdeintifier"];
[actionCategory setActions:@[okAction, cancelAction] forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
Create a local notification with a “notification category” and then schedule the notification.
UILocalNotification *n = [UILocalNotification new];
n.alertTitle = [localizedDict objectForKey:@"title"];
n.alertBody = [localizedDict objectForKey:@"message"];
n.category = @"myOnEnterActionCategoryIdeintifier";
n.userInfo =@{@"action": @"User Info what you need to handle"};
if (action.fireDate)
{
n.fireDate = action.fireDate;
}
else
{
n.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
}
[[UIApplication sharedApplication] scheduleLocalNotification:n];
Let’s see how it works on iPhone :D