Push data

Attach data payloads to your notifications to update your application in the background

You can send data along with your push notifications. Android and iOS also support sending silent push notifications to update your app in the background without drawing users attention.

The push payload

The push payload is where you can add data to be consumed by your app or website. There are multiple ways of specifying additional data to the push payload using:

Silent notifications

You can send silent notifications on iOS and Android. Silent notifications are un-noticeable by end-users.

Using the API

To send silent notifications using the API, simply omit any textual content in the text or title fields, and do not attach any media to your notification.

Here's an example of sending a silent notification:

curl -XPOST https://management-api.wonderpush.com/v1/deliveries?accessToken="YOUR_ACCESS_TOKEN" \
       -d targetSegmentIds=YOUR_SEGMENT_ID \
       -d notification='{"push": {"payload":{"exampleKey":"exampleValue"}}}'

Using the dashboard

To send silent push notifications using the online dashboard, just choose Attach JSON data in the notification form, specify a JSON payload and check This message just pushes data and has no content.

Consuming push data

Consuming push data is possible on every platform, but each platform has a different way of doing it. In the following sections, we show you how to consume the example payload specified above with iOS devices, Android devices and Websites.

Consuming push data on iOS

Consuming push data on iOS is done in your AppDelegate. All you have to do is:

  • add a remote-notification entry to UIBackgroundModes in your Info.plist file,
  • override the application:didReceiveRemoteNotification:fetchCompletionHandler method of your UIApplicationDelegate implementation:
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
    func application(_ application: UIApplication,
      didReceiveRemoteNotification userInfo: [AnyHashable : Any],
      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        // Print "exampleValue" to your logs
        NSLog("%@", userInfo["exampleKey"])

        // Call completion handler: adapt argument to your usecase
        completionHandler(.noData)
    }
    // ...
}
@implementation AppDelegate
         - (void)application: (UIApplication *)application
didReceiveRemoteNotification: (NSDictionary *)userInfo
      fetchCompletionHandler: (void (^)(UIBackgroundFetchResult result))completionHandler {

    // Print "exampleValue" in your logs:
    NSLog(@"%@", userInfo[@"exampleKey"]);
  
    // Call completion handler: adapt argument to your usecase
    completionHandler(UIBackgroundFetchResultNoData);
  }
  // ... 
@end

Consuming push data on Android

The WonderPush SDK broadcasts a local intent when a data notification is received. Simply register a local BroadcastReceived, preferably in your Application class:

LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_NOTIFICATION_TYPE_DATA.equals(
                intent.getStringExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_NOTIFICATION_TYPE))) {

            Intent pushNotif = intent.getParcelableExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_RECEIVED_PUSH_NOTIFICATION);
            // Read and process the data from the push notification intent

        }
    }
}, new IntentFilter(WonderPush.INTENT_NOTIFICATION_WILL_OPEN));

📘

Make sure you've added localbroadcastmanager to the dependencies section of your app/build.gradle like this:

dependencies {
    // ...
    // Add this line:
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}

Consuming push data on a Website

🚧

Consuming push data on a Website requires updating a Service Worker

If you are not familiar with Service Workers, you'll find Getting started articles on MDN or Google.

Consuming push data on a Website is only possible if you are using your own push notification domain. If you are using a *.by.wonderpush.com domain, it is not possible to consume data from push notification payloads.

To consume data from a push notification, you will have to edit the wonderpush-worker-loader.min.js file you uploaded in the Website Quickstart guide.

// Put the following code after WonderPush.init...

// This registers a listener called each time a push notification is received:
self.addEventListener('push', function(event) {
  // This prints "exampleValue" to the service worker console.
  // You can open the service worker console in chrome by going to 
  // chrome://serviceworker-internals/ and clicking "Inspect" on the appropriate entry
  console.log(event.data.json()['exampleKey']);
});

Consuming push data on Cordova

document.addEventListener('wonderpush.notificationOpen', function onNotificationOpen(event) {
  // This is called for data notifications when they are received and for regular notifications when they are clicked
  if (event.notificationType == "data") {
    console.log("Received data notification:", event.notification);
  }
}, false);

Using silent push to remove a notification already displayed

Like all messengers do (Whatsapp, Telegram, Facebook messenger...). If you get a push on your phone and desktop both, if you read it from your dektop, push notification is automatically removed / hidden on your phone. To do this, you must:

  1. Set a tag when sending your push:
curl -XPOST https://management-api.wonderpush.com/v1/deliveries \
    -d accessToken=YOUR_APPLICATION_ACCESS_TOKEN \
    -d targetUserIds=johndoe \
    -d notification='{"alert":{"title":"New message from Kathy","text":"Hi John, how are you?","tag":"UNIQUE_CONVERSATION_ID"}}'
  1. In response to a user click on the notification, mark the conversation as read in your servers, and pass the tag along.

  2. Send a silent push to the user that just read the notification, with the following payload:

    {"_wp":{"receiveActions":[{"type":"closeNotifications","tag":"UNIQUE_CONVERSATION_ID"}]}}