【问题标题】:FCM push notifications queuing up if browser is closed / Web-push如果浏览器关闭/网络推送,FCM 推送通知排队
【发布时间】:2018-01-24 05:23:28
【问题描述】:

当浏览器关闭时,其间发送的每条推送通知都会排队,导致第二天第一次打开浏览器时收到数百条通知。有没有办法阻止推送通知排队?

【问题讨论】:

    标签: javascript push-notification firebase-cloud-messaging web-push


    【解决方案1】:

    一个选项是使用time_to_live:

    此参数指定如果设备离线,消息应在 FCM 存储中保留多长时间(以秒为单位)。支持的最长生存时间为 4 周,默认值为 4 周。如需更多信息,请参阅Setting the lifespan of a message

    并将其设置为您想要的时间。基本上它只确定 FCM 将消息保留在队列中的时间。

    【讨论】:

      【解决方案2】:

      对于那些使用web push c# 库的人,对此的代码参考如下:

          public void SendPushNotification(PushSubscription sub)
          {           
      
              var pushEndpoint = sub.EndPoint;
              var p256Dh = sub.P256dh;
              var auth = sub.Auth;
              const string subject = @"mailto:xyz@xyz.com";
              var publicKey = PushConfiguration.PublicKey; //configuration attribute coming from the web.config file
              var privateKey = PushConfiguration.PrivateKey; //configuration attribute coming from the web.config file
              var subscription = new PushSubscription(pushEndpoint, p256Dh, auth);
              var vapidDetails = new VapidDetails(subject, publicKey, privateKey);
              var options = new Dictionary<string, object> {{"TTL", 3}, {"vapidDetails", vapidDetails}};
              var myPayload = new NotificationData(){// my data};
      
              var webPushClient = new WebPushClient();
              try
              {
                  webPushClient.SendNotification(subscription, myPayload , options);
              }
              catch (WebPushException exception)
              {
                  Console.WriteLine("Http STATUS code" + exception.StatusCode);
              }
          }
      

      【讨论】:

        【解决方案3】:

        有两种选择:

        • 当您向推送服务发出请求时,您可以在通知中包含 TTL 标头(以秒为单位),这样如果在该时间内未送达,它就会过期
        • 您可以使用tag 通知when you display them 来隐藏属于同一组(标签)的旧通知

        【讨论】:

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