【问题标题】:i have getting "do_not_collapse" as payload in FCM WEB notification我在 FCM WEB 通知中将“do_not_collapse”作为有效负载
【发布时间】:2017-11-09 13:15:45
【问题描述】:

我第一次做 fcm 网络通知并收到一条消息

{    from: "205864560478",    collapse_key: "do_not_collapse"   }

我的代码是

                messaging.onMessage(function(payload) {
                  console.log("Message received. ", payload);
                  // ...
                });

还有我的 firebase-messaging-sw.js

      const messaging = firebase.messaging();


    messaging.setBackgroundMessageHandler(function(payload) {
      console.log('[firebase-messaging-sw.js] Received background message ', payload);
      // Customize notification here
      const notificationTitle = 'Background Message Title';
      const notificationOptions = {
        body: 'Background Message body.',
        icon: '/firebase-logo.png'
      };

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

请帮忙。我不知道为什么我会收到这个错误 我完美地为 ios 和 Android 工作。 我的 web 项目在 localhost 中并且没有 https

【问题讨论】:

    标签: angularjs firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    { from: "205864560478", collapse_key: "do_not_collapse" } 是预期的有效负载,它不是错误。 此外,为了开发,localhost 不受 https 条件的限制。

    在 FCM 中,如果用户当前正在查看您的网页并收到推送通知,那么它将不会显示为推送通知(默认行为)。

    消息的行为取决于页面是否在 前景(有焦点),或在背景中,隐藏在其他 选项卡,或完全关闭。

    当您的应用处于前台时(用户当前正在查看您的 网页),您可以直接在 页面。

    https://firebase.google.com/docs/cloud-messaging/js/receive

    如果您仍想在用户当前查看您的网页时显示通知,那么您可以在 messaging.onMessage 添加推送代码逻辑

    例子

    messaging.onMessage(function (payload) {
        console.log('Message received. ', payload);
        var options = {
            body: 'Background Message body.', // payload.body
            icon: '/firebase-logo.png' .      // payload.icon
        };
        var n = new Notification('Notification says',options);
        setTimeout(n.close.bind(n), 5000);
    });
    

    【讨论】:

    • 感谢您的回复
    猜你喜欢
    • 2016-09-21
    • 2017-05-26
    • 2018-11-03
    • 2020-04-20
    • 2015-09-24
    • 2018-04-04
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多