{"__v":30,"_id":"55a3c52253611017004386bd","category":{"__v":0,"_id":"55b9ffa5d72d1e1900276a39","project":"5540ce1b31827a0d007ab1cc","version":"5540ce1c31827a0d007ab1cf","pages":[],"reference":false,"createdAt":"2015-07-30T10:42:45.253Z","from_sync":false,"order":15,"slug":"cordova-phonegap","title":"Cordova (PhoneGap)"},"project":"5540ce1b31827a0d007ab1cc","user":"5540cdbf5cf9682100d61a97","version":{"__v":27,"_id":"5540ce1c31827a0d007ab1cf","project":"5540ce1b31827a0d007ab1cc","createdAt":"2015-04-29T12:27:08.390Z","releaseDate":"2015-04-29T12:27:08.390Z","categories":["5540ce1c31827a0d007ab1d0","5540d91bbb9e762d00f594ad","5540e5f131827a0d007ab212","5540e5febb9e762d00f594d3","5540e61331827a0d007ab213","5540e6195cf9682100d61afa","5540e62631827a0d007ab214","5540e63031827a0d007ab215","5540e63531827a0d007ab216","5540e63e5cf9682100d61afc","5540e6445cf9682100d61afd","5540e64a5cf9682100d61afe","55a4ff5b2e70c0250038050f","55acb28318eefd0d0071d504","55ae1abe8576b92300291c80","55ae453ef302af23000ac109","55af586d555b900d0036d296","55af91dac8a85321007a53c3","55b9fee204775a2f00628071","55b9ff0e04775a2f00628072","55b9ff4604775a2f00628073","55b9ff5fd72d1e1900276a38","55b9ff7f04775a2f00628074","55b9ff90eb08801900f833e5","55b9ffa5d72d1e1900276a39","55b9ffca04775a2f00628075","55b9fffdd72d1e1900276a3a"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"createdAt":"2015-07-13T14:03:14.795Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"order":0,"body":"[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Supports iOS, Android, Windows Phone\"\n}\n[/block]\n[**Download SDK**](https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin)\n[**Download Sample**](https://github.com/Pushwoosh/pushwoosh-phonegap-cordova-sample)\n[**Plugin API Docs**](https://rawgit.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin/master/Documentation/files/PushNotification-js.html) \n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"iOS Simulator can neither subscribe nor receive push notifications. Android and Windows Phone emulator will work.\",\n  \"title\": \"Note:\"\n}\n[/block]\nTo integrate Pushwoosh with your Cordova application you need to do simple following steps:\n\n1. Install the Plugin source code for Phonegap push notifications into your app by running the following command in the command line:\n\n  * For Phonegap: `phonegap plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin.git`\n  * For Cordova: `cordova plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin.git`\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"IMPORTANT: You also need to add **cordova-plugin-whitelist** plugin.\\n`cordova plugin add cordova-plugin-whitelist`\"\n}\n[/block]\n2. Whitelist ***.pushwoosh.com** domain in the **config.xml** file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<access origin=\\\"*.pushwoosh.com\\\" />\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\n3. Registering for push notifications:\n\nAdd the following function to your javascript file, enter the correct Pushwoosh App ID.\nFor **Android** also enter the correct Google Project Number.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function initPushwoosh() {\\n    var pushNotification = cordova.require(\\\"com.pushwoosh.plugins.pushwoosh.PushNotification\\\");\\n \\n    //set push notification callback before we initialize the plugin\\n    document.addEventListener('push-notification', function(event) {\\n                                //get the notification payload\\n                                var notification = event.notification;\\n \\n                                //display alert to the user for example\\n                                alert(notification.aps.alert);\\n                               \\n                                //clear the app badge\\n                                pushNotification.setApplicationIconBadgeNumber(0);\\n                            });\\n \\n    //initialize the plugin\\n    pushNotification.onDeviceReady({pw_appid:\\\"PUSHWOOSH_APP_ID\\\"});\\n     \\n    //register for pushes\\n    pushNotification.registerDevice(\\n        function(status) {\\n            var deviceToken = status['deviceToken'];\\n            console.warn('registerDevice: ' + deviceToken);\\n        },\\n        function(status) {\\n            console.warn('failed to register : ' + JSON.stringify(status));\\n            alert(JSON.stringify(['failed to register ', status]));\\n        }\\n    );\\n     \\n    //reset badges on app start\\n    pushNotification.setApplicationIconBadgeNumber(0);\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"iOS Javascript\"\n    },\n    {\n      \"code\": \"function initPushwoosh()\\n{\\n    var pushNotification = cordova.require(\\\"com.pushwoosh.plugins.pushwoosh.PushNotification\\\");\\n \\n    //set push notifications handler\\n    document.addEventListener('push-notification', function(event) {\\n        var title = event.notification.title;\\n        var userData = event.notification.userdata;\\n                                 \\n        if(typeof(userData) != \\\"undefined\\\") {\\n            console.warn('user data: ' + JSON.stringify(userData));\\n        }\\n                                     \\n        alert(title);\\n    });\\n \\n    //initialize Pushwoosh with projectid: \\\"GOOGLE_PROJECT_NUMBER\\\", pw_appid : \\\"PUSHWOOSH_APP_ID\\\". This will trigger all pending push notifications on start.\\n    pushNotification.onDeviceReady({ projectid: \\\"GOOGLE_PROJECT_NUMBER\\\", pw_appid : \\\"PUSHWOOSH_APP_ID\\\" });\\n \\n    //register for pushes\\n    pushNotification.registerDevice(\\n        function(status) {\\n            var pushToken = status;\\n            console.warn('push token: ' + pushToken);\\n        },\\n        function(status) {\\n            console.warn(JSON.stringify(['failed to register ', status]));\\n        }\\n    );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Android Javascript\"\n    },\n    {\n      \"code\": \"function initPushwoosh()\\n{\\n    var pushNotification = cordova.require(\\\"com.pushwoosh.plugins.pushwoosh.PushNotification\\\");\\n \\n    //set push notifications handler\\n    document.addEventListener('push-notification', function(event) {\\n                //get the notification payload\\n                var notification = event.notification;\\n \\n                //display alert to the user for example\\n                alert(JSON.stringify(notification));\\n    });\\n \\n    //initialize the plugin\\n    pushNotification.onDeviceReady({ appid: \\\"PUSHWOOSH_APP_ID\\\", serviceName: \\\"\\\" });\\n \\n    //register for pushes\\n    pushNotification.registerDevice(\\n        function(status) {\\n            var pushToken = status;\\n            console.warn('push token: ' + pushToken);\\n        },\\n        function(status) {\\n            console.warn(JSON.stringify(['failed to register ', status]));\\n        }\\n    );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Windows Phone Javascript\"\n    }\n  ]\n}\n[/block]\nIn your **onDeviceReady** function add: `initPushwoosh();`\n\nExample:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"bindEvents: function() {\\n    document.addEventListener('deviceready', this.onDeviceReady, false);\\n},\\n// deviceready Event Handler\\n//\\n// The scope of 'this' is the event. In order to call the 'receivedEvent' function, we must explicitly call 'app.receivedEvent(...);'\\nonDeviceReady: function() {\\n    app.receivedEvent('deviceready');\\n    initPushwoosh();\\n},\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n4. Receiving push notifications. See the following snippet of code in **initPushwoosh** function\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"document.addEventListener('push-notification', function(event) {\\n             var notification = event.notification;\\n             alert(notification.aps.alert);\\n             pushNotification.setApplicationIconBadgeNumber(0);\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"iOS Javascript\"\n    },\n    {\n      \"code\": \"document.addEventListener('push-notification', function(event) {\\n    var title = event.notification.title;\\n    var userData = event.notification.userdata;\\n \\n    console.warn('user data: ' + JSON.stringify(userData));\\n    alert(title);\\n});\\n\",\n      \"language\": \"javascript\",\n      \"name\": \"Android Javascript\"\n    },\n    {\n      \"code\": \"document.addEventListener('push-notification', function(event) {\\n    //get the notification payload\\n    var notification = event.notification;\\n \\n    //display alert to the user for example\\n    alert(JSON.stringify(notification));\\n});\\n\",\n      \"language\": \"javascript\",\n      \"name\": \"Windows Phone Javascript\"\n    }\n  ]\n}\n[/block]\nWasn’t it too easy?\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"Due to compatibility with Android 4 please make sure you are using at least 11 version of the Android API.\\nThe SDK will run on older devices.\"\n}\n[/block]","excerpt":"How to integrate Pushwoosh SDK into your Cordova project","slug":"cordova-phonegap","type":"basic","title":"Cordova / PhoneGap"}

Cordova / PhoneGap

How to integrate Pushwoosh SDK into your Cordova project

[block:callout] { "type": "success", "title": "Supports iOS, Android, Windows Phone" } [/block] [**Download SDK**](https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin) [**Download Sample**](https://github.com/Pushwoosh/pushwoosh-phonegap-cordova-sample) [**Plugin API Docs**](https://rawgit.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin/master/Documentation/files/PushNotification-js.html) [block:callout] { "type": "info", "body": "iOS Simulator can neither subscribe nor receive push notifications. Android and Windows Phone emulator will work.", "title": "Note:" } [/block] To integrate Pushwoosh with your Cordova application you need to do simple following steps: 1. Install the Plugin source code for Phonegap push notifications into your app by running the following command in the command line: * For Phonegap: `phonegap plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin.git` * For Cordova: `cordova plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin.git` [block:callout] { "type": "warning", "body": "IMPORTANT: You also need to add **cordova-plugin-whitelist** plugin.\n`cordova plugin add cordova-plugin-whitelist`" } [/block] 2. Whitelist ***.pushwoosh.com** domain in the **config.xml** file: [block:code] { "codes": [ { "code": "<access origin=\"*.pushwoosh.com\" />", "language": "xml" } ] } [/block] 3. Registering for push notifications: Add the following function to your javascript file, enter the correct Pushwoosh App ID. For **Android** also enter the correct Google Project Number. [block:code] { "codes": [ { "code": "function initPushwoosh() {\n var pushNotification = cordova.require(\"com.pushwoosh.plugins.pushwoosh.PushNotification\");\n \n //set push notification callback before we initialize the plugin\n document.addEventListener('push-notification', function(event) {\n //get the notification payload\n var notification = event.notification;\n \n //display alert to the user for example\n alert(notification.aps.alert);\n \n //clear the app badge\n pushNotification.setApplicationIconBadgeNumber(0);\n });\n \n //initialize the plugin\n pushNotification.onDeviceReady({pw_appid:\"PUSHWOOSH_APP_ID\"});\n \n //register for pushes\n pushNotification.registerDevice(\n function(status) {\n var deviceToken = status['deviceToken'];\n console.warn('registerDevice: ' + deviceToken);\n },\n function(status) {\n console.warn('failed to register : ' + JSON.stringify(status));\n alert(JSON.stringify(['failed to register ', status]));\n }\n );\n \n //reset badges on app start\n pushNotification.setApplicationIconBadgeNumber(0);\n}", "language": "javascript", "name": "iOS Javascript" }, { "code": "function initPushwoosh()\n{\n var pushNotification = cordova.require(\"com.pushwoosh.plugins.pushwoosh.PushNotification\");\n \n //set push notifications handler\n document.addEventListener('push-notification', function(event) {\n var title = event.notification.title;\n var userData = event.notification.userdata;\n \n if(typeof(userData) != \"undefined\") {\n console.warn('user data: ' + JSON.stringify(userData));\n }\n \n alert(title);\n });\n \n //initialize Pushwoosh with projectid: \"GOOGLE_PROJECT_NUMBER\", pw_appid : \"PUSHWOOSH_APP_ID\". This will trigger all pending push notifications on start.\n pushNotification.onDeviceReady({ projectid: \"GOOGLE_PROJECT_NUMBER\", pw_appid : \"PUSHWOOSH_APP_ID\" });\n \n //register for pushes\n pushNotification.registerDevice(\n function(status) {\n var pushToken = status;\n console.warn('push token: ' + pushToken);\n },\n function(status) {\n console.warn(JSON.stringify(['failed to register ', status]));\n }\n );\n}", "language": "javascript", "name": "Android Javascript" }, { "code": "function initPushwoosh()\n{\n var pushNotification = cordova.require(\"com.pushwoosh.plugins.pushwoosh.PushNotification\");\n \n //set push notifications handler\n document.addEventListener('push-notification', function(event) {\n //get the notification payload\n var notification = event.notification;\n \n //display alert to the user for example\n alert(JSON.stringify(notification));\n });\n \n //initialize the plugin\n pushNotification.onDeviceReady({ appid: \"PUSHWOOSH_APP_ID\", serviceName: \"\" });\n \n //register for pushes\n pushNotification.registerDevice(\n function(status) {\n var pushToken = status;\n console.warn('push token: ' + pushToken);\n },\n function(status) {\n console.warn(JSON.stringify(['failed to register ', status]));\n }\n );\n}", "language": "javascript", "name": "Windows Phone Javascript" } ] } [/block] In your **onDeviceReady** function add: `initPushwoosh();` Example: [block:code] { "codes": [ { "code": "bindEvents: function() {\n document.addEventListener('deviceready', this.onDeviceReady, false);\n},\n// deviceready Event Handler\n//\n// The scope of 'this' is the event. In order to call the 'receivedEvent' function, we must explicitly call 'app.receivedEvent(...);'\nonDeviceReady: function() {\n app.receivedEvent('deviceready');\n initPushwoosh();\n},", "language": "javascript" } ] } [/block] 4. Receiving push notifications. See the following snippet of code in **initPushwoosh** function [block:code] { "codes": [ { "code": "document.addEventListener('push-notification', function(event) {\n var notification = event.notification;\n alert(notification.aps.alert);\n pushNotification.setApplicationIconBadgeNumber(0);\n});", "language": "javascript", "name": "iOS Javascript" }, { "code": "document.addEventListener('push-notification', function(event) {\n var title = event.notification.title;\n var userData = event.notification.userdata;\n \n console.warn('user data: ' + JSON.stringify(userData));\n alert(title);\n});\n", "language": "javascript", "name": "Android Javascript" }, { "code": "document.addEventListener('push-notification', function(event) {\n //get the notification payload\n var notification = event.notification;\n \n //display alert to the user for example\n alert(JSON.stringify(notification));\n});\n", "language": "javascript", "name": "Windows Phone Javascript" } ] } [/block] Wasn’t it too easy? [block:callout] { "type": "warning", "body": "Due to compatibility with Android 4 please make sure you are using at least 11 version of the Android API.\nThe SDK will run on older devices." } [/block]