1

当我的应用程序已经运行或打开时,为什么我收不到推送通知?我正在使用推送插件。这是我的代码:

<script>
    var tokenID = "";
    document.addEventListener("deviceready", function(){
        //Unregister the previous token because it might have become invalid. Unregister everytime app is started.
//            window.plugins.pushNotification.unregister(successHandler, errorHandler);
        if(intel.xdk.device.platform == "Android")
        {
            //register the user and get token
            window.plugins.pushNotification.register(
            successHandler,
            errorHandler,
            {
                //project ID from Google Cloud Messaging
                "senderID":"749289157135",
                //callback function that is executed when phone recieves a notification for this app
                "ecb":"onNotification"
            });
        } 
    }, false);
    //app given permission to receive and display push messages in Android.
    function successHandler (result) {
        // alert('result = ' + result);
    }

    //app denied permission to receive and display push messages in Android.
    function errorHandler (error) {
        alert('error = ' + error);
    }
    //fired when token is generated, message is received or an error occured.
    function onNotification(e) 
    {
        switch( e.event )
        {
            //app is registered to receive notification
            case 'registered':
                if(e.regid.length > 0)
                {
                    // Your Android push server needs to know the token before it can push to this device
        // here is where you might want to send the token to your server along with user credentials.
                    // if(localStorage.getItem('deviceID') != "" || localStorage.getItem("deviceID") != NULL || localStorage.getItem("puID") !="" || localStorage.getItem("puID") != NULL){
                    //     window.location = "emergencyCode.html";
                    // }
                    // else{
                        tokenID = e.regid;
                        localStorage.setItem("deviceID", tokenID);
                        window.location = "start.html"; 

                }
            break;
            case 'message':
                if ( e.foreground ) {
                   alert("foreground");
                } else {
                    if ( e.coldstart )  {
                        // window.location = "login.html";
                        alert('name = ' + e.payload.name);
                        alert('lat = ' + e.payload.lat);
                        alert('lng = ' + e.payload.lng);

                    } else {
                        alert("BACKGROUND NOTIFICATION");
                    }
                }
            break;
            case 'error':
              alert('GCM error = '+e.msg);
            break;
            default:
              alert('An unknown GCM event has occurred');
            break;
        }
    }
</script>
4

2 回答 2

0

如果您的应用程序关闭或打开, PushPlugin会收到通知。如果您的应用程序已关闭,插件会在通知区域中创建通知。但是如果您的应用程序是打开的,它会将通知数据传递给您的应用程序,而无需创建通知

因此,您将始终在配置中获得从服务器发送的通知数据,ecb但不会始终创建通知

于 2015-04-28T03:03:25.587 回答
0

推送通知不起作用的原因可能有很多。确保您的注册 ID、令牌、服务器等有效且有效。通过阅读以下文档,确保首先满足您的所有要求。希望这为您提供了一个发现错误的起点。

于 2015-02-17T18:23:18.187 回答