【问题标题】:Receiving notification additional data on Ionic (ionic native push / phonegap-plugin-push)在 Ionic 上接收通知附加数据(离子原生推送/phonegap-plugin-push)
【发布时间】:2020-06-16 19:08:28
【问题描述】:

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

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

应用打开,但在主页或之前打开的任何页面中。

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

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

我们使用以下版本:

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

我们知道这个插件已经停产,我们可能应该改用 OneSignal,但我们会尽量避免这种情况,除非这是我们最后的手段,因为它需要额外的开发。

这是我们使用有效负载创建通知的 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())

【问题讨论】:

  • pushObject.on('notification') 中尝试console.log 来检查您是否获得了数据。
  • 如您所见,onNotification 方法已经做了一个控制台日志,没有任何成功。

标签: cordova ionic-framework push-notification ionic-native phonegap-plugin-push


【解决方案1】:

不确定这是否适合您,但在我之前的应用程序中,我必须为 android 和 iOS 设置不同的结构。

安卓:

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

iOS:

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

或者..

您可以通过 Android notification data pass 访问下面的有效负载应该适用于 Android 和 iOS。

{ “优先级”:“高”, “通知” : { “标题”:“标题”, “身体”:“身体”, “声音”:“默认”, “click_action”:“com.adobe.phonegap.push.background.MESSAGING_EVENT” }, “数据” : { “更多”:“数据在这里” }, “到”:“身份证” }

希望这会有所帮助。干杯。

【讨论】:

  • 您好,感谢您的回答。我正在使用适用于 Android 的 Firebase Cloud 消息发送通知,它应该已经可以与我提供的有效负载一起使用,无论如何我都会编辑我的问题以添加该部分。其他选项是开放的拉取请求?我不确定它是否可以在稳定版本中工作
  • 终于修复了...这是给你的赏金,以感谢你的努力。
【解决方案2】:

所以我终于设法修复它...必须在数据负载中设置content-available: "1",在推送请求正文中设置contentAvailable: true,以及设置noCache: "1"

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

如果additionalData.foreground 为真,您还需要在通知回调中进行处理,因为在前台,通知事件会在通知显示后立即触发,并在前景被点击时再次触发为假。

这是我发送到有效的 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多