Skip to content

Push Notifications

The remote notification client class helps your mobile applications use push notification capabilities provided by SAP Cloud Platform Mobile Services. First, the mobile applications will need to obtain push token from FCM infrastructure and register with mobile services Push service. Once the Push token is registered, the mobile services administration infrastructure can be used to send the notification to devices.

Setting up FCM Capability for an Application

To Enable FCM for your application, follow the Google documentation at Set Up a Firebase Cloud Messaging Client App on Android.

Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure and provides new features. See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

You can find how to set up GCM at Setup GCM on Android.

Notifications

Registering a Device Token

Device tokens are the UUID codes generated by the FCM. The device token which is obtained from the push infrastructure like FCM should be registered with Mobile Services.

The registerDeviceToken method of RemoteNotificationClient class can be used for registering the device token with the mobile service.

This method must be invoked from the UI thread. It takes device token and RemoteNotificationParameters as parameters. The callback methods are invoked on the UI thread.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
RemoteNotificationParameters parameters = new RemoteNotificationParameters.Builder()
    .lastKnownLocation(new RemoteNotificationParameters.LastKnownLocation("32.80776", "12.456546"))
    .build();
RemoteNotificationClient remoteNotificationClient = new RemoteNotificationClient(okHttpClient, settingsParameters);

remoteNotificationClient.registerDeviceToken(token,parameters,
new RemoteNotificationClient.CallbackListener() {
     @Override
      public void onSuccess() {
         //Here goes the code for processing successful response...
      }

      @Override
      public void onError(Throwable result) {
         //Handle your failure handling code here...
        if (result instanceof HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                HttpException ne = (HttpException)result;
                Log.e("Http Exception: " , ne.message() + ", with Error code: " + ne.code());
        } else {
                Log.e("Exception occured: ", result.getMessage());
        }
      }
    }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
val parameters = RemoteNotificationParameters.Builder()
    .lastKnownLocation(RemoteNotificationParameters.LastKnownLocation("32.80776", "12.456546"))
    .build()
val remoteNotificationClient = RemoteNotificationClient(okHttpClient, settingsParameters)

remoteNotificationClient.registerDeviceToken(token, parameters,
    object : RemoteNotificationClient.CallbackListener {
        override fun onSuccess() {
            //Here goes the code for processing successful response...
        }

        override fun onError(result: Throwable) {
            //Handle your failure handling code here...
            if (result is HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                Log.e("Http Exception: ", result.message() + ", with Error code: " + result.code())
            } else {
                Log.e("Exception occured: ", result.message)
            }
        }
    }
)

Unregistering a Device Token

If the mobile application wants to disable notification, it may delete the device token from the mobile service. The unregisterDeviceToken method can be used for that purpose. Once the deregistration is done, the server will not be able to send a push notification to the device.

This method must be invoked from the UI thread. The callback methods are invoked on the UI thread.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
RemoteNotificationClient remoteNotificationClient = new RemoteNotificationClient(okHttpClient, settingsParameters);

remoteNotificationClient.unregisterDeviceToken(
      new RemoteNotificationClient.CallbackListener() {
        @Override
        public void onSuccess() {
            //Here goes the code for processing successful response...
        }
        @Override
        public void onError(Throwable result) {
            //Handle error here...
            if (result instanceof HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                HttpException ne = (HttpException)result;
                Log.e("Http Exception: " , ne.message() + ", with Error code: " + ne.code());
            } else {
                Log.e("Exception occurred: ", result.getMessage());
            }
         }
   }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
val remoteNotificationClient = RemoteNotificationClient(okHttpClient, settingsParameters)

remoteNotificationClient.unregisterDeviceToken(
    object : RemoteNotificationClient.CallbackListener {
        override fun onSuccess() {
            //Here goes the code for processing successful response...
        }

        override fun onError(result: Throwable) {
            //Handle error here...
            if (result is HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                Log.e("Http Exception: ", result.message() + ", with Error code: " + result.code())
            } else {
                Log.e("Exception occurred: ", result.message)
            }
        }
    }
)

Notification Feedback

SAP Cloud Platform Mobile Services provides a feedback service which a mobile application can use to acknowledge the notification. Every notification message from the service carries notificationid that uniquely identifies the notification. Using this unique identification value, the updateNotificationStatus method can be used to update the notification status.

The mobile service supports three statuses for feedback: NOTIFICATIONSTATUS.RECEIVED, NOTIFICATIONSTATUS.CONFIRMED and NOTIFICATIONSTATUS.CONSUMED. The code sample below shows how to provide a received feedback.

This method must be invoked from the UI thread. It takes device notification id and SettingsParameters as parameters. The callback methods are invoked on the UI thread.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
RemoteNotificationClient remoteNotificationClient = new RemoteNotificationClient(okHttpClient, settingsParameters);
remoteNotificationClient.updateNotificationStatus(notificationId, RemoteNotificationClient.NOTIFICATIONSTATUS.RECEIVED,
     new RemoteNotificationClient.CallbackListener() {
         @Override
         public void onSuccess() {
               //Here goes the code for processing successful response...
         }

         @Override
         public void onError(Throwable result) {
            //Handle error here...
            if (result instanceof HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                HttpException ne = (HttpException)result;
                Log.e("Http Exception: " , ne.message() + ", with Error code: " + ne.code());
            } else {
                Log.e("Exception occurred: ", result.getMessage());
            }
         }
     }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
val remoteNotificationClient = RemoteNotificationClient(okHttpClient, settingsParameters)
remoteNotificationClient.updateNotificationStatus(notificationId, RemoteNotificationClient.NOTIFICATIONSTATUS.RECEIVED,
    object : RemoteNotificationClient.CallbackListener {
        override fun onSuccess() {
            //Here goes the code for processing successful response...
        }

        override fun onError(result: Throwable) {
            //Handle error here...
            if (result is HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                Log.e("Http Exception: ", result.message() + ", with Error code: " + result.code())
            } else {
                Log.e("Exception occurred: ", result.message)
            }
        }
    }
)

Support for Baidu Push

Remote notification API can be used to support the Baidu push service. Registering and unregistering the device token is done by specifying Baidu as the push service through SettingsParameter. The settings parameter should be configured prior to creating a RemoteNotificationClient instance.

This following example shows how to register a Baidu push token to Mobile Services:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
RemoteNotificationParameters parameters = new RemoteNotificationParameters.Builder()
    .lastKnownLocation(new RemoteNotificationParameters.LastKnownLocation("32.80776", "12.456546"))
    .build();
//This following line specify that the target we are using is for BAIDU
settingsParameters.setPushService(SettingsParameters.PushService.BAIDU);
RemoteNotificationClient remoteNotificationClient = new RemoteNotificationClient(okHttpClient, settingsParameters);

remoteNotificationClient.registerDeviceToken(token,parameters,
new RemoteNotificationClient.CallbackListener() {
     @Override
      public void onSuccess() {
         //Here goes the code for processing successful response...
      }

      @Override
      public void onError(Throwable result) {
         //Handle your failure handling code here...
        if (result instanceof HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                HttpException ne = (HttpException)result;
                Log.e("Http Exception: " , ne.message() + ", with Error code: " + ne.code());
        } else {
                Log.e("Exception occured: ", result.getMessage());
        }
      }
    }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
val parameters = RemoteNotificationParameters.Builder()
    .lastKnownLocation(RemoteNotificationParameters.LastKnownLocation("32.80776", "12.456546"))
    .build()
//This following line specify that the target we are using is for BAIDU
settingsParameters.pushService = SettingsParameters.PushService.BAIDU
val remoteNotificationClient = RemoteNotificationClient(okHttpClient, settingsParameters)

remoteNotificationClient.registerDeviceToken(token, parameter,
    object : RemoteNotificationClient.CallbackListener {
        override fun onSuccess() {
            //Here goes the code for processing successful response...
        }

        override fun onError(result: Throwable) {
            //Handle your failure handling code here...
            if (result is HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                Log.e("Http Exception: ", result.message() + ", with Error code: " + result.code())
            } else {
                Log.e("Exception occured: ", result.message)
            }
        }
    }
)

This following example shows how to unregister the Baidu push token from Mobile Services:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//This following line specify that the target we are using is for BAIDU
settingsParameters.setPushService(SettingsParameters.PushService.BAIDU);

RemoteNotificationClient remoteNotificationClient = new RemoteNotificationClient(okHttpClient, settingsParameters);

remoteNotificationClient.unregisterDeviceToken(
      new RemoteNotificationClient.CallbackListener() {
        @Override
        public void onSuccess() {
            //Here goes the code for processing successful response...
        }
        @Override
        public void onError(Throwable result) {
            //Handle error here...
            if (result instanceof HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                HttpException ne = (HttpException)result;
                Log.e("Http Exception: " , ne.message() + ", with Error code: " + ne.code());
            } else {
                Log.e("Exception occurred: ", result.getMessage());
            }
         }
   }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//This following line specify that the target we are using is for BAIDU
settingsParameters.pushService = SettingsParameters.PushService.BAIDU

val remoteNotificationClient = RemoteNotificationClient(okHttpClient, settingsParameters)

remoteNotificationClient.unregisterDeviceToken(
    object : RemoteNotificationClient.CallbackListener {
        override fun onSuccess() {
            //Here goes the code for processing successful response...
        }

        override fun onError(result: Throwable) {
            //Handle error here...
            if (result is HttpException) {
                //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
                Log.e("Http Exception: ", result.message() + ", with Error code: " + result.code())
            } else {
                Log.e("Exception occurred: ", result.message)
            }
        }
    }
)