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

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

问题描述

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

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

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

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

Cordova 版本 7.0.1安卓版本 6.2.3

我的代码:

this.platform.ready().then(() => {this.pushsetup();});私有 pushOptions:PushOptions;私有推送对象:推送对象;pushsetup() {//检查我们是否有权限this.push.hasPermission().then((res: any) => {如果(res.isEnabled){console.log('我们有权发送推送通知');//推送通知的配置this.pushOptions = {安卓: {发件人ID:'XXXXXXXXXXX',图标:'icon_notification'},IOS:{警报:'真实',徽章:真实,声音:'假',发件人ID:'XXXXXXXXXXX'},窗口:{}};this.pushObject = this.push.init(this.pushOptions);//附加推送事件this.storage.get('isPushRegistered').then(isPushRegistered => {如果(!isPushRegistered){this.pushObject.on('registration').subscribe((registration: any) => {console.log('设备已注册', 注册)this.storage.set('isPushRegistered', true)});}})this.pushObject.on('notification').subscribe((notification: any) => {console.log('收到通知', notification)});this.pushObject.on('error').subscribe(error => console.error('推送插件出错', error));}});}

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

感谢您的时间和支持.

解决方案

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

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

<块引用>

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

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

<代码>{通知": {"title": "测试通知","body": "此优惠将于 11:30 或其他时间到期",notId":10}}

<块引用>

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

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

<代码>{通知": {"title": "测试通知","body": "此优惠将于 11:30 或其他时间到期",notId":10},数据" : {surveyID":ewtawgreg-gragrag-rgarhthgbad"}}

<块引用>

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

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

<代码>{数据" : {"title": "测试通知","body": "此优惠将于 11:30 或其他时间到期",notId":10,surveyID":ewtawgreg-gragrag-rgarhthgbad"}}

<块引用>

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

<代码>{"message": "此优惠将于 11:30 或其他时间到期","title": "测试通知",附加数据":{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天全站免登陆