简体   繁体   English

在 Ionic 上接收通知附加数据(ionic native push / phonegap-plugin-push)

[英]Receiving notification additional data on Ionic (ionic native push / phonegap-plugin-push)

We are struggling with Ionic Native push plugin (which is based on phonegap-plugin-push).我们正在努力使用 Ionic Native 推送插件(基于 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.虽然我们确实收到发送的推送通知,但我们无法处理我们发送的特定负载,因此当点击通知时,应用程序会在特定页面中打开。

For Android push notifications we use Firebase Cloud Messaging to deliver the notifications, for iOS we are using APNS.对于 Android 推送通知,我们使用 Firebase Cloud Messaging 来传递通知,对于 iOS,我们使用 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);
  }

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. onNotification方法应该与 Ionic Native 推送“通知”事件一起触发,它永远不会被调用,因此我们无法处理让我们导航到与通知相关的特定页面的额外负载。

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

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.我们知道此插件已停产,我们可能应该切换到 OneSignal,但我们正在努力避免这种情况,除非这是我们的最后手段,因为它需要额外的开发。

This is the Kotlin code fragment where we create the notification with the payload, if it helps:这是我们使用有效负载创建通知的 Kotlin 代码片段,如果有帮助的话:

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())

Not sure if this will work for you but in my previous app, I had to set different structure for both android and iOS.不确定这是否适合您,但在我以前的应用程序中,我必须为 android 和 iOS 设置不同的结构。

Android:安卓:

      registration_ids : [],
      data: {
        notId: "", // notId on Android needed to be an int and not a string
        title: "",
        body: "",
        soundname: "default",
      }

iOS: IOS:

      registration_ids : [],
      notification:{
        title: "",
        body: "",
        sound: "default",
      },
      data: {
        title: "",
        body: "",
      }

OR..或者..

you could go thru this Android notification data pass where below payload should work for both Android & iOS.你可以通过这个Android 通知数据传递,下面的有效负载应该适用于 Android 和 iOS。

{ "priority" : "high", "notification" : { "title": "Title", "body": "Body", "sound": "default", "click_action": "com.adobe.phonegap.push.background.MESSAGING_EVENT" }, "data" : { "more": "data goes here" }, "to" : "id" } { "priority": "high", "notification": { "title": "Title", "body": "Body", "sound": "default", "click_action": "com.adobe.phonegap.push .background.MESSAGING_EVENT" }, "data" : { "more": "data go here" }, "to" : "id" }

Hope this helps.希望这可以帮助。 Cheers.干杯。

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" .所以我终于设法修复它......必须在数据有效负载中设置一个content-available: "1"和一个contentAvailable: true在推送请求正文中,以及设置noCache: "1"

Ditched out the Firebase SDK and manually performed the request, now it finally calls on('notification') .放弃 Firebase SDK 并手动执行请求,现在它最终调用on('notification')

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.如果additionalData.foreground为 true,您还需要在您的通知回调中处理,因为在前台通知事件会在显示通知后立即触发,并在点击时再次触发,前景为 false。

Here's the sample payload I'm sending to FCM API that worked:这是我发送到 FCM API 的示例有效负载:

{
    "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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM