推送插件通知

时间:2015-02-17 15:06:07

标签: android push-notification google-cloud-messaging phonegap-pushplugin

为什么我的应用程序运行或打开时无法收到推送通知?我使用的是push插件。 这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

推送通知无法正常工作的原因有很多。确保您的注册ID,令牌,服务器等有效且有效。阅读以下doc,确保首先满足您的所有要求。希望这能为您提供一个抓住错误的起点。

答案 1 :(得分:0)

如果您的申请已关闭或已开启,

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

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