一、安装插件:

1、phonegaplocal plugin add https://github.com/phonegap-build/PushPlugin.git

2、phonegap plugin add cordova-plugin-device

3、phonegap plugin add cordova-plugin-media

二、在js文件中调用

var pushNotification;

var app = {
// Application Constructor

initialize: function() {
this.bindEvents();

},

// Bind Event Listeners

//

// Bind any events that are required on startup. Common events are:

// ‘load’, ‘deviceready’, ‘offline’, and ‘online’.

bindEvents: function() {
document.addEventListener(‘deviceready’, this.onDeviceReady, false);

 

},

// deviceready Event Handler

//

// The scope of ‘this’ is the event. In order to call the ‘receivedEvent’

// function, we must explicitly call ‘app.receivedEvent(…);’

 

onDeviceReady: function() {
app.receivedEvent(‘deviceready’);

app.registerN();

 

},

// Update DOM on a Received Event

receivedEvent: function(id) {

var parentElement = document.getElementById(id);

var listeningElement = parentElement.querySelector(‘.listening’);

var receivedElement = parentElement.querySelector(‘.received’);

listeningElement.setAttribute(‘style’, ‘display:none;’);

receivedElement.setAttribute(‘style’, ‘display:block;’);

console.log(‘Received Event: ‘ + id);

},

registerN: function() {

pushNotification = window.plugins.pushNotification;

 

pushNotification.register(

this.tokenHandler,

this.errorHandler,

{
“badge”:”true”,

“sound”:”true”,

“alert”:”true”,

“ecb”:”onNotificationAPN”

});

},

onNotificationAPN: function(event) {
if ( event.alert )

{
navigator.notification.alert(event.alert);

}

 

if ( event.sound )

{
var snd = new Media(event.sound);

snd.play();

}

 

if ( event.badge )

{
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);

}

},

tokenHandler: function(result) {
alert (‘success’);

alert(‘device token = ‘ + result);

},

errorHandler: function(error) {
alert(‘register erro’);

alert(‘error = ‘ + error);

}

};