// create the application
cordova create PushExample
// go into the application directory
cd PushExample/
// add android
cordova platform add android
// add ios
cordova platform add ios
For details have a look at https://github.com/phonegap-build/PushPlugin
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
For details have a look at https://github.com/apache/cordova-plugin-device/blob/master/doc/index.md
cordova plugin add org.apache.cordova.device
cordova plugin
should display something similar to this:
com.phonegap.plugins.PushPlugin 2.2.1 "PushPlugin"
org.apache.cordova.device 0.2.10 "Device"
cd www/
bower install mcapjs-client
Download: driver.com.phonegap.plugins.PushPlugin.js to www/js/driver.com.phonegap.plugins.PushPlugin.js
Example: driver.com.phonegap.plugins.PushPlugin.js to www/js/driver.com.phonegap.plugins.PushPlugin.js
.
Add the UI for logging:
<div id="home">
<div id="app-status-div">
<ul id="app-status-ul">
<li>Cordova PushNotification Plugin Demo with mCAP support</li>
</ul>
</div>
</div>
First add the mcapjs-client dependencies:
<script type="text/javascript" src="cordova.js"></script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/backbone/backbone.js"></script>
<script src="bower_components/uri.js/src/URI.js"></script>
<script src="bower_components/mcapjs-client/dist/mcap.js"></script>
Then the driver:
<script src="js/driver.com.phonegap.plugins.PushPlugin.js"></script>
add your business logic
<script type="text/javascript">
function deviceReady(){
// set the url to the mcap server
mCAP.application.set('baseUrl', 'http://mcap.server.io:8443');
// configure the push object with the uuid of the push you want to use
mCAP.push.set('uuid', '5854AE59-8642-4B05-BC71-72B76B4E81E8');
// setup the options
var options = {
// the sender id given from google
senderId: "841515181518",
// register callback
register: function () {
log('register', arguments);
},
// error callback
error: function () {
log('error', arguments);
},
// token received callback
token: function () {
log('token', arguments);
},
// callback when the device was successful registered against the mcap
registerdevice: function () {
log('registerdevice', arguments);
},
// callback when the device registration was unsuccessful
registerdeviceerror: function () {
log('registerdeviceerror', arguments);
},
// callback when a push comes in
message: function () {
log('message', arguments);
},
// if a push was received but that was not a message
unknown: function () {
log('unknown', arguments);
}
};
// register you logic
mCAP.cordovaPushPlugin.onDeviceReady(options);
}
</script>
After that register your businesslogic to the deviceready
event:
<script type="text/javascript">
// just a wrapper
function log(){
console.log.apply(console, arguments);
$('#app-status-ul').append('<li>' + JSON.stringify(arguments) + '</li>')
}
document.addEventListener('deviceready', deviceReady, false);
</script>
- Enabling On-device Developer Options
- Connect an android device via USB
cordova run android
orcordova run ios
- Send a push
cURL
curl 'http://mcap.server.io:8443/push/api/v1/apps/5854AE59-8642-4B05-BC71-72B76B4E81E8/jobs' -H 'Pragma: no-cache' -H 'Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Accept-Language: en-US,en;q=0.8,de;q=0.6' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Cache-Control: no-cache' -H 'Authorization: Basic bWhhbm86c3lzdGVtOm1oYW5v' -H 'Cookie: JSESSIONID=1hv12083zf19e1cktvaaisn0wj' -H 'Connection: keep-alive' --data-binary $'{\n "message" : "Hallo Android and iOS",\n "sound" : "nameOfSound.wav",\n "badge" : "2",\n "extras" : {\n "myHidden" : "Attribute"\n }\n}' --compressed
JavaScript
// Sends a push message to all registered devices
var job = mCAP.push.jobs.add({
"message": "Hello Android and iOS",
"sound": "nameOfSound.wav",
"badge": "2",
"extras": {
"myHidden": "Attribute"
}
});
// login
mCAP.authentication.login().then(function () {
return job.sendPush().then(function () {
log('Send a push message to all registered devices');
});
});
The log on your device should now display the push message