Skip to content

Push Notifications

Register Push Token from Firebase Cloud Messaging with SAP Cloud Platform 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
RemoteNotificationClient remoteNotificationClient = null;
remoteNotificationClient = new RemoteNotificationClient(ClientProvider.get(),
    CpmsParameters.getSettingsParameters());

// Capabilities may be used by backend sending notification to CPms to send to a specific
// group/user
RemoteNotificationParameters.Capability[] cap = {
    new RemoteNotificationParameters.Capability("SalesAppGroup",
        "Module", "OrderProcessingData"),
    new RemoteNotificationParameters.Capability("SalesAppGroup",
        "Module", "TicketCreationData")};
RemoteNotificationParameters remoteNotificationParameters = new RemoteNotificationParameters.Builder()
    .pushGroup("SalesAppGroup")
    .userLocale(Locale.getDefault().getDisplayName())
    .timeZone(TimeZone.getDefault().getDisplayName())
    .formFactor("phone")
    .capabilities(cap)
    .lastKnownLocation(new RemoteNotificationParameters.LastKnownLocation("32.80776", "12.456546"))//GPDR regulations apply
    .build();

// Must be invoked from UI thread
remoteNotificationClient.registerDeviceToken(token, 
    remoteNotificationParameters, 
    new RemoteNotificationClient.CallbackListener() {

        //Executes on UI thread
        @Override
        public void onSuccess() {
            sLogger.debug("Token registered.");
        }

        // Executes on UI thread
        @Override
        public void onError(@NonNull Throwable ex) {
            // com.sap.cloud.mobile.foundation.networking.HttpException
            if (ex instanceof HttpException) {
                // 401 - bad credentials, 403 - no permission, 404 - push disabled
                sLogger.error("Token could not be registered. Message: {}, http code: {}",
                    ((HttpException) ex).message(), ((HttpException) ex).code());
            } else {
                sLogger.error("Request failed: {}", ex.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var remoteNotificationClient: RemoteNotificationClient? = null
remoteNotificationClient = RemoteNotificationClient(ClientProvider.get(),CpmsParameters.getSettingsParameters())

// Capabilities may be used by backend sending notification to CPms to send to a specific
// group/user
val cap = arrayOf(RemoteNotificationParameters.Capability("SalesAppGroup",
            "Module", "OrderProcessingData"), RemoteNotificationParameters.Capability("SalesAppGroup",
            "Module", "TicketCreationData"))
val remoteNotificationParameters = RemoteNotificationParameters.Builder()
                .pushGroup("SalesAppGroup")
                .userLocale(Locale.getDefault().displayName)
                .timeZone(TimeZone.getDefault().displayName)
                .formFactor("phone")
                .capabilities(cap)
                .lastKnownLocation(RemoteNotificationParameters.LastKnownLocation("32.80776", "12.456546"))//GPDR regulations apply
                .build()

// Must be invoked from UI thread
remoteNotificationClient.registerDeviceToken(token,
            remoteNotificationParameter,
            object : RemoteNotificationClient.CallbackListener {

                //Executes on UI thread
                override fun onSuccess() {
                    sLogger.debug("Token registered.")
                }

                // Executes on UI thread
                override fun onError(ex: Throwable) {
                    // com.sap.cloud.mobile.foundation.networking.HttpException
                    if (ex is HttpException) {
                        // 401 - bad credentials, 403 - no permission, 404 - push disabled
                        sLogger.error("Token could not be registered. Message: {}, http code: {}",
                                ex.message(), ex.code())
                    } else {
                        sLogger.error("Request failed: {}", ex.message)
                    }
                }
})