Skip to content

Push Notifications

Register Push Token From Firebase Cloud Messaging with SAP Mobile Services

RemoteNotificationClient remoteNotificationClient = null;
remoteNotificationClient = new RemoteNotificationClient();

// Capabilities may be used by back end 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)
    .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());
            }
        }
});
var remoteNotificationClient: RemoteNotificationClient? = null
remoteNotificationClient = RemoteNotificationClient()

// Capabilities may be used by back end 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)
                .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)
                    }
                }
})

Register Push Token From Firebase Cloud Messaging Using FirebasePushService with SAP Mobile Services

    List services = new ArrayList<MobileService>();

    FirebasePushService pushService = new FirebasePushService();
    pushService.setPushCallbackListener(new FCMPushCallbackListener());
    pushService.setEnableAutoMessageHandling(true);
    pushService.setBackgroundNotificationInterceptor(pushNotificationEvent -> pushNotificationEvent.displayNotification(pushNotificationEvent.getPushRemoteMessage()));
    pushService.setForegroundNotificationInterceptor(pushNotificationEvent -> {
        pushNotificationEvent.displayNotificationWhen(pushNotificationEvent.getPushRemoteMessage(), ()->{
            Activity activity = AppLifecycleCallbackHandler.getInstance().getActivity();
            if (activity == null)
                return false;
            else
                return !activity.getClass().getName().equals("com.company.samlpushmulti.app.WelcomeActivity");
        });
    });
    services.add(pushService);
    SDKInitializer.INSTANCE.start(this, services.toArray(new MobileService[0]), null);
val services = mutableListOf<MobileService>()
services.add(FirebasePushService().apply {npm run lint
        setPushCallbackListener(FCMPushCallbackListener())
        isEnableAutoMessageHandling = true
        setPersistedNotification(true)
    })
SDKInitializer.start(application, * services.toTypedArray(), apiKey = null)

Last update: June 23, 2023