With interactive notifications in an application, you can add up to three actions in one iOS notification, perform an action based on the notification itself without needing to surface to the foreground, and interact with the notification and not the interface of your mobile application. This feature is supported in iOS 8 and above.
Overview
If you send a notification and add two actions that enable users to vote on a specific subject, your app could send the response of the users' decision to your database without having to actually load the user interface and then perform the server update. This creates simple, responsive updates and allows you to collect information from your users.
How it works
Implement interactive notifications within your app by grouping actions into an action category. You create the actions you want to show to your users and add them to a specific category.
There are two types of action categories, static and dynamic. In static, categories actions are created in your app and cannot be changed. This article will describe the use of static categories within your application.
To create a static category, you need to first specify the actions and labels for your notifications. Example code:
id acceptAction = [[MutableUserNotificationAction alloc] init];
[acceptAction setIdentifier: @'Accept'];
[acceptAction setTitle: @'Accept'];
[acceptAction setActivationMode: UIUserNotificationActivationModeForeground];
[acceptAction setDestructive: false];
[acceptAction setAuthenticationRequired: false];
id rejectAction = [[MutableUserNotificationAction alloc] init];
[rejectAction setIdentifier: @'Reject'];
[rejectAction setTitle: @'Reject'];
[rejectAction setActivationMode: UIUserNotificationActivationModeForeground];
[rejectAction setDestructive: false];
[rejectAction setAuthenticationRequired: false];
Class MutableUserNotificationCategory = NSClassFromString(@'UIMutableUserNotificationCategory');
id category = [[MutableUserNotificationCategory alloc] init];
[category setIdentifier: @'example'];
[category setActions:@[acceptAction, rejectAction] forContext: UIUserNotificationActionContextDefault];
[category setActions:@[acceptAction, rejectAction] forContext: UIUserNotificationActionContextMinimal];
appCategories = [NSSet setWithArray: @[ category ]];
Next, you need to inform the OS to use the created categories:
UIUserNotificationSettings *settings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:[appCategories]];
To handle the user interaction of these notifications, add your implementation to the following method:
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void
(^)())completionHandler
{
if(userInfo[@"aps"] && [userInfo[@"aps"][@"category"] isEqual: @"example"])
{ // perform action based on user interacting with the example category
Your app now has all it needs to show the notification to the user as well as perform an action based on the users' selection. Now, send a notification to the user using this category you have just created. The sample app provided within the SDK has this static category already implemented for you to test.
Add the category to a push notification from the Acoustic Campaign instance
- Navigate to Development > Actions. At the end of this article, you will see the input box for where categories are created.
- Click the New category button to add the template to the page. The default category template will be created, you need to modify the 'category' key to match to what we have created. For our example the category would need to look like in the sample below. If you needed to add extra variables with this push, you could use the custom properties sections as shown in the default category template.
{ 'label': 'Example Category implemented in the sample app', 'category': 'example', 'description': 'Contains the Accept, Reject' }
- Now when you are adding actions to your iOS app message within the send experience, you can select the category you want to use. Select the Add category button. You will now see the available categories created within the Acoustic Campaign instance. In this case, you have the example and invitation categories.
- Select the example category.
- (Optional) Add a simple action which will be used if a device does not support these types of notifications. This message will then be sent to your users when the app receives this message it will display the notifications as created in the static category.
- You can also use this category in messages which are sent to the API. To use this category within an API call you can use the following payload:
'aps' : {
'category' : 'example',
'alert' : 'Message',
'badge' : 3,
'sound' : "default'
}