简体   繁体   中英

push notification not received in background phonegap android

I am using data from this plugin for push notification cordova plugin add com.phonegap.plugins.pushplugin

my cordova version 5.4.1

I have installed android version of 5.0.0

I have installed below plugin with version of

 device 1.1.1
 file 4.1.0
 media 2.1.0
 phonegap-plugin-push 1.5.3

But my app working fine in Foreground only, its not fired when app is closed or background mode

My deviceReadyFunction is as below

var pushNotification;

document.addEventListener('deviceready', onDeviceReady, true);

function onDeviceReady() 
{
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android') {
    $("#app-status-ul").append('<li>registering android</li>');
    pushNotification.register(successHandler, errorHandler, {"senderID":"114019398228","ecb":"onNotificationGCM"});     // required!
} else {
    $("#app-status-ul").append('<li>registering iOS</li>');
    pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});    // required!
}
}

function onNotificationGCM(e) 
{
$("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
switch (e.event) {
    case 'registered':
        if (e.regid.length > 0) 
        {
            $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
        }
    break;

    case 'message':

    $("#app-status-ul").append('<li> Fore Ground ::' + JSON.stringify(e.foreground) + "</li>");
    if (e.foreground)
    {
        $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
        // if the notification contains a soundname, play it.
        var my_media = new Media("beep.wav");
        my_media.play();

    }
    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>');
    }

    $("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.Notice + '</li>');

    break;

    case 'error':
        $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
    break;

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

I think the problem is that you are using the deprecated version of the plugin. Instead of

cordova plugin add com.phonegap.plugins.pushplugin

Use

cordova plugin add phonegap-plugin-push

And base your code in this documentation:

https://github.com/phonegap/phonegap-plugin-push

I left you some code that works:

        var PushNotificationManager = {

        GOOGLE_SENDER_ID: Config.notifications.SENDER_ID,

        push: null,

        registerDeviceAndroid: function () {

            var that = this,
                deferredValue = $.Deferred();

            that.push = PushNotification.init({ 
                    "android": {
                        "senderID": that.GOOGLE_SENDER_ID,
                        "iconColor": "gray",
                        "icon": "icon_notification",
                        "forceShow": true
                    },
                    "ios": {}, 
                    "windows": {} 
            });

            that.push.on('registration',

                function (deviceToken) {

                    log("[PushNotificationManager] Token: " + deviceToken.registrationId, Config.notifications.message.info);

                    that.manageNotifications();

                    deferredValue.resolve(deviceToken.registrationId);

                },
                function () {
                    deferredValue.reject('[PushNotificationManager] Error al registrar el notificador.');
                }, {
                    senderID: this.GOOGLE_SENDER_ID,
                    ecb: 'window.onAndroidNotification'
            });

            setTimeout(function () {

                if (deferredValue.state() === 'pending') {
                    deferredValue.reject('[PushNotificationManager] No se obtuvo respuesta del servidor.');
                }
            }, 10000);

            return deferredValue.promise();
        },

        registerDeviceIOS: function () {

            var that = this,
                deferredValue = $.Deferred();

            that.push = PushNotification.init({ 
                    "android": {},
                    "ios": {
                        "alert": "true",
                        "badge": "true",
                        "clearBadge": "true",
                        "sound": "true"},
                    "windows": {} 
            });

            that.push.on('registration', function (deviceToken) {

                log("[PushNotificationManager] Token: " + deviceToken.registrationId, Config.notifications.message.info);

                that.manageNotifications();

                deferredValue.resolve(deviceToken.registrationId);

            }, function (e) {

                        deferredValue.reject('[PushNotificationManager] Error al registrar el notificador.');
                        log(e, Config.notifications.message.error);
            }, {
                        'badge': 'false',
                        'sound': 'true',
                        'alert': 'true',
                        'ecb': 'window.onIosNotification'
            });

            return deferredValue.promise();
        },

        manageNotifications: function () {

            var that = this;

            if ( !_.isNull(that.push) && !_.isUndefined(that.push) ) {

                that.push.on('notification', function (data) {
                    /*window.localStorage["cold"] = "true";
                    window.localStorage["data"] = JSON.stringify(data);*/
                    console.log(data);
                    if (data.additionalData.foreground != true){


                        if(data.additionalData.coldstart == true ) {



                        } else {


                        }
                    }

                    that.push.finish();

                    // To make visible item selection

                });

            }
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM