Push notifications

Push notifications

Implement push notifications into your application with SAP Cloud Platform mobile service for development and operations.

Details

You will learn

In this tutorial, you will implement push notifications into your application, and use the SAP Cloud Platform mobile service for development and operations push notification configuration settings.

You can use the Apple Push Notification Service to propagate information from the backend to the device. In this tutorial you use the native iOS push services to enable APNS for your iOS application. To enable your application for push notifications, you need to carry out the following tasks:

Create CSR (Certificate Signing Request) file

Create an App ID

Create provisioning profile

Update your application to use Push Notifications

Configure APNS in SAP Cloud Platform cockpit

In order to implement Push Notifications, a paid Apple developer account is required. Students or other developers with a personal Apple ID for their team will not be able to use push notifications, because they won’t have access to the Developer Portal to generate the required certificate.

Step 1: Create CSR (Certificate Signing Request) file

First, we create the CSR file.

On your Mac, open the Keychain Access application, and navigate to Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority…

In the dialog, enter the email address you use which is associated with your Apple Developer account. Also, make sure you tick the Request is saved to disk option.

Click Continue.

Choose a folder to store the certificate – it is good practice to store generated files in a separate folder for each project – and click Save.

Once you see a dialog saying the certificate is saved successfully, click Done to finish.

The certificate you just saved will be used to sign other certificates in the Apple Developer website.

Step 2: Create an App ID

Now, you need to create a new App ID in the Apple Developer Member Center. This App ID is used to identify your app and ensures the Apple Push Notification Servers properly push notifications to your app.

First, the device registers for remote notifications by calling the method registerForRemoteNotification. Then, you have specified the types of notifications your app will support, as well as added a reference to the notification settings class, which enables your application for the push notifications to be received. This will result in your application to display the “App Name” Would Like to Send You Notifications confirmation dialog.

Let’s look at the registerForRemoteNotification method. In this method, a reference to the SDK Foundation class SAPcpmsRemoteNotificationClient is made. With this API, you will upload the received device token to SAP Cloud Platform mobile service for development and operations:

With these two methods, you can receive notifications, but your application also needs to respond to them in a useful manner. Therefor, two delegate methods are implemented:

// Called to let your app know which action was selected by the user for a given notification.
func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
self.logger.info("App opened via user selecting notification: \(response.notification.request.content.body)")
// Here is where you want to take action to handle the notification, maybe navigate the user to a given screen.
completionHandler()
}
// Called when a notification is delivered to a foreground app.
func userNotificationCenter(_: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
self.logger.info("Remote Notification arrived while app was in forground: \(notification.request.content.body)")
// Currently we are presenting the notification alert as the application were in the backround.
// If you have handled the notification and do not want to display an alert, call the completionHandle with empty options: completionHandler([])
completionHandler([.alert, .sound])
}

If you now run your application from your device, you will notice it will first ask permission to display notifications:

Go back to your SAP Cloud Platform mobile service for development and operations Cockpit and navigate to Applications. Click the Action icon next to your application, and select Push from the context menu.

In the Push Desk, find your device using the filter options, and enter the following content in the Message area:

SAPcpms says: 'How do you like the tutorials so far?

If you click Send, your device should now receive a new push notification:

If you happen to have an Apple Watch, and have it configured to display push notifications received on your iPhone, the push notification you have sent via SAP Cloud Platform mobile service for development and operations will also be displayed on your Apple Watch: