Integrating Cordova Plugin
How to integrate Pushwoosh SDK into your Cordova project

Supports iOS, Android, Windows Phone, Windows

Note:

iOS Simulator can neither subscribe nor receive push notifications. Android and Windows Phone emulator will work.
To integrate Pushwoosh with your Cordova application, follow these simple steps:
1. Install the Plugin source code into your app:
    For Phonegap: phonegap plugin add pushwoosh-pgb-plugin
    For Cordova: cordova plugin add pushwoosh-cordova-plugin
    For Ionic: ionic plugin add pushwoosh-cordova-plugin
    For Intel XDK: Add pushwoosh-intel-xdk-plugin as Third-Party Plugin
2. Whitelist .pushwoosh.com domain in the config.xml file:
1
<access origin="*.pushwoosh.com" />
Copied!
Do not forget to add cordova-plugin-whitelist plugin for android platform. cordova plugin add cordova-plugin-whitelist
3. Get the google-services.json from your Firebase console as described here and add it to your project.
4. Add the following section to your config.xml:
1
<platform name="android">
2
<resource-file src="google-services.json" target="app/google-services.json" />
3
...
4
</platform>
Copied!
When building with Phonegap Build you will also need to use cordova-cli:8.0.0:
config.xml
1
<preference name="phonegap-version" value="cli-8.0.0" />
Copied!
5. Initialize plugin
Add the following function to your javascript file, enter the correct Pushwoosh App ID. For Android also enter the correct Google Project Number. serviceName is optional for Windows Phone (wp8)
Cordova/PhoneGap/Indel XDK
Ionic
1
function initPushwoosh() {
2
var pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");
3
4
// Should be called before pushwoosh.onDeviceReady
5
document.addEventListener('push-notification', function(event) {
6
var notification = event.notification;
7
// handle push open here
8
});
9
10
// Initialize Pushwoosh. This will trigger all pending push notifications on start.
11
pushwoosh.onDeviceReady({
12
appid: "PUSHWOOSH_APP_ID",
13
projectid: "YOUR_FCM_SENDER_ID",
14
serviceName: "MPNS_SERVICE_NAME"
15
});
16
}
Copied!
1
initPushwoosh() {
2
var pushNotification = (<any>window).plugins.pushNotification;
3
4
//set push notifications handler
5
document.addEventListener('push-notification',
6
function(event) {
7
var message = (<any>event).notification.message;
8
var userData = (<any>event).notification.userdata;
9
10
alert("Push message opened: " + message);
11
console.info(JSON.stringify((<any>event).notification));
12
13
//dump custom data to the console if it exists
14
if (typeof(userData) != "undefined") {
15
console.warn('user data: ' + JSON.stringify(userData));
16
}
17
}
18
);
19
20
document.addEventListener('push-receive',
21
function (event) {
22
var message = (<any>event).notification.message;
23
var userData = (<any>event).notification.userdata;
24
25
alert("Push message received: " + message);
26
console.info(JSON.stringify((<any>event).notification));
27
28
//dump custom data to the console if it exists
29
if (typeof (userData) != "undefined") {
30
console.warn('user data: ' + JSON.stringify(userData));
31
}
32
}
33
);
34
35
//initialize Pushwoosh with projectid: "YOUR_FCM_SENDER_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
36
pushNotification.onDeviceReady({
37
projectid: "YOUR_FCM_SENDER_ID",
38
appid: "PUSHWOOSH_APP_ID",
39
serviceName: ""
40
});
41
42
//register for push notifications
43
var app = this;
44
pushNotification.registerDevice(
45
function(status) {
46
alert("registered with token: " + status.pushToken);
47
},
48
function(status) {
49
alert("failed to register: " + status);
50
console.warn(JSON.stringify(['failed to register ', status]));
51
}
52
);
53
}
Copied!
Call initPushwoosh(); on application start.
Example:
Cordova/PhoneGap
Ionic
Intel XDK
1
bindEvents: function() {
2
document.addEventListener('deviceready', this.onDeviceReady, false);
3
},
4
// deviceready Event Handler
5
//
6
// The scope of 'this' is the event. In order to call the 'receivedEvent' function, we must explicitly call 'app.receivedEvent(...);'
7
onDeviceReady: function() {
8
app.receivedEvent('deviceready');
9
initPushwoosh();
10
},
Copied!
1
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
2
.run(function($ionicPlatform) {
3
$ionicPlatform.ready(function() {
4
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
5
// for form inputs)
6
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
7
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
8
cordova.plugins.Keyboard.disableScroll(true);
9
10
}
11
if (window.StatusBar) {
12
// org.apache.cordova.statusbar required
13
StatusBar.styleLightContent();
14
}
15
16
initPushwoosh();
17
});
18
})
Copied!
1
function onAppReady() {
2
if( navigator.splashscreen && navigator.splashscreen.hide ) { // Cordova API detected
3
navigator.splashscreen.hide() ;
4
}
5
6
initPushwoosh();
7
}
8
document.addEventListener("app.Ready", onAppReady, false) ;
Copied!
6. Registering and handling push notifications
To register for push notifications call
1
pushwoosh.registerDevice(
2
function(status) {
3
var pushToken = status.pushToken;
4
// handle successful registration here
5
},
6
function(status) {
7
// handle registration error here
8
}
9
);
Copied!
For handling notificatios see the following snippet of code in initPushwoosh function
1
// should be called before pushwoosh.onDeviceReady
2
document.addEventListener('push-notification', function(event) {
3
var notification = event.notification;
4
// handle push open here
5
});
Copied!
7. iOS platform If you use cordova-ios earlier than 4.3.0, enable Push Notifications in Capabilities section in XCode project.
8. Android platform
Check out the latest Google Play services and Android Support libraries using Android SDK Manager.
Starting from version 8.0.0 of the plugin, Pushwoosh Android SDK uses Android X libraries instead of Android Support. To enable Android X in your android project, use cordova-plugin-androidx.
9. Windows platform
Pushwoosh plugin for windows platform uses WNS. Associate App with the Store in Visual Studio and set Notifications->Toast Capable; Please note that all UWP apps (Windows 10 and higher) are by default capable of sending toast notifications, without the need to declare such capability.
Pretty easy, isn't it!
Last modified 1yr ago
Copy link