【问题标题】:Customize notification from firebase cloud messaging running in background handler自定义来自在后台处理程序中运行的 firebase 云消息传递的通知
【发布时间】:2017-07-21 23:58:56
【问题描述】:

如何自定义由 Web 应用程序中 Firebase 的后台处理程序创建的通知?

一些示例代码,经过我的修改,如下所示:

messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);

  // Customize notification here
  const data = payload.data;
  const click_action = "https://www.google.com";
  const notificationTitle = data.title;
  const notificationOptions = {
    "body": data.body,
    "icon": "icon.png",
    "click_action": click_action
  };

  const timeout = data.timeout;

  // var n = new Notification(notificationTitle, notificationOptions);
  // n.onclick = function () {
  //   if (click_action !== null) {
  //     window.open(click_action);
  //   }
  // };
  // setTimeout(n.close.bind(n), 5000);

  return self.registration.showNotification(notificationTitle, notificationOptions);
});

我想设置超时选项。当我在内容脚本中创建通知时,我使用了允许我指定超时的注释掉的代码。但是,我不能在这里应用相同的技术。

我还想自定义点击操作。在通知选项对象中指定 click_action 键对我没有任何作用。在内容脚本中,我会添加一个 onClickListener。

总的来说,我需要一种方法来添加点击侦听器并自定义超时持续时间。

【问题讨论】:

  • 将 "time_to_live" : 3 添加到您的 JSON 有效负载中。

标签: javascript firebase web-applications firebase-cloud-messaging service-worker


【解决方案1】:

通过将"time_to_live" : 3 添加到您的 JSON 负载中,这指定了 FCM 中消息的生命周期。

您可以在此查看更多详细信息 https://firebase.google.com/docs/cloud-messaging/concept-options#ttl

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 2017-05-05
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2020-01-02
    • 2021-08-04
    相关资源
    最近更新 更多