【问题标题】:How to collapse a Web Push notification sent by FCM如何折叠 FCM 发送的 Web 推送通知
【发布时间】:2020-09-07 23:25:56
【问题描述】:

我正在使用 FCM 和 firebase-admin 为多个客户端发送 Web 推送通知。我想用新消息替换旧消息。

我是这样称呼它的:

return await admin.messaging().sendMulticast({
  tokens,
  notification: {
    title: "Message Title",
    body: "Message body.",
  },
  data: {
    type: "ring",
    callId,
  },
  webpush: {
    fcmOptions: {
      link: process.env.PWA_APP_URL,
    },
    headers: {
      Urgency: "high",
    },
  },
  android: {
    collapseKey: "ring",
    priority: "high",
    ttl: 10,
    notification: {
      color: "#ff0000",
      defaultSound: true,
      sound: "default",
      lightSettings: {
        color: "#ffcc00",
        lightOffDurationMillis: 1000,
        lightOnDurationMillis: 1000,
      },
      tag: "ring",
      vibrateTimingsMillis: [1000, 1000],
      visibility: "public",
      priority: "max",
    },
  },
});

当消息到达 Android 应用客户端时,由于 android.collapseKey 属性,它会被折叠。但我无法在我的 Web 应用程序中获得相同的行为。

根据文档,有一个用于 Web 通知的 Topic 选项,但我不确定 where to put it。我尝试将其用作webpush.headers.Topic 属性,但没有成功。 Android/Chrome 中的消息不会折叠。

我做错了什么?

【问题讨论】:

  • 当你在你的 service worker 中调用 showNotification 时,你可以传递一个 tag 选项:任何以前带有该标签的通知都会被替换。这是 Push API 标准……我不知道专有的 FCM SDK 是如何处理这些选项的。

标签: firebase firebase-cloud-messaging firebase-admin web-push


【解决方案1】:

我已关注collimarco's tip,可以使用showNotification method 折叠通知。

在 firebase-admin/server 代码中,我必须删除消息负载中的通知字段。现在它只是一个data message

return await admin.messaging().sendMulticast({
  tokens,
  data: {
    type: "ring",
    callId,
  },
});

在 Web 应用程序中,我创建了一个 background message handler。现在,当我使用带有renotify=truetag 属性和renotify=true 的新消息到达时,我可以调用showNotification 方法。

messaging.setBackgroundMessageHandler(function (payload) {
  // ...
  return self.registration.showNotification("Message Title", {
    body: "Message body.",
    tag: "ring",
    renotify: true,
    vibrate: [1000, 1000],
  });
});

最好只在消息负载上设置这些属性。

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2020-09-17
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多