【问题标题】:Cordova PushNotification onNotificationGCM is not called未调用 Cordova PushNotification onNotificationGCM
【发布时间】:2015-07-10 00:01:34
【问题描述】:

我正在为 Android/IOS 做推送通知。

我使用了一个cordova push-plugin https://github.com/phonegap-build/PushPlugin,它似乎工作得很好。

信息:我在一个 AngularJS 项目中。

在我的 NotificationHelper 工厂中,我有这个初始化方法:

helper.init = function() {

    // Instanciate push plugin notification
    var pushNotification = window.plugins.pushNotification;

    var errorHandler = function(error) {
        logger.debug('errorHandler = ' + error);
    };

    if ($rootScope.isAndroid()) {
        var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID;
        pushNotification.register(function(result) {
            logger.debug('successHandler = ' + result);
        }, errorHandler, {
            'senderID' : senderId,
            'ecb' : 'onNotificationGCM'
        });
    }
};

我还在 mains.js 上定义了这些方法:

var onNotificationGCM = function(event) {
   // call back to web service in Angular.
   var elem = angular.element(document.querySelector('[ng-app]'));
   var injector = elem.injector();
   var service = injector.get('NotificationHelper');
   service.onNotificationGCM(event);
};

从主javascript调用angularJS工厂是一个技巧。

'onNotificationGCM' 调用 'NotificationHelper.onNotificationGCM' 方法:

helper.onNotificationGCM = function(e) {
    switch (e.event) {
    case 'message':
        // Notification happened while app was in the foreground
        if (e.foreground) {
            logger.debug('[notification] [message] Foreground : ' + JSON.stringify(e));
        } else { // Notification touched in the notification tray
            logger.debug('[notification] [message] Background : ' + JSON.stringify(e));
            if (e.coldstart) {
                // App was not running and user clicked on notification
            } else {
                // App was running and user clicked on notification
            }
        }
        decriptPayloadNotification(e.payload);
        break;
    case 'registered':
        logger.debug('[notification] [registered] : ' + JSON.stringify(e));
        if (e.regid.length > 0) {
            registerUser(e.regid, 'gcm');
        }
        break;

    case 'error':
        logger.debug('[notification] [error] : ' + JSON.stringify(e));
        break;

    default:
        logger.debug('[notification] [default] : Unknown, an event was received and we do not know what it is : ' + JSON.stringify(e));
        break;
    }
};

在第一次使用时,一切正常:

  • 收到“已注册”事件
  • 收到通知
  • 当我在前台时,我会收到“消息”事件:

NotificationHelper: [notification] [message] 前台: {"event":"message","from":"847593779913","message":"Agenêts 23/03 10h\r\n","collapse_key": "do_not_collapse","foreground":true,"payload":{"lt":"school","lv":"E1154","notId":"35429","title":"Agenêts le 23/03/ 2015","message":"Agenêts 23/03 10h\r\n"}}

  • 当我在后台时,如果我收到通知并在通知托盘上触摸它,我会收到“消息”事件:

NotificationHelper: [notification] [message] 背景: {"event":"message","from":"847593779913","message":"la piscine sera fermée 1 journée pour raison technology","coldstart": false,"collapse_key":"do_not_collapse","foreground":false,"payload":{"lt":"swimming pool","lv":"E114","notId":"29869","title": "23/04/2015 fermeture de la piscine","message":"la piscine sera fermée 1 journée pour raison technology"}}

但是,如果我杀死我的应用程序,一切都会停止工作。

如果我重新启动应用程序,我将不会再收到“消息”事件并且不会调用“onNotificationGCM”。

我写了一些文章谈论这个问题,但没有成功:

Stackoverflow : Phonegap PushNotification to open a specific app page

有人知道这个问题吗?

【问题讨论】:

    标签: javascript angularjs cordova push-notification phonegap-pushplugin


    【解决方案1】:

    如果您使用 node-gcm 作为推送通知服务器端,我希望这可能对您的问题有所帮助: 检查事项:

    • 从控制台使用 adb logcat 并检查您的设备日志以查看您的设备是否收到任何通知(如果收到,您应该会看到来自 gcmservices 的一些日志)。我相信您应该会看到一些日志,否则当应用处于前台时您可能也不会收到任何通知。
    • 检查您是否在消息数据中添加了“title”和“message”键(这让我很抓狂)
    • 检查delayWhileIdle参数是否为假

    我正在使用的示例消息变量和方法如下:

    var pushAndroidSender = new gcm.Sender('Your GoogleApi Server Key Here');
    var message = new gcm.Message({
        collapseKey: 'demo',
        delayWhileIdle: false,
        timeToLive: 3,
        data: {
            "message":"What's up honey?",
            "title":"Come on"
        }
    });
    pushAndroidSender.send(message, registrationIds, function(err, result) {
        if (err) {
            console.error("this is an error: " + err);
        } else {
            console.log("this is a successful result:" + result);
        }
    });
    

    【讨论】:

    • 我猜只需要messagetitle 是可选的
    • 我照你说的做了。 1) 在 adb logcat 中,我可以看到来自 gcmservices 的一些日志。 2)我的消息数据上有“标题”和“消息”。 3)我将“delayWhileIdle”设置为false。但我有同样的问题:(
    【解决方案2】:

    我问自己为什么它第一次正常工作,而不是接下来的时间......我有这个想法:

    • 可能回调函数是第一次订阅插件,但不是下一次。

    这正是我的问题的根源。我只调用一次“pushNotification.register”来初始化插件并获取ID ... 但是下次我启动应用程序时,我不会再次实例化插件,所以pushplugin不知道在回调请求中调用哪个方法。

    所以答案是:在每次应用启动时调用“pushNotification.register”!

    在我的代码中,我必须在每个“设备就绪”事件上调用下面的方法:

    helper.init = function() {
    
    // Instanciate push plugin notification
    var pushNotification = window.plugins.pushNotification;
    
    var errorHandler = function(error) {
        logger.debug('errorHandler = ' + error);
    };
    
    if ($rootScope.isAndroid()) {
        var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID;
        pushNotification.register(function(result) {
            logger.debug('successHandler = ' + result);
        }, errorHandler, {
            'senderID' : senderId,
            'ecb' : 'onNotificationGCM'
        });
    }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多