当应用程序在后台运行时,不会触发phonegap-plugin-push on("notification")事件 [英] phonegap-plugin-push on("notification") event is not firing when app is in background

查看:95
本文介绍了当应用程序在后台运行时,不会触发phonegap-plugin-push on("notification")事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

科尔多瓦版本 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')事件.关闭应用程序后不会触发.

感谢您的时间和支持.

解决方案

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

摘自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

这篇关于当应用程序在后台运行时,不会触发phonegap-plugin-push on("notification")事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆