我也有問題,你沒有註冊的消息。刪除包含html的代碼行。如果您想檢索它,請將其存儲到sessionStorage/localStorage,控制檯或提醒它。
我的HTML版本刪除
var pushNotification;
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
console.log("registering android");
window.plugins.pushNotification.register(successHandler, errorHandler, {
"senderID": "xxxxxxxxxxx",
"ecb": "onNotificationGCM"
}); // required!
} else {
console.log("registering iOS");
pushNotification.register(tokenHandler, errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
}); // required!
}
}
// handle APNS notifications for iOS
function onNotificationAPN(e) {
if (e.alert) {
navigator.notification.alert(e.alert);
}
if (e.sound) {
var snd = new Media(e.sound);
snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
}
}
// handle GCM notifications for Android
function onNotificationGCM(e) {
navigator.notification.alert(e.event);
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
navigator.notification.alert(e.regid);
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
console.log("regID = " + e.regid);
sessionStorage.setItem("deviceId",e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground) {
navigator.notification.alert('--INLINE NOTIFICATION--');
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/" + e.soundname);
my_media.play();
} else { // otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart) navigator.notification.alert('--COLDSTART NOTIFICATION--');
else navigator.notification.alert('--BACKGROUND NOTIFICATION--');
}
navigator.notification.alert(e.payload.message);
navigator.notification.alert('MESSAGE -> MSGCNT: ' + e.payload.msgcnt);
break;
case 'error':
navigator.notification.alert('ERROR -> MSG:' + e.msg);
break;
default:
navigator.notification.alert('EVENT -> Unknown, an event was received and we do not know what it is');
break;
}
}
function tokenHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "APNS");
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
}
function successHandler(result) {
navigator.notification.alert(result, null, 'Alert', 'OK');
sessionStorage.setItem("deviceId", result);
sessionStorage.setItem("notificationServer", "GCM");
}
function errorHandler(error) {
navigator.notification.alert(error, null, 'Alert', 'OK');
}
我已經使用你的代碼。 onNotificationGCM註冊從未被調用過。 – Fxster
確保您首先將對話框和控制檯phongap插件安裝到您的項目中。 –
這些都是我安裝的插件:E:\ androidworkspace \ PG>的PhoneGap本地插件列表 [PhoneGap的] com.phonegap.plugins.PushPlugin [PhoneGap的] org.apache.cordova.console [PhoneGap的] org.apache.cordova設備 [phonegap] org.apache.cordova.dialogs [phonegap] org.apache.cordova.splashscreen [phonegap] org.apache.cordova。振動 – Fxster