【问题标题】:Unable to handle event in background push notification无法处理后台推送通知中的事件
【发布时间】:2017-06-15 09:12:22
【问题描述】:

我正在使用cordova插件添加phonegap-plugin-push插件来推送通知 在前台通知工作正常。我也可以处理事件。 当我的应用程序在后台时,我也会收到通知,但是单击推送通知时,我的事件不会触发。 我正在使用以下代码

$cordovaPushV5.initialize(options).then(function() {
                // start listening for new notifications
                $cordovaPushV5.onNotification();
                // start listening for errors
                $cordovaPushV5.onError();

                // register to get registrationId
                if (PNdeviceToken == null) //becuase registration will be done only the very first 
                {
                    $cordovaPushV5.register().then(function(registrationId) {
                        // save `registrationId` somewhere;
                        window.localStorage.setItem('PNdeviceToken', registrationId);
                        $rootScope.fcmToken = registrationId;
                        console.log(registrationId)
                        alert("first time registered id -- " + registrationId)
                    })

                } else {
                    $rootScope.fcmToken = PNdeviceToken;
                    alert("already saved registered id -- " + $rootScope.fcmToken)
                }

            });


$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data) {
                console.log(event)
                console.log(data)
                })

当我点击后台推送通知时 $cordovaPushV5:notificationReceived 事件未触发,我该如何解决这个问题?

如何处理后台推送通知事件?

【问题讨论】:

    标签: android cordova ionic-framework


    【解决方案1】:

    我遇到了同样的问题,经过 2 天的研究后得到了解决。

    无论应用程序是在前台还是后台,处理通知事件相同

    我们必须在数据字段中设置 "content-available" : "1"在推送通知时。否则,如果应用在后台,它不会调用notificationReceived 事件。

    另请注意,目前通过Google Firebase Console 是不可能的。

    我们必须使用firebase servers 中的任何一个单独发送我们的custom payload messages(数据或通知或两者)。

    详细信息可以在插件的GitHub Docs Page on background notifications 上找到。 从那里引用-

    在 Android 上,如果您希望在应用处于后台时调用 on('notification') 事件处理程序*,这相对简单。

    首先,您从 GCM 发送的 JSON 需要包含“content-available”:“1”。这将告诉推送插件调用您的 on('notification') 事件处理程序*,无论推送通知中有什么其他数据。

    *on('notification') 事件处理程序 = $cordovaPushV5:notificationReceived 事件在您的情况下。

    有关使用 PHPNodeJS

    发送自定义负载消息的信息,请参阅 this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 2017-11-27
      • 2019-11-10
      • 2017-07-30
      相关资源
      最近更新 更多