当点击状态栏通知(Phonegap PushPlugin)应用程序启动,但不检测冷启动状态



我正在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;
    }
}

这段代码是在我的应用程序的主页上,当你点击通知并成功显示标签"内联通知"one_answers"背景通知"时打开,无论哪个适用

不应该放在

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

应该执行,如果应用程序来自被关闭?

非常感谢您的合作,因为当通知到达时,我的应用程序应该打开一个与index.html不同的页面,这个页面位于index.html

中。

在这里,我将编辑我的问题,在原始插件文档中放置一个文本片段,指向变量"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",我将其添加到pushhandleractivity .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);
                }

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

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

识别你的应用程序在后台或应用程序已关闭(如果它触发coldstart->true)


编辑1: coldstart true当你的应用程序关闭(不是在后台),你点击进入通知启动应用程序。当你的应用程序在后台然后coldstart仍然为假。你可以在上面的函数中修改变量'isPushPluginActive'=true来破解代码

也许这可以解决你的问题

https://github.com/phonegap-build/PushPlugin/issues/179

最新更新