当应用在后台

时间:2017-09-12 12:25:28

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

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

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

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

实际行为 我成功收到通知。但是当应用程序处于后台或关闭时,当我收到通知并点击通知时,on(“notification”)事件未触发。

Cordova版 7.0.1 Android版 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')事件。应用程序关闭时不会触发。

感谢您的时间和支持。

3 个答案:

答案 0 :(得分:6)

这不是客户端代码的问题。由于通知有效负载,此问题正在发生。

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

  

通知的行为会有所不同,具体取决于接收应用的前景/背景状态以及您发送到应用的有效负载。

     

例如,如果您发送以下有效负载:   的PhoneGap-插件推/

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

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

     

如果您通过混合通知发送有效负载&像这样的数据对象:

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

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

     

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

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

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

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

Link to the docs

答案 1 :(得分:0)

以下代码对我有用。应用关闭后,您会收到通知。如果要通过查看某些有效负载数据来处理click事件。然后看看下面的代码:

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

答案 2 :(得分:0)

您可以通过以下代码检查forground:

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

相关问题