Phonegap PushPlugin没有在Android上激活ecb

时间:2014-12-03 06:42:48

标签: android cordova phonegap-pushplugin

我在Android中遇到了PushPlugin的问题。一直在寻找解决方案的时间,但仍然没有成功 当我收到推送通知时,ecb不会被触发 它在注册时成功触发,但当我关闭应用程序并收到通知时,我通过点击通知打开应用程序,它不会被调用。

这是我的代码:

var Utility = {
    registerPushNotification : function() {
        if( typeof device != 'undefined' )
        {
            // checking if device token is exist
            var deviceToken = dbUtil.getValue('deviceToken');
            if(deviceToken==null)
            {
                if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
                    window.plugins.pushNotification.register(
                        function(result) {
                            alert('result = ' + result);
                        },
                        function(error) {
                            alert('error = ' + error);
                        },
                        {
                            "senderID":'my_project_number',
                            "ecb":"Utility.onNotification"
                        }
                    );
                } else if (device.platform == 'iOS'){
                    window.plugins.pushNotification.register(
                    function(result) {
                        // Your iOS push server needs to know the token before it can push to this device
                        // here is where you might want to send it the token for later use.
                        // alert('device token = ' + result);
                        dbUtil.setValue('deviceToken', result);
                        // upload deviceToken to server
                        Utility.registerDevice(result,app.CONSTANTS.TYPE_APNS);
                    },
                    function(error) {
                        // alert('error = ' + error);
                        pageUtil.drawToast('error getting device token');
                    },
                    {
                        "badge":"true",
                        "sound":"true",
                        "alert":"true",
                        "ecb":"Utility.onNotificationAPN"
                    });
                }               
            }
        }
    },
    onNotificationAPN : function (event) {
        if ( event.alert )
        {
            navigator.notification.alert(event.alert);
        }
        if ( event.sound )
        {
            var snd = new Media(event.sound);
            snd.play();
        }
        if ( event.badge )
        {
            // window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
        }
    },
    // Android and Amazon Fire OS
    onNotification : function(e) {
        switch( e.event )
        {
            case 'registered':
            {
                if ( e.regid.length > 0 )
                {
                    // 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.
                    // alert(e.regid);
                    // console.log("regID = " + e.regid);
                    dbUtil.setValue('deviceToken', e.regid);
                    // upload deviceToken to server
                    Utility.registerDevice(e.regid,app.CONSTANTS.TYPE_GCM);
                }
                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 )
                {
                    // on Android soundname is outside the payload.
                    // On Amazon FireOS all custom attributes are contained within payload
                    var soundfile = e.soundname || e.payload.sound;
                    // if the notification contains a soundname, play it.
                    var my_media = new Media("/android_asset/www/"+ soundfile);
                    my_media.play();
                    alert(e.payload.message);
                }
                else
                {
                    pageUtil.loadingFullPage(true,e.payload.message);
                    // 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>');
                    }
                }
                break;
            }
            case 'error':
            {
                alert('error getting google notification');
                break;
            }
            default:
            {
                break;
            }
        }
    },
    .....
};

这是我如何注册,ecb被调用,我可以得到regID

Utility.registerPushNotification();

但永远不会调用e.event =“message”的情况。

请注意:

  

window.plugins.pushNotification.register只被调用一次。我是否必须在每次启动应用时致电register

1 个答案:

答案 0 :(得分:0)

尝试放 您Utility.registerPushNotification()活动中的deviceready