我认为问题是您使用的是该插件的不推荐版本。而不是
cordova plugin add com.phonegap.plugins.pushplugin
使用
cordova plugin add phonegap-plugin-push
并在此文档中编写代码:
https://github.com/phonegap/phonegap-plugin-push
我给你留下了一些有效的代码:
var PushNotificationManager = {
GOOGLE_SENDER_ID: Config.notifications.SENDER_ID,
push: null,
registerDeviceAndroid: function () {
var that = this,
deferredValue = $.Deferred();
that.push = PushNotification.init({
"android": {
"senderID": that.GOOGLE_SENDER_ID,
"iconColor": "gray",
"icon": "icon_notification",
"forceShow": true
},
"ios": {},
"windows": {}
});
that.push.on('registration',
function (deviceToken) {
log("[PushNotificationManager] Token: " + deviceToken.registrationId, Config.notifications.message.info);
that.manageNotifications();
deferredValue.resolve(deviceToken.registrationId);
},
function () {
deferredValue.reject('[PushNotificationManager] Error al registrar el notificador.');
}, {
senderID: this.GOOGLE_SENDER_ID,
ecb: 'window.onAndroidNotification'
});
setTimeout(function () {
if (deferredValue.state() === 'pending') {
deferredValue.reject('[PushNotificationManager] No se obtuvo respuesta del servidor.');
}
}, 10000);
return deferredValue.promise();
},
registerDeviceIOS: function () {
var that = this,
deferredValue = $.Deferred();
that.push = PushNotification.init({
"android": {},
"ios": {
"alert": "true",
"badge": "true",
"clearBadge": "true",
"sound": "true"},
"windows": {}
});
that.push.on('registration', function (deviceToken) {
log("[PushNotificationManager] Token: " + deviceToken.registrationId, Config.notifications.message.info);
that.manageNotifications();
deferredValue.resolve(deviceToken.registrationId);
}, function (e) {
deferredValue.reject('[PushNotificationManager] Error al registrar el notificador.');
log(e, Config.notifications.message.error);
}, {
'badge': 'false',
'sound': 'true',
'alert': 'true',
'ecb': 'window.onIosNotification'
});
return deferredValue.promise();
},
manageNotifications: function () {
var that = this;
if ( !_.isNull(that.push) && !_.isUndefined(that.push) ) {
that.push.on('notification', function (data) {
/*window.localStorage["cold"] = "true";
window.localStorage["data"] = JSON.stringify(data);*/
console.log(data);
if (data.additionalData.foreground != true){
if(data.additionalData.coldstart == true ) {
} else {
}
}
that.push.finish();
// To make visible item selection
});
}
}
};