应用程序恢复时如何处理推送通知? [英] How to handle Push notification when application is resumed?

查看:201
本文介绍了应用程序恢复时如何处理推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 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':
//这是实际的推送通知。它的格式取决于来自推送服务器的数据模型
alert('message ='+ e.message);
alert('message ='+ e.message +'msgcnt ='+ e.msgcnt);
if(e.foreground)
{
alert(Notification Received);

}
else
{//否则我们启动是因为用户触摸了通知托盘中的通知。
if(e.coldstart)
{
alert(coldstart);
}
else
{
alert(other than coldstart);
}
}
break;

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

默认值:
alert('发生未知的GCM事件');
break;
}
}

一切正常。


  1. 当应用程序处于前台时,我收到警报。

  2. 当我收到消息时单击通知,应用程序打开,

  3. 当应用程序在后台,点击通知时,应用程序进入前台,并收到警报。

但如果应用程序处于后台并且收到通知,当我把应用程序带到前面而没有点击通知,我没有得到警报。那么如何处理这种情况呢?

解决方案

我面对同样的问题。简单地说, PushPlugin 现在不支持此功能。



p>当插件的 GCMIntentService 接收到消息时, onMessage 被调用。此方法获取传递给它的所有其他参数,将它们保存在 extras 中。之后,如果应用程序处于前台,此函数只需通过调用 sendExtras extras 传递给您。否则,它会调用 createNotification ,这实际上会在状态栏中创建通知。



strong>



根据我从您的问题中了解到的内容,如果在状态栏中收到通知后,您在打开应用程序时未触及通知,则不会收到提醒



这是会发生什么:


  1. GCMIntentService 收到一条消息

  2. 由于您的应用程序位于后台,PushPlugin会调用 createNotification

  3. 启动应用程式时,并未收到通知,因为您尚未触摸状态列中的通知

如果您触摸了通知,您会收到提醒,因为通知意图会唤醒您的应用程序。当您的应用程式唤醒时,它会查找缓存附加组件,然后再次通过调用 sendExtras 将它们传递给您的应用程序。



strong>解决方案



我还没有测试过,但我坚信如果你编辑你的插件, sendExtras 之前(或之后)调用 createNotification ,数据将被缓存为您的应用程序,无论是否触摸通知或滑动它。 p>

  protected void onMessage(Context context,Intent intent){
Log.d(TAG,onMessage - context:上下文);
//从消息中提取有效负载
Bundle extras = intent.getExtras();
if(extras!= null)
{
//如果我们在前台,只是表面的有效负载,否则发布到状态栏
if(PushPlugin.isInForeground )){
extras.putBoolean(foreground,true);
PushPlugin.sendExtras(extras);
}
else {
extras.putBoolean(foreground,false);
//如果有消息,发送通知
if(extras.getString(message)!= null&& extras.getString(message)length()!= 0 ){
createNotification(context,extras);
}
}
}
}

修改上面的部分:

  protected void onMessage(Context context,Intent intent){
Log.d 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);
}
}
//调用sendExtras总是
PushPlugin.sendExtras(extras);
}
}


Trying to handle push Notification using PushPlugin. Following is my code:

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

So everything is working

  1. When application is in foreground I am getting the alert.
  2. When I click the notification when message is received, application opens and I am getting the alert.(coldstart)
  3. When application is in the background and I click on notification the application comes to foreground and I am getting the alert.

But if application is in background and a notification arrives; when I bring the application to the front without clicking on notification, I get no alert. So how to handle this type of situation?

解决方案

I have faced the same issue. Simply put, PushPlugin does not support this feature now. But you can very easily modify it to suit your needs

How it works now

When a message is received by the plugin's GCMIntentService, onMessage is called. This method fetches all the additional parameters passed to it, saving them in extras. After this, if the app is in foreground, this function simply passes extras to you by calling sendExtras. Otherwise it calls createNotification, which actually creates the notification in the status bar.

Your problem

From what I have understood from your question, the alert is not received if, after getting a notification in status bar, you open your app without touching on the notification.

This is what happens:

  1. GCMIntentService receives a message
  2. Since your app is in background, PushPlugin calls createNotification
  3. When you start your app, it gets no alert, because you have not yet touched the notification in the status bar

You would have received the alert if you had touched the notification because then the notification intent would wake up your app. When your app wakes up it looks for cached extras and then passes them to your app by again calling sendExtras.

The solution

I have not tested this yet, but I strongly believe that if you edit your plugin, to sendExtras before (or after) you call createNotification, the data will be cached for your application regardless of whether you touch the notification or swipe it away.

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

Modify the above section to:

protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "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);
    }
}

这篇关于应用程序恢复时如何处理推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆