cordova – 如何在恢复申请时处理推送通知?

cordova – 如何在恢复申请时处理推送通知?,第1张

概述尝试使用 PushPlugin处理推送通知. 以下是我的代码. onNotificationGCM: function(e) { switch( e.event ) { case 'registered': if ( e.regid.length > 0 ) { console.log( 尝试使用 PushPlugin处理推送通知.
以下是我的代码.

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;    }}

所以一切都在发挥作用.

>当应用程序在前台时,我收到警报.
>当收到消息时单击通知应用程序
   打开,我收到警报.(冷启动)
>当应用程序在后台然后单击时
通知应用程序进入前台,我得到了
警报.

但是当我将应用程序保留到后台并且当我将应用程序带到前面而没有单击通知时推送通知到达时我没有得到警报.那么如何处理这种情况呢?

解决方法 我遇到了同样的问题.简而言之,PushPlugin现在不支持此功能.但您可以非常轻松地修改它以满足您的需求

它现在如何运作

当插件的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 – 如何在恢复申请时处理推送通知?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1000758.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存