且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序在后台运行时,不会触发phonegap-plugin-push on("notification")事件

更新时间:2022-12-27 10:47:09

这不是客户端代码的问题.发生此问题的原因是通知有效负载.

摘自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方法./p>

如果您发送的负载中包含通知和像这样的数据对象:

{
    "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方法./p>

在使用此插件时(与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"
    }
}

链接到文档

I am using following plugin for push notification in Ionic2

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

Expected Behaviour: When app is closed, And notification received, And when user tap the notification, on("notification") event should fire after app opens.

Actual Behaviour: I am getting notification successfully. But When Application is in background or closed, at that time when I receive notification and I tap the notification, on("notification") event is not firing.

Cordova version 7.0.1 Android version 6.2.3

My code:

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

So, on my code, you can see this.pushObject.on('notification') event. that is not firing when app is closed.

Thank you for your time and support.

This is not the issue with client side code. This issue is occuring because of the notification payload.

From Official docs of phonegap-plugin-push

Notifications behave differently depending on the foreground/background state of the receiving app and the payload you send to the app.

For instance if you send the following payload: phonegap-plugin-push/

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

When your app is in the foreground, any on('notification') handlers you have registered will be called. However, if your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app but your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.

If you send a payload with a mix of notification & data objects like this:

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

When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app and your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.

My recommended format for your push payload when using this plugin (while it differs from Google's docs) works 100% of the time:

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

When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, then the notification will show up in the system tray. Clicking on the notification in the system tray will start the app, and your on('notification') handler will be called with the following data:

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

Link to the docs