【问题标题】:When click status bar Notification (Phonegap PushPlugin) The application is launched but does not detect coldstart state单击状态栏时通知(Phonegap Push Plugin)应用程序启动但未检测到冷启动状态
【发布时间】:2014-03-20 01:00:49
【问题描述】:

我正在 Eclipse 中开发一个 android 应用程序,并且我已经成功安装了 Phonegap PushPlugin,一切正常,在状态栏中接收并启动通知,并且在触摸时打开我的应用程序。

问题出现在我的应用程序关闭(即冷态)时,应用程序已成功启动,但没有运行“if (e.coldstart)”中的操作

我根据插件官方文档中发布的以下示例编写了我的代码:

function onNotificationGCM(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
    switch (e.event) {
    case 'registered':
        if (e.regid.length > 0) {
            $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
            // Your GCM push server needs to know the regID before it can push to this device
            // here is where you might want to send it the regID for later use.
            console.log("regID = " + e.regid);
        }
        break;
    case 'message':
        // if this flag is set, this notification happened while we were in the foreground.
        // you might want to play a sound to get the user's attention, throw up a dialog, etc.
        if (e.foreground) {
            $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
            // if the notification contains a soundname, play it.
            var my_media = new Media("/android_asset/www/" + e.soundname);
            my_media.play();
        } else { // otherwise we were launched because the user touched a notification in the notification tray.
            if (e.coldstart) {
                $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
            } else {
                $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
            }
        }
        $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
        $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
        break;
    case 'error':
        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
        break;
    default:
        $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
        break;
    }
}

此代码位于我的应用程序的主页上,当您点击通知并成功显示标签“INLINE NOTIFICATION”和“BACKGROUND NOTIFICATION”(以适用者为准)时打开

不应该这样放在

if (e.coldstart) {
    $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
}

如果应用来自被关闭的情况下应该执行吗?

非常感谢您的合作,因为当通知到达时,我的应用程序应该打开一个与 index.html 不同的页面,并且该页面保留在 index.html 中

在这里我将编辑我的问题,在original plugin documentation 中放置一个文本片段,引用变量“coldstart”:

最后,您是否应该通过点击后退按钮完全退出应用程序 从主页,您可能仍会收到通知。接触 通知托盘中的该通知将重新启动您的应用程序并 允许您处理通知 (COLDSTART)。在这种情况下 将在传入事件上设置冷启动标志。

另外添加我自己的代码,在收到推送通知时打开一个新页面:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent();
    },
    // Update DOM on a Received Event
    receivedEvent: function() {
        var pushNotification = window.plugins.pushNotification;
        pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"My sender ID","ecb":"app.onNotificationGCM"});        
    },
 // result contains any message sent from the plugin call
    successHandler: function(result) {
       alert('Callback Success! Result = '+result)
    },
    errorHandler:function(error) {
        alert(error);
    },
    onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                    window.localStorage.setItem("gcmid", e.regid);
                }
            break; 
    case 'message':
        // if this flag is set, this notification happened while we were in the foreground.
        // you might want to play a sound to get the user's attention, throw up a dialog, etc.
        if ( e.foreground )
        {
    alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r);         
    top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message;
        }
        else
        {  // otherwise we were launched because the user touched a notification in the notification tray.
            if ( e.coldstart )
            {
    alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r);         
    top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message;
            }
            else
            {
     alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r);         
    top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message;
            }
        }
    break; 
            case 'error':
              alert('GCM error = '+e.msg);
            break; 
            default:
              alert('An unknown GCM event has occurred');
              break;
        }
    }
};

暂时解决我的问题,添加一个额外的有效负载,指示我的活动应该打开一个与 index.html 不同的页面,尽管该解决方案不会将“e.coldstart”作为“true”传递,但我将其添加到函数中在 PushHandlerAtivity.java 中:

之后:

private void forceMainActivityReload()
{
    PackageManager pm = getPackageManager();
    Intent launchIntent =    pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());  
    startActivity(launchIntent);
}

之前:

private void forceMainActivityReload()
{
    PackageManager pm = getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());       

    Bundle extras = getIntent().getExtras();
    launchIntent.putExtra("room_msg", extras.getString("room_msg"));

    startActivity(launchIntent);
}

当然在生成通知的函数中使用 addDataWithKeyValue 函数添加额外的有效负载,然后我的主要活动添加以下内容:

Bundle extras = getIntent().getExtras();
            String message = extras.getString("room_msg");
             if(message != null){
                    super.loadUrl("file:///android_asset/www/chat.html?", 10000);         
                }else{
                    super.loadUrl("file:///android_asset/www/index.html", 10000);
                }

【问题讨论】:

  • 当应用程序在后台时,JS 不工作(停止)。这样它就不会执行
  • 当后台运行完美@HanhLe,问题是当完全关闭时,我假设pushplugin启动应用程序然后运行JS函数“onNotificationGCM(e)”
  • 哦,当您的应用程序完全关闭时,如果您点击启动应用程序的通知,e.coldstart 将无法工作。因为,您的应用程序关闭,这意味着所有资源都将无法正常工作(除了服务背景 Android,它会收到消息和通知)。
  • 好的@HanhLe,然后出现另一个问题,适用于“e.coldstart”?
  • "e.coldstart" 表示你的应用启动和推送插件未创建,你能发布你的代码,我会找到准确的点

标签: android cordova notifications push-notification


【解决方案1】:

我的英语不好。为了确保 PushPlugin 正常工作,您应该有一个 Java 测试用例,以确保插件运行正确或您有错误。当你在通知中触摸时,PushHandlerActivity 将被调用,它调用函数:

private void processPushBundle(boolean isPushPluginActive)
{
    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        Bundle originalExtras = extras.getBundle("pushBundle");

        originalExtras.putBoolean("foreground", false);
        originalExtras.putBoolean("coldstart", !isPushPluginActive);

        PushPlugin.sendExtras(originalExtras);
    }
}

在后台识别您的应用程序或应用程序已关闭(以防它触发冷启动-->true)


编辑 1: 当您的应用关闭(不在后台)并且您点击通知以启动应用时,coldstart 为真。当您的应用程序在后台时,冷启动仍然是错误的。您可以通过在上述函数中更改变量 'isPushPluginActive'=true 来破解代码。

【讨论】:

  • 对不起我的英语,我来自委内瑞拉加拉加斯并尝试使用谷歌翻译,事实上我已经完成了这些测试,因为你可以首先看到通知工作完美,这些显示在状态栏中,触摸时打开我的应用程序,然后根据情况显示标签 INLINE NOTIFICATION(当应用程序处于前台时)或 BACKGROUND NOTIFICATION(当应用程序处于后台时),但是当应用程序关闭时(触摸后退按钮)仅打开应用程序但变量“coldstart = true”未传递,因此不会触发条件(如果冷启动)
  • 你发布的这个功能(顺便说一下我花了三天时间分析)只在应用程序在后台而不是在冷启动时有效。
  • 先生,我的英语不好。好的,我明天会给你一个测试用例。我很忙,所以我现在没有答案
  • 哦,抱歉,我明白了,非常感谢您抽出宝贵时间。
  • 您好 RaulObagi 和 @HanhLe,您找到有关此问题的答案了吗?我也有这个问题。谢谢。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2015-04-05
    • 2012-01-18
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多