应用程序在前景错误推送插件cordova

时间:2013-12-11 07:51:53

标签: android cordova

我正在使用cordova开发一个Android应用程序,它使用PushPlugin从我的服务器接收推送通知。我的申请表中有几页。我希望实现的是从我的 landing.html 页面重定向到另一个页面,当通知到达时,请说 test.html 。我为此目的尝试了以下代码:

function onNotificationGCM(e) {
                $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
                switch( e.event )
                {
                    case 'registered':
                    if ( e.regid.length > 0 )
                    {
                        console.log("REGISTRATION ON GCM");
                        $("#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':
                        console.log("MESSAGE ON GCM");
                        message = JSON.stringify(e.payload.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)
                        {
                            console.log("APPLICATION IN 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();
                            var redUrl = "test.html?msg="+e.payload.message;
                            console.log("URL FROM REDIRECTION: "+redUrl); 
                            window.location.href = redUrl;
                        }
                        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>');
                        }
                        console.log("MESSAGE: "+JSON.stringify(message));
                        $("#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':
                        console.log("ERROR ON GCM");
                        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
                    break;

                    default:
                        console.log("UNKNOWN ON GCM");
                        $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
                    break;
                }
        }

即使我在设备上收到消息,也不会发生重定向。我检查了日志,这就是我发现的:

"foreground": "false"
那可能是什么问题?

1 个答案:

答案 0 :(得分:0)

如果e.foregroundfalse,您将永远无法访问您尝试执行的代码块。您的应用程序需要在前台才能接收此类通知。