我已经使用Phonegap 3.4安装了PushPlugin。
onNotificationGCM案例:注册从未执行,我无法将regid存储在我的服务器数据库中并开始发送通知。
每当我的应用程序打开时,它都会显示
我做了:
我需要:
我等了几分钟,但没有像
这样的注册消息$("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
答案 0 :(得分:1)
我也有问题。删除包含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');
}
答案 1 :(得分:0)
解决。经过这么多天的搜索,pushplugin或phonegap都没有错。这一轮,它就是手机本身。许多人在Android 4.2.2尤其是s4上面临推送通知问题。它也不适用于我的Android 4.2.2平板电脑。我不确定whatsapp和facebook是如何做到的,只要它们是真正的本机应用程序,所以我相信它们可以更好地控制调试。
我是如何解决的...首先我重置了工厂并安装了相同的应用程序..它可以在没有任何代码更改的情况下工作!继续前进,因为我担心4.2.2的问题会回来,我已经升级到固件4.3并且现在工作正常。