Customizing Cordova Plugin

How to customize Pushwoosh SDK for Cordova project

Push Notifications in the Foreground

When receiving a push in the background, no events are triggered until a push notification is clicked. After it is opened, the Pushwoosh plugin fires push-receive and push-notificationevents.

When a push is received in the foreground, the plugin fires push-receive automatically, and creates a notification in the Notification Center. When this notification is opened, it fires push-notification.

You can listen to push-receive event to immediately react on a push in case it is received in the foreground, e.g. update content on a current page in your app. push-notification, on the other side, is used to react to a notification click event, which requires user interaction, e.g. to navigate within your app, trigger a new process in your app, etc.

To change the push notification foreground appearance, you can use IOS_FOREGROUND_ALERT_TYPE and ANDROID_FOREGROUND_PUSH preferences:

IOS_FOREGROUND_ALERT_TYPE

  • NONE – do not display a notification when the app is in the foreground (default)

  • BANNER – displays a banner in-app alert

  • ALERT – alert notification

ANDROID_FOREGROUND_PUSH

  • true – do not automatically handle foreground pushes

  • false – automatically handle foreground pushes (default)

Example:

cordova plugin add pushwoosh-cordova-plugin --variable IOS_FOREGROUND_ALERT_TYPE="ALERT" --variable ANDROID_FOREGROUND_PUSH="true"

Custom Push Sound

To enable custom notification sound on Android, place the sound files in www/res folder and specify every sound in config.xml:

<?xml version=‘1.0’ encoding=‘utf-8’?>
<widget id="YOUR_ID" version="1.0.0" xmlns="http://www.w3.org/ns/widgets” xmlns:cdv=“http://cordova.apache.org/ns/1.0">
	...
    <platform name="android">
        <allow-intent href="market:*" />
		...
        <-- Add this line for each sound -->
        <resource-file src="www/res/push.wav" target="res/raw/push.wav" />
    </platform>
</widget>

The sounds will be accessible in the Sound dropdown menu of the Send Push panel.

On iOS, sound files are not automatically associated with the Pushwoosh Application yet, but can be used manually, e.g. www/res/sound_name.wav.

iOS

iOS notifications support only .aiff, .wav and, .caf file formats.

Custom Push Icon

Push notification icon can be customized on Android locally by using pw_notification as resource name for such icon (see sample 1 2); or remotely by referencing any resource name in the Icon setting in the Send Push panel.

Custom Push Data

Custom push data can be sent using the Action setting in the Send Push panel. To receive and handle such data use the following code:

document.addEventListener('push-notification',
  function(event) {
    var message = event.notification.message; // Push message
    var userData = event.notification.userdata; // Custom push data

    if (userData) {
      // handle custom push data here
    }
  }
);

Controlling Log Level

In order to assist with debugging and integration, SDK will print all the requests to the console by default. When you are ready for the production build, set LOG_LEVEL variable to one of the following values:

  • NONE – no logs from the SDK

  • ERROR – only display errors in the console

  • WARNING – show warnings

  • INFO – show informational messages

  • DEBUG – show everything, including debug information (default)

Example:

cordova plugin add pushwoosh-cordova-plugin --variable LOG_LEVEL="INFO"

Deep linking

We recommend using https://github.com/EddyVerbruggen/Custom-URL-scheme It's super easy to set deep links for Cordova and it works with Pushwoosh deep linking functionality out of the box.

You can find installation and usage in this guide or in the plugin docs.

Rich Media JS Bridge

You can call your JS functions from Rich Media in Cordova WebView via javascript interfaces. A call from a Rich Media must have the following format:

<interface>.callFunction('<function_name>', <params_string>)

Example:

1. Create Rich Media with a javascript call:

testBridge.callFunction('testFunction', JSON.stringify({'param1' : 1, 'param2':'test'}))

2. Add a javascript interface to your project:

pushNotification.addJavaScriptInterface('testBridge');

3. Add function:

function testFunction(params) {
    alert("Bridge is working! " + params.param1 + " " + params.param2);
}

Using Pushwoosh plugin with other FCM services

If your app uses other plugins that rely on firebase-messaging, it might result in conflicts, or in one of the plugins not working as expected. To resolve this conflict, you can add a custom FirebaseMessagingService class that will route push notifications between the plugins. Pushwoosh Cordova plugin contains a template of this class which you can use as is to force all notifications to go through Pushwoosh handlers or modify it to handle notifications from other providers as well. To enable the template, uncomment the following lines in the plugin's plugin.xml:

    <service android:name="com.pushwoosh.plugin.pushnotifications.CustomFirebaseMessagingService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

Share your feedback with us

Your feedback helps us create a better experience, so we would love to hear from you if you have any issues during the SDK integration process. If you face any difficulties, please do not hesitate to share your thoughts with us via this form.

Last updated