gpt4 book ai didi

android - 在 Android Phonegap 应用程序中处理推送通知消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:20 25 4
gpt4 key购买 nike

我正在 Phonegap Android 应用程序中实现推送通知。我正在学习教程 here .在本教程中,onDeviceready 函数如下所示:

onDeviceReady: function() {
app.receivedEvent('deviceready');
var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"836557454855","ecb":"app.onNotificationGCM"});
},

这意味着每次应用程序启动时,都会向 Google 注册推送通知。我认为这只需要完成一次。所以在我的,我有:

onDeviceReady: function() {

var device_id = window.localStorage.getItem('mountmercy_device_id');

//if device_id is in local storage, then it means registration
// with google has already taken place. If not, then register
if(typeof(device_id)==='undefined' || device_id===null){

var pushNotification = window.plugins.pushNotification;
if (window.device.platform == 'android' || window.device.platform == 'Android') {
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"475226855592","ecb":"app.onNotificationGCM"});
}
else{
//so its apple
pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
}

}

}

然后在 onNotificationGCM 中,我设置了本地存储,这样设备就不会再次注册:

onNotificationGCM: function(e) {

switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
/*
save reg id to server and store response in local storage
...
...
...
*/

window.localStorage.setItem('mountmercy_device_id', data.id);
window.localStorage.setItem('mountmercy_api_key', data.get('api_key'));

}
break;

case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = '+e.message+' msgcnt = '+e.msgcnt);

break;

case 'error':
//alert('GCM error = '+e.msg);
break;

default:
// alert('An unknown GCM event has occurred');
break;
}
},

当手机收到新的推送通知时出现问题。在教程的原始项目中,当用户单击通知消息时,应用程序打开并且用户看到警告:“message = blachblah msgcnt = blahblah”。这是因为执行了onNotificationGCM()中"message"情况下的代码。

在我的应用程序中,应用程序打开但“消息”案例中的代码未执行。这是因为,在 onDeviceReady() 中,我只向 Google 注册了一次设备。如果我删除条件:

if(typeof(device_id)==='undefined' || device_id===null){

并且每次注册设备,执行“消息”案例。但是每次都必须在onDeviceReady()中注册设备这似乎是错误的。还有其他解决方案吗?

最佳答案

我在 Google Cloud Messaging 的文档中看到它说

Google may periodically refresh the registration ID

在这种情况下,我会在每次打开应用程序时向 Google 注册,并更新返回的注册 ID,以防它发生变化。这也解决了在通知栏中单击时处理推送消息的问题。

关于android - 在 Android Phonegap 应用程序中处理推送通知消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21138105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com