【问题标题】:push notification not received in background phonegap android在后台phonegap android中未收到推送通知
【发布时间】:2016-02-09 07:34:01
【问题描述】:

我正在使用此插件中的数据进行推送通知 cordova plugin add com.phonegap.plugins.pushplugin

my cordova version 5.4.1

我已经安装了安卓版本5.0.0

我已经安装了以下版本的插件

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

但我的应用仅在前台运行良好,在应用关闭或后台模式时不会触发

我的 deviceReadyFunction 如下

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

【问题讨论】:

  • 我找到了这个答案stackoverflow.com/a/20969606/3840093,但是在构建找不到符号notif.setLatestEventInfo的应用程序时它给了我一个错误
  • 我有相同的版本,它工作正常。你有什么问题?通知没有到达?活动无效?
  • 是的,我无法在通知托盘中收到通知,如果有任何事件侦听器或处理程序可以在应用程序处于后台模式时处理推送通知,请建议我。
  • 当应用程序在前台时,push.on 通知是否有效?在后台?你能展示你的初始化函数吗?
  • 抱歉回复晚了,我已经更新了我的问题,请看一下。

标签: android cordova push-notification


【解决方案1】:

我认为问题在于您使用的是已弃用的插件版本。而不是

cordova plugin add com.phonegap.plugins.pushplugin

使用

cordova plugin add phonegap-plugin-push

并将您的代码基于此文档:

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

我给你留下了一些有效的代码:

        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

                });

            }
        }
    };

【讨论】:

  • 嘿@Del,谢谢你,但我需要先在Android上实现这个,在androidonAndroidNotification上注册后的功能在这里丢失了。你能建议我为此做些什么吗?
  • 注册后,此代码启动that.manageNotifications();,他负责处理通知,android 和 IOS。你指的是这个吗?
  • 现在出现 Config is not defined 错误,我很抱歉,但是从过去的 2 天一夜开始一直在处理这个问题,所以对此感到非常困惑和厌倦
  • Config 是我之前定义的存储数据的文件。只需将 Config.notifications.SENDER_ID 替换为您的 google 发件人 ID,等等,此代码仅供参考,您必须针对您的代码进行调整
  • 你好@Del,现在我遇到了这个问题stackoverflow.com/questions/34065875/…,正如你之前所说的那样
猜你喜欢
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 2013-01-14
相关资源
最近更新 更多