Push notifications in native iOS applications
IBM MobileFirst Platform Foundation provides a unified set of API methods to send, or push, notifications to devices where the MobileFirst application is installed in. It is possible to send a notification in 3 distinct types: event source notifications, broadcast notifications and tag notifications.
In this tutorial, the concept, API and usage of push notifications will be explained in the context of Native iOS applications.
To create and configure an iOS native project, first follow these tutorials:
- Creating your first Native iOS MobileFirst application
- Invoking adapter procedures from native iOS applications
The following topics are covered:
Setting up push notifications
- Create a MobileFirst project and add a MobileFirst iOS Native API.
- Add the Apple Push Notification Service (APNS) p12 keys to the root folder of the application (either apns-certificate-sandbox.p12 or apns-certificate-production.p12).
apns-certificate-sandbox.p12 is used in development mode. When you move to production, use apns-certificate-production.p12.
- In application-descriptor.xml, add the
pushSendertag with the
passwordattribute. Use the .p12 keystore as the password value.
For example :
- Deploy the MobileFirst native API. In MobileFirst Studio, right-click the native API and select "Run As > Deploy Native API". With the CLI, use
mfp deployfrom within the application folder.
Event source notifications
Event source notifications are notification messages that are targeted to devices with a user subscription.
Broadcast notifications are notification messages that are targeted to all subscribed devices.
Tag notifications are notification messages that are targeted to all subscribed devices to a particular tag.
For more information, select a notification type.
Additional iOS-specific notification types
iOS 7 and above.
Silent notifications is a feature allowing to send notifications without disturbing the user. Notifications are not shown in the notification center or notification bar.
Callback methods are executed even when the application is running in the background.
For more information, refer to the "silent notifications" topics in the MobileFirst Platform user documentation, and in Apple's user documentation.
Server API for silent notification
To implement silent notification in the case of event source/broadcast/tag-based notifications, create a notification object by using the
WL.Server.createDefaultNotification API and set the type as below:
notification.APNS.type = "DEFAULT" | "SILENT" | "MIXED";
DEFAULTmeans normal notification, which shows the alert and is kept in the notification center if the application is running in the background.
SILENTmeans silent notification, which does not show any alert or the message is not placed in the notification center. In the silent type, the aps tag of push notification contains only content-available.
MIXEDmeans a combination of the above: This option invokes the callback method silently and shows the alert.
Client-side API for silent notification
To handle silent notification on the client-side:
- Enable the application capability to perform background tasks on receiving the remote notifications
To enable background processing, select the project in XCode and in the capabilities tab, select the appropriate background modes, like Remote notifications and Background fetch.
- Implement a new callback method in the
(application: didReceiveRemoteNotification:fetchCompletionHandler:)to receive silent notifications when the application is running in the background.
- In the callback, check whether the notification is silent by checking that the key content-available is set to 1.
- Call the
fetchCompletionHandlerblock method at the end of the notification handler.
iOS 8 and above.
Interactive notification enables users to take actions when a notification is received without the application being open.
When an interactive notification is received, the device shows action buttons along with the notification message.
For more information, refer to the "interactive notifications" topics in the MobileFirst Platform user documentation, and in Apple's user documentation.
Server API for interactive notification
To send interactive notification, set a string to indicate the category.
Categories describe a custom type of notification that your application sends and contains actions that a user can perform in response.
- For event-source notifications, create a notification object and set type as below:
notification.APNS.category = “poll";
- For broadcast/tag-based notifications, create a notification object and set the type as below:
notification.settings.apns.category = “poll";
- The category name must be same as the one used on the client side.
Client-side steps for interactive notification
On the client side, to handle interactive notification:
- 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.
To enable background processing, select the project in XCode and in the capabilities tab, select the appropriate background modes like Remote notifications and Background fetch.
- Set categories before setting
(application: didRegisterForRemoteNotificationsWithDeviceTokenapplication:)method in
- Implement the new callback method:
This new callback method is invoked when the 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
Select a notification type▲
- Broadcast notifications in native iOS applications
- Event source-based notifications in native iOS applications
- Tag-based notifications in native iOS applications