The WonderPush Developer Hub

Welcome to the WonderPush developer hub. You'll find comprehensive guides and documentation to help you start working with WonderPush as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Advanced click handling

Letting your own code decide what happens on push notification click

Most of the time, handling push notification clicks is done automatically by our SDKs. But in some rare cases, you have to handle the clicks yourself to trigger proprietary logic in your app or website.

In this document, we list a variety of techniques you can use to handle clicks from push notifications in a modular and evolutive way.

Handling click broadcasts

When a notification is clicked whose action is to open the special URL wonderpush://notificationOpen/broadcast, our SDKs forward this click to your app or website without taking any other action.

Such notifications can be created from the online dashboard by choosing the option to Let application code decide from the compose tab of the notification edition interface.

You can listen to these clicks and decide what to do by adding a small amount of code.

🚧

Handling click broadcasts 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.

Handling click broadcasts 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 currently possible to consume data from push notification payloads.

// Only websites using their own push notification domain can handle click broadcasts
// This code goes in the wonderpush-worker-loader.min.js service worker file you've installed at the root of your website when setting up WonderPush

self.addEventListener('WonderPushEvent', function(event) {
  // Click broadcasts are events with .detail.type === 'notificationOpened'
  if (event && event.detail && event.detail.type === 'notificationOpened') {
    
    // The event object contains the original 'notificationclick' event object
    // in its .detail.event property. 
    // The notification clicked is available like so:
    console.log('Notification clicked:', event.detail.event.notification);
  }             
});
// Listen to a NotificationCenter notification.
// A good place to put this code is your AppDelegate's application:didFinishLaunchingWithOptions:

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: WP_NOTIFICATION_OPENED_BROADCAST), object: nil, queue: nil, using: {notification in
    if let pushNotification = notification.userInfo {
        debugPrint("Notification clicked:", pushNotification)
    }
})
// Listen to a NSNotificationCenter notification.
// A good place to put this code is your AppDelegate's application:didFinishLaunchingWithOptions:

[[NSNotificationCenter defaultCenter] addObserverForName:WP_NOTIFICATION_OPENED_BROADCAST object:nil queue:nil usingBlock:^(NSNotification *note) {
    NSDictionary *pushNotification = note.userInfo;
    NSLog(@"Notification clicked: %@", pushNotification);
}];
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);
            Log.d("WonderPush", pushNotif.toString());
        }
    }
}, new IntentFilter(WonderPush.INTENT_NOTIFICATION_WILL_OPEN));

Registering a callback

Registering a callback allows you to execute some code when a push notification is clicked.

Users of the online dashboard can plug your code directly inside their notification buttons by choosing the option to Trigger a registered callback from the compose tab of the notification edition interface:

🚧

This technique is not available in our Website and Cordova SDKs

To register a callback, you'll need a few lines of code. In the following example, we register a callback named example:

// Put the following call in the [application:didFinishLaunchingWithOptions:] method of your AppDelegate
// Here is how to register the callback named "example"
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "example"), object: nil, queue: nil, using: {notification in
    let arg = notification.userInfo?[WP_REGISTERED_CALLBACK_PARAMETER_KEY] as? String
    // Do something useful here
})
// Put the following call in the [application:didFinishLaunchingWithOptions:] method of your AppDelegate
// Here is how to register the callback named "example"
[[NSNotificationCenter defaultCenter] addObserverForName:@"example" object:nil queue:nil usingBlock:^(NSNotification *note) {
    NSString *arg = [note.userInfo objectForKey:WP_REGISTERED_CALLBACK_PARAMETER_KEY];
    // Do something useful here
}];
// Put the following call before you initialize the SDK, in your Application class
// Here is how to register the callback named "example"
IntentFilter exampleMethodIntentFilter = new IntentFilter();
exampleMethodIntentFilter.addAction(WonderPush.INTENT_NOTIFICATION_BUTTON_ACTION_METHOD_ACTION);
exampleMethodIntentFilter.addDataScheme(WonderPush.INTENT_NOTIFICATION_BUTTON_ACTION_METHOD_SCHEME);
exampleMethodIntentFilter.addDataAuthority(WonderPush.INTENT_NOTIFICATION_BUTTON_ACTION_METHOD_AUTHORITY, null);
exampleMethodIntentFilter.addDataPath("/example", PatternMatcher.PATTERN_LITERAL); // Note: prepend a / to the actual method name
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String arg = intent.getStringExtra(WonderPush.INTENT_NOTIFICATION_BUTTON_ACTION_METHOD_EXTRA_ARG);
        // Do something useful here
    }
}, exampleMethodIntentFilter);

Updated 10 months ago

Advanced click handling


Letting your own code decide what happens on push notification click

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.