接收有关Ionic的通知附加数据(离子本机推送/phonegap-plugin-push) [英] Receiving notification additional data on Ionic (ionic native push / phonegap-plugin-push)

查看:81
本文介绍了接收有关Ionic的通知附加数据(离子本机推送/phonegap-plugin-push)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在努力使用Ionic Native推送插件(基于phonegap-plugin-push).当我们确实收到发送的推送通知时,我们无法处理我们发送的特定有效负载,因此,在点击通知时,应用程序会在特定页面中打开.

We are struggling with Ionic Native push plugin (which is based on phonegap-plugin-push). While we do receive push notifications sent, we cannot process the specific payload that we send so that when the notification is tapped, the app opens in a specific page.

对于Android推送通知,我们使用Firebase Cloud Messaging传递通知,对于iOS,我们使用APNS.

For Android push notifications we use Firebase Cloud Messaging to deliver the notifications, for iOS we are using APNS.

该应用会打开,但可以在主页中或之前打开过的任何页面中.

The app opens, but either in the home page or whatever page was open before.

这是我们的初始推送代码:

Here is our init push code:

private initPush() {
    this.push.hasPermission()
        .then((res: any) => {
           // just some console logs irrelevant to this question
        });
    const options: PushOptions = {
      android: { clearBadge: true, forceShow: true },
      ios: { alert: 'true', badge: true, sound: 'false', clearBadge: true },
      windows: {}
    };
    const pushObject: PushObject = this.push.init(options);
    try {
      pushObject.on('notification').subscribe((notification: any) => this.onNotification(notification));
      pushObject.on('registration').subscribe((registration: any) => this.onRegister(registration));
        pushObject.on('error').subscribe(error => this.onError(error));
        this.authorizationService.currentUser.subscribe((nextUser) => this.fullfillPushTokenRegistration());
    } catch (e) {
      console.error('Error on registering push methods', e);
    }
  }

  onNotification(notification): void {
      console.info('On Push Notification', JSON.stringify(notification));
      const addData = notification.additionalData;
      this.notificationEventsService.createEvent('push', 'click', addData);
  }

应与Ionic Native推送"notification"事件一起触发的 onNotification 方法从未调用,因此我们无法处理额外的有效负载,该额外的有效负载使我们可以导航到与以下内容相关的特定页面:通知.

The onNotification method, which should be fired with the Ionic Native push "notification" event is never called, hence we can't process the additional payload that lets us navigate to the specific page related to the notification.

我们正在使用以下版本:

We're using the following versions:

@ionic/core: 4.11.1
@ionic-native/push: 5.15.0
phonegap-plugin-push: 2.3.0
@angular/core: 8.1.2

我们知道此插件已停产,我们可能应该切换到OneSignal,但除非我们最后一招,否则我们将尽量避免使用此插件,因为它需要进行其他开发.

We are aware that this plugin is discontinued and that we should probably switch to OneSignal, but we're trying to avoid this unless it's our last resort, since it would require an additional development.

这是Kotlin代码片段,如果有帮助,我们可以在其中创建带有有效负载的通知:

This is the Kotlin code fragment where we create the notification with the payload, if it helps:

val message = Message.builder().setToken(device.deviceToken)
val fcmNotification: com.google.firebase.messaging.Notification = com.google.firebase.messaging.Notification(
            notification.title, notification.message
        )
message.setNotification(fcmNotification)
message.putData("action", notification.action!!.toString())
message.putData("pendingToViewUserNotifications", pendingToViewUserNotifications.toString())
message.putData("referenced", notification.referenced)
message.putData("notificationId", notification.id.toString())
message.putData("title", notification.title)
message.putData("body", notification.message)
message.putData("badge", pendingToViewUserNotifications.toString())
message.putData("content-available", "1")

when (device.deviceOs!!.toLowerCase()) {
    "android" -> message.setAndroidConfig(AndroidConfig.builder()
        .setTtl(3600 * 1000)
        .setNotification(AndroidNotification.builder()
        .setIcon("stock_ticker_update")
        .setColor("#f45342")
        .build())
        .build())

推荐答案

所以我终于设法解决了这个问题……必须在数据有效载荷中设置一个 content-available:"1" 并且推送请求正文中的 contentAvailable:true ,以及设置 noCache:"1" .

So I finally managed to fix it... had to set a content-available: "1" in the data payload AND a contentAvailable: true in the push request body, as well as setting noCache: "1".

放弃Firebase SDK并手动执行请求,现在它最终调用 on('notification').

Ditched out the Firebase SDK and manually performed the request, now it finally calls on('notification').

如果 additionalData.foreground 为true,您还希望在通知回调中进行处理,因为在前台,通知事件在显示通知后立即触发,并在前台再次触发到点击时为false.

You'll want to handle also in your notification callback if additionalData.foreground is true, because in foreground the notification event is fired as soon as the notification is displayed, and fired again with foreground to false when tapped.

这是我发送到FCM API的有效示例负载

Here's the sample payload I'm sending to FCM API that worked:

{
    "to":"FIREBASE_TOKEN",
    "content_available": true,
    "data": {
        "action": "ACTION(Custom payload data)",
        "referenced": "REFERENCED(Custom payload data)",
        "force-start": "1",
        "content-available": "1",
        "no-cache": "0",
        "notId": "4361c4f5-ae8d-42fa-a863-a04acff2ab7a",
        "title": "TEST"
    }
    "priority":10
}

这篇关于接收有关Ionic的通知附加数据(离子本机推送/phonegap-plugin-push)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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