Customizing Cordova Plugin
How to customize Pushwoosh SDK for Cordova project
When receiving push in background, no events are triggered until a push notification is clicked. After it is opened, Pushwoosh plugin fires
push-receive
and push-notification
events.When a push is received in 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 foreground, e.g. update content on a current page in your app. push-notification
, on the other side, is used to react on 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 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 banner in-app alertALERT
– alert notification
ANDROID_FOREGROUND_PUSH
true
– do not automatically handle foreground pushesfalse
– automatically handle foreground pushes (default)
Example:
cordova plugin add pushwoosh-cordova-plugin --variable IOS_FOREGROUND_ALERT_TYPE="ALERT" --variable ANDROID_FOREGROUND_PUSH="true"
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 Sound dropdown menu of Send Push panel.
On iOS, sound files are not automatically associated with Pushwoosh Application yet, but can be used manually, e.g.
www/res/sound_name.wav
.Custom push data can be sent using 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
}
}
);
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 SDKERROR
– only display errors in the consoleWARNING
– show warningsINFO
– show informational messagesDEBUG
– show everything, including debug information (default)
Example:
cordova plugin add pushwoosh-cordova-plugin --variable LOG_LEVEL="INFO"
1. To enable location tracking in your application:
1.1 Install the Geozones Plugin source code into your app:
- For Phonegap:
phonegap plugin add pushwoosh-geozones-cordova-plugin
- For Cordova:
cordova plugin add pushwoosh-geozones-cordova-plugin
- For Ionic:
ionic plugin add pushwoosh-geozones-cordova-plugin
1.2. For iOS add the following keys to your Info.plist:
NSLocationWhenInUseUsageDescription
– (required) for app to track Geozones only while running in the foreground.NSLocationAlwaysAndWhenInUseUsageDescription
- (required) for app to track Geozones in both conditions, and to show a permission request dialog pop-up.NSLocationAlwaysUsageDescription
– (optional) for app to track Geozones at all times; should be used if your app targets iOS 10 and earlier versions.
On PhoneGap Build these permissions could be added using
cordova-plugin-geolocation
plugin. NSLocationWhenInUseUsageDescription
can be specified using GEOLOCATION_USAGE_DESCRIPTION
variable2. To start location tracking simply call:
var pushwooshGeozones = cordova.require("pushwoosh-geozones-cordova-plugin.PushwooshGeozones");
pushwooshGeozones.startLocationTracking();
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 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 javascript interface to your project:
pushNotification.addJavaScriptInterface('testBridge');
3. Add function:
function testFunction(params) {
alert("Bridge is working! " + params.param1 + " " + params.param2);
}
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 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>
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 modified 9mo ago