大佬教程收集整理的这篇文章主要介绍了cordova – 如何在恢复申请时处理推送通知?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
onNotificationGCM: function(E) { switch( e.event ) { case 'registered': if ( e.regid.length > 0 ) { console.log("Regid " + e.regid); //alert('registration id = '+e.regid); sdeviceid = e.regid; //alert(sdeviceid); } break; case 'message': // this is the actual push notification. its format depends on the data model from the push server alert('message = '+e.messagE); alert('message = '+e.message+' msgcnt = '+e.msgcnt); if ( e.foreground ) { alert("Notification Received"); } else { // otherwise we were launched because the user touched a notification in the notification tray. if ( e.coldstart ) { alert("coldstart"); } else { alert("other than coldstart"); } } break; case 'error': alert('GCM error = '+e.msg); break; default: alert('An unkNown GCM event has occurred'); break; } }
所以一切都在发挥作用.
>当应用程序在前台时,我收到警报.
>当收到消息时单击通知应用程序
打开,我收到警报.(冷启动)
>当应用程序在后台然后单击时
通知应用程序进入前台,我得到了
警报.
但是当我将应用程序保留到后台并且当我将应用程序带到前面而没有单击通知时推送通知到达时我没有得到警报.那么如何处理这种情况呢?
它现在如何运作
当插件的GCMIntentservice收到消息时,将调用onMessage.此方法获取传递给它的所有其他参数,将它们保存在附加内容中.在此之后,如果应用程序位于前台,此函数只需通过调用sendExtras将额外内容传递给您.否则,它会调用createNotification,它实际上会在状态栏中创建通知.
你的问题
根据我从您的问题中理解的情况,如果在状态栏中收到通知后您打开应用程序而未触及通知,则不会收到警报.
这是发生的事情:
> GCMIntentservice收到一条消息
>由于您的应用程序位于后台,因此PushPlugin会调用createNotification
>当您启动应用程序时,它不会收到警报,因为您尚未触摸状态栏中的通知
如果您触摸了通知,则会收到警报,因为通知意图会唤醒您的应用.当您的应用程序唤醒时,它会查找cached extras,然后再次调用sendExtras将它们传递给您的应用程序.
解决方案
我还没有对此进行过测试,但我坚信如果你编辑你的插件,在调用createNotification之前(或之后)sendExtras,无论你是否触摸通知或刷掉它,都会为你的应用程序缓存数据.
protected void onmessage(Context context,Intent intent) { Log.d(tag,"onmessage - context: " + context); // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { // if we are in the foreground,just surface the payload,else post it to the statusbar if (PushPlugin.isInForeground()) { extras.putBoolean("foreground",truE); PushPlugin.sendExtras(extras); } else { extras.putBoolean("foreground",falsE); // Send a notification if there is a message if (extras.getString("message") != null && extras.getString("message").length() != 0) { createNotification(context,extras); } } } }
将以上部分修改为:
protected void onmessage(Context context,"onmessage - context: " + context); Bundle extras = intent.getExtras(); if (extras != null) { if (PushPlugin.isInForeground()) { extras.putBoolean("foreground",truE); } else { extras.putBoolean("foreground",falsE); if (extras.getString("message") != null && extras.getString("message").length() != 0) { createNotification(context,extras); } } // call sendExtras always PushPlugin.sendExtras(extras); } }
以上是大佬教程为你收集整理的cordova – 如何在恢复申请时处理推送通知?全部内容,希望文章能够帮你解决cordova – 如何在恢复申请时处理推送通知?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。