【问题标题】:phonegap-plugin-push on("notification") event is not firing when app is in background当应用程序处于后台时,phonegap-plugin-push on("notification") 事件不会触发
【发布时间】:2018-02-20 21:56:07
【问题描述】:

我在 Ionic2 中使用以下插件进行推送通知

http://ionicframework.com/docs/native/push/

预期行为: 当应用关闭时,收到通知,当用户点击通知时,应用打开后应该触发 on("notification") 事件。

实际行为: 我正在成功收到通知。但是当应用程序在后台或关闭时,当我收到通知并点击通知时,on("notification") 事件不会触发。

Cordova 版本 7.0.1 安卓版本 6.2.3

我的代码:

this.platform.ready().then(() => {
    this.pushsetup();
});

private pushOptions: PushOptions;
private pushObject: PushObject;
pushsetup() {
    // to check if we have permission
    this.push.hasPermission()
        .then((res: any) => {
            if (res.isEnabled) {
                console.log('We have permission to send push notifications');
                // configuration of push notification
                this.pushOptions = {
                    android: {
                        senderID: 'XXXXXXXXXXX',
                        icon: 'icon_notification'
                    },
                    ios: {
                        alert: 'true',
                        badge: true,
                        sound: 'false',
                        senderID: 'XXXXXXXXXXX'
                    },
                    windows: {}
                };
                this.pushObject = this.push.init(this.pushOptions);

                // attach push events
                this.storage.get('isPushRegistered')
                    .then(isPushRegistered => {
                        if( !isPushRegistered ){
                            this.pushObject.on('registration').subscribe((registration: any) => {
                                console.log('Device registered', registration)
                                this.storage.set('isPushRegistered', true)
                            });
                        }
                    })


                this.pushObject.on('notification').subscribe((notification: any) => {
                    console.log('Received a notification', notification)
                });
                this.pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
            }
        });
}

所以,在我的代码中,您可以看到 this.pushObject.on('notification') 事件。应用关闭时不会触发。

感谢您的时间和支持。

【问题讨论】:

  • 您是否在后端发送的有效负载数据中设置了“content-available”:1。
  • 是的,我已经设置好了。但仍然没有成功。另外,我已经阅读了 phonegap 插件文档,其中统计如下:“如果用户杀死了应用程序,则不会对推送数据进行进一步处理。” github.com/phonegap/phonegap-plugin-push/blob/master/docs/…
  • 当app关闭时,您无法执行phone-gap-push的任何功能。但是当它在后台时,您可以通过设置“content-available”来执行:1.

标签: cordova push-notification ionic2 cordova-plugins phonegap-pushplugin


【解决方案1】:

这不是客户端代码的问题。由于通知负载而出现此问题。

来自 phonegap-plugin-push 的官方文档

根据接收应用的前台/后台状态以及您发送到应用的负载,通知的行为会有所不同。

例如,如果您发送以下有效负载: phonegap-plugin-push/

{
  "notification": {
    "title": "Test Notification",
    "body": "This offer expires at 11:30 or whatever",
    "notId": 10
  }
}

当您的应用处于前台时,您注册的任何 on('notification') 处理程序都会被调用。但是,如果您的应用程序在后台,则通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,但您的 on('notification') 处理程序将被调用,因为具有通知负载的消息不会导致插件 onMessageReceived 方法被调用。

如果您发送包含通知和数据对象混合的有效负载,如下所示:

{
    "notification": {
        "title": "Test Notification",
        "body": "This offer expires at 11:30 or whatever",
        "notId": 10
    },
    "data" : {
        "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
    }
}

当您的应用程序处于前台时,您注册的任何 on('notification') 处理程序都会被调用。如果您的应用程序在后台,通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,并且您的 on('notification') 处理程序将被调用,因为具有通知负载的消息不会导致插件 onMessageReceived 方法被调用。

使用此插件时我推荐的推送负载格式(虽然它与 Google 的文档不同)在 100% 的情况下都有效:

{
    "data" : {
        "title": "Test Notification",
        "body": "This offer expires at 11:30 or whatever",
        "notId": 10,
        "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
    }
}

当您的应用程序处于前台时,您注册的任何 on('notification') 处理程序都会被调用。如果您的应用程序在后台,则通知将显示在系统托盘中。单击系统托盘中的通知将启动应用程序,您的 on('notification') 处理程序使用以下数据调用:

{
    "message": "This offer expires at 11:30 or whatever",
    "title": "Test Notification",
    "additionalData": {
        "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
    }
}

Link to the docs

【讨论】:

  • 完美!我花了一天时间来解决这个问题,最后,这解决了我的问题。
  • 非常适合 android,但更改它会破坏 ios 的通知
  • 如果应用完全关闭怎么办?例如,我的通知有效负载包含通知和数据对象。我在这里问了同样的问题github.com/phonegap/phonegap-plugin-push/issues/2575
  • 嘿@HiteshUpadhyay 有你找到任何解决方案我也被困在这里
  • @KishanOza,嗨,兄弟,我验证的答案对我有用。请注意,这里的数据结构非常重要。
【解决方案2】:

以下是对我有用的代码。当应用程序关闭时,您会收到通知。如果您想通过查看一些有效负载数据来处理点击事件。然后看看下面的代码:

pushObject.on('notification').subscribe((notification: any) => {

    // this method will be called when you click on notification when app is closed
    pushObject.finish()
       .then(e => {})
       .catch(e => { console.log("ERROR NOTIFICATION",e); })

}).catch(e => {
    console.log("ERROR NOTIFICATION",e);
})

【讨论】:

【解决方案3】:

可以通过以下代码查看前台:

pushObject.on('notification').subscribe((data: any) => {
      console.log('message -> ' + data.message);
      //if user using app and push notification comes
      if (data.additionalData.foreground) {
        // if application open, show popup
      }
      else{
       //if user NOT using app and push notification comes
      }

有关在 Ionic 中发送推送通知的更多详细信息,您可以访问:

https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2020-07-29
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多