Skip to content

Push Notification Events

Mobile Development Kit provides events for handling push notification.

The SAP Mobile Services' push feature can be used to push updates from the back-end data source to applications that are running on mobile devices, including Mobile Development Kit App. Push notification for Mobile Development Kit apps can be triggered under various circumstances.

OnReceiveForegroundNotification

Triggered when a notification is delivered to a foreground application. The notification title, body and payload are exposed to you as part of application events.

To send push notification on SAP Mobile Services, please see Configuring Push Notifications and Sending Push Notifications

Handling the Event in Your Rule

You can use the ClientAPI object that you received as an argument to retrieve the "AppEventData" property by calling getAppEventData function. Then, extract the notification payload from "AppEventData":

  const appEventData = clientAPI.getAppEventData();  
  const payload = appEventData.payload;

You should return an enum of PresentationOptions inside the function block to notify the system about how the notification should be displayed. The return values are:

On iOS devices:

  • None - Do nothing.
  • Alert - Display the alert using the content provided by the notification.
  • Badge - Apply the notification's badge value to the app’s icon.
  • Sound - Play the sound associated with the notification.
  • All - equals Alert | Badge | Sound

On Android devices: The return value has no effect on Android devices.

Example:

// Rules/ForegroundNotificationEventHandler.js

export default function ForegroundNotificationEventHandler(clientAPI) {
    var appEventData = clientAPI.getAppEventData();
    // payload might defferent data between iOS and Android
    // var payload = appEventData.payload;
    clientAPI.setActionBinding({
        title: appEventData.title,
        message: appEventData.body
    });
    return clientAPI.executeAction('/MDKDevApp/Actions/PushNotification/PresentNotification/AsAlert.action').then(()=>{
        return appEventData.PresentationOptions.All;
    });
}

OnReceiveFetchCompletion

Triggered when a remote notification arrived that indicates there is data to be fetched. The notification payload is exposed to you as part of application events.

On iOS devices: triggered only when the content-available key is set to true.

On Android devices: triggered only when the payload does not contain notification section.

Send push notification on SAP Mobile Services, as follows:

{
  "customParameters": {
    "apns.contentAvailable": "true"
  }
}

Send push notifications using Apple Push Notification Service (APNs)

{  
  "aps":{  
    "content-avaiable" : "1"  
  }
}

Send push notifications using Firebase

{  
  "data":{  
    "anyKey" : "value"  
  }
}

You will receive a ClientAPI object as an argument, which you can then use to retrieve the AppEventData object by clientAPI.getAppEventData(). The notification payload can then be extracted from AppEventData:

  const appEventData = clientAPI.getAppEventData();
  const payload = appEventData.payload;

Note

On Android, if the payload also contains a "notification" section, OnReceiveFetchCompletion are not triggered when the application is running in the background. If the application is running in the foreground, both OnReceiveFetchCompletion and OnReceiveForegroundNotification are triggered, but you can not get back the "notification" payload in the OnReceiveFetchCompletion event handler.

You should at the end of the function, return a FetchResult enum value that best describes the results of your download operation.

const enum FetchResult {
  NewData = 0,   // New data was successfully downloaded.
  NoData = 1,    // There was no new data to download.
  Failed = 2     // An attempt to download data was made but that attempt failed.
}

Example:

// Rules/ContentAvailableEventHandler.js

export default function ContentAvailableEventHandler(clientAPI) {
    const appEventData = clientAPI.getAppEventData();
    const payload = appEventData.payload;

    // TODO(developer): Handle received messages here.

    // if new data was successfully downloaded, then
    return appEventData.FetchResult.NewData;  //0
}

OnReceiveNotificationResponse

Called to let your app know which action was selected by the user for a given notification. The notification payload and actionIdentifier are exposed to you as part of application events.

You will receive a ClientAPI object as an argument, which you can then use to retrieve the "AppEventData" object by using getAppEventData function, The payload and actionIdentifier can be accessed as such.

  const appEventData = clientAPI.getAppEventData();
  const payload = appEventData.payload;
  const userAction = appEventData.actionIdentifier;

Note

In Android, you cannot get back the notification section of the payload, only the data section is available for this event.

Example:

// Rules/ReceiveNotificationResponseEventHandler.js

export default function ReceiveNotificationResponseEventHandler(clientAPI) {
    const appEventData = clientAPI.getAppEventData();
    const payload = appEventData.payload;
    const userAction = appEventData.actionIdentifier;

    // TODO(developer): Handle user action here.
}

Last update: December 21, 2023