1

我在 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每个应用程序启动吗?

4

1 回答 1

0

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

于 2015-02-14T04:37:50.557 回答