iOS Notification Buttons¶
A button in an iOS push notification is represented by the UNNotificationAction
class and contains the following properties:
- identifier - uniquely identifies the button among others
- title - text displayed on the button
- options - define button behavior
The Backendless Button Options for iOS closely match these properties:
Available button behavior options are:
Option |
Description |
---|---|
Authentication Required |
The action can be performed only on an unlocked device. If the user taps the button while the device is locked, they will be required to unlock the device by entering their code, using a fingerprint, etc. |
Destructive |
The action button is displayed with special highlighting to indicate that it performs a destructive task. Use this option for actions that delete user data or change the app irrevocably. |
Foreground |
When the user selects an action containing this option, the system brings the app to the foreground, asking the user to unlock the device as needed. Use this option for actions that require the user to interact further with your app. Do not use this option simply to bring your app to the foreground. |
Inline reply |
Allows the user to provide a custom text-based response. When the user selects an action of this type, the system displays controls for the user to enter or dictate the text content. That text is then included in the response object that is delivered to your app. |
Button Handling in Code¶
When a user taps a button in a push notification, the action can be processed in the code using the following method:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler;
optional func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
The response
argument contains the button identifier specified in the Button Options configuration which is associated with the Push Template used to publish the notification. Additionally, the response
argument provides access to the notification object itself. The code below demonstrates how to access the identifier and the individual notification properties such as message body, title, subtitle and the headers. These properties of a push notification come from the push template from which a push notification is created. All listed elements of a push notification (message body, title, subtitle and the headers) can be defined on the "WHAT" section in Push Composer. The sample code below demonstrates the API used to obtain these values:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
// get button identifier
NSLog(@"Button identifier: %@", response.actionIdentifier);
// get message body
NSLog(@"Message body: %@", response.notification.request.content.body);
// get message title
NSLog(@"Message title: %@", response.notification.request.content.title);
// get message subtitle
NSLog(@"Message subtitle: %@", response.notification.request.content.subtitle);
// get custom header value - in this example, the name of the header is "sample_header_name"
NSLog(@"Custom header: %@", [response.notification.request.content.userInfo valueForKey:@"sample_header_name"]);
completionHandler();
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// get button identifier
print("Button identifier \(response.actionIdentifier)")
// get message body
print("Message body: \(response.notification.request.content.body)")
// get message title
print("Message title: \(response.notification.request.content.title)")
// get message subtitle
print("Message subtitle: \(response.notification.request.content.subtitle)")
// get custom header value - in this example, the name of the header is "sample_header_name"
print("Custom header: \(response.notification.request.content.userInfo["response.notification.request.content.userInfo"] ?? "NONE")")
completionHandler()
}