我一直试图让Android推送通知在我的应用程序上运行一段时间(iOS已经完成),除了只是通知实际显示在Android设备上之外,还要整理所有内容。
注册设备ID,并推送到GCM服务器似乎都工作正常,但当我测试GCM回复中的消息返回时,我一直收到undefined
。
将消息推送到GCM时的所有响应都是成功的,正确的设备ID,与之关联的消息ID等等。任何人都能指出正确的方向吗?下面您将看到代码片段,其中只有一个示例“警告”用于显示将要返回的内容,最终将用作“推送”中的通知。
此提醒
alert('message = ' + e.message + ' payload message: ' + e.payload.message +
' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
似乎没有得到任何回报来展示推动。
function onDeviceReady() {
console.log('deviceready');
try {
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos') {
console.log('PN register');
pushNotification.register(successHandler, errorHandler, {
"senderID": "177718756870",
"ecb": "onNotification"
}); // required!
console.log('after PN register');
} else {
console.log('PN register');
pushNotification.register(tokenHandler, errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
}); // required!
console.log('after PN register');
}
}
catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
console.log("ERROR", txt);
}
}
var pushNotification;
// handle GCM notifications for Android
window.onNotification = function(e) {
console.log('EVENT RECEIVED ' + e.event)
console.log("regID BEFORE CHECKS = " + e.regid);
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0)
{
console.log("regID = " + e.regid);
var data =
{
'device_id': e.regid,
'platform': device.platform,
'os_version': device.version,
'app_version': lawnmowerConfig.versionString,
'device_model': device.model
};
localStorage.setItem('push_data', JSON.stringify(data));
}
break;
case 'message':
console.log('Inside case message: ' + e.regid)
if (e.foreground)
{
// Add something to play a sound once working
}
else
{
if (e.coldstart) {
console.log("coldstart");
}
else {
console.log("not coldstart");
}
}
alert('message = ' + e.message + ' payload message: ' + e.payload.message + ' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
break;
case 'error':
alert('GCM error = ' + e.msg);
break;
default:
// Testing using these alerts instead
alert('An unknown GCM event has occurred');
break;
}
};
function tokenHandler (result) {
console.log('push token handler');
console.log(result);
var data =
{
'device_id': result,
'platform': device.platform,
'os_version': device.version,
'app_version': lawnmowerConfig.versionString,
'device_model': device.model
};
localStorage.setItem('push_data', JSON.stringify(data));
}
function successHandler (result) {
console.log('success handler push success');
console.log("result: " + result);
}
function errorHandler (error) {
console.log('push error');
}
document.addEventListener('deviceready', onDeviceReady, true);
答案 0 :(得分:0)
注册设备ID并推送到GCM服务器似乎一切正常,但是当我测试GCM回复中的消息返回时,我一直得到
undefined
。< / p>
这意味着当你正在测试&#34;消息....是什么&#34;您正在引用尚未定义的变量。在这一行:
alert('message = ' + e.message + ' payload message: ' + e.payload.message +
' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
没有变量e.message
。您从服务器发送的数据附加到e.payload
,e.event
的值设置为message
。我认为如果您删除e.message
,您的问题就可以解决了。类似的东西:
alert('event = ' + e.event + ' payload message: ' + e.payload.message +
' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);
注意要检测变量问题(范围和/或声明),请一次调试一个变量。这将帮助您精确识别问题并将其跟踪到其原点。
我建议你使用
alert("Payload message: " + e.payload.message);