【发布时间】:2018-01-24 05:23:28
【问题描述】:
当浏览器关闭时,其间发送的每条推送通知都会排队,导致第二天第一次打开浏览器时收到数百条通知。有没有办法阻止推送通知排队?
【问题讨论】:
标签: javascript push-notification firebase-cloud-messaging web-push
当浏览器关闭时,其间发送的每条推送通知都会排队,导致第二天第一次打开浏览器时收到数百条通知。有没有办法阻止推送通知排队?
【问题讨论】:
标签: javascript push-notification firebase-cloud-messaging web-push
一个选项是使用time_to_live:
此参数指定如果设备离线,消息应在 FCM 存储中保留多长时间(以秒为单位)。支持的最长生存时间为 4 周,默认值为 4 周。如需更多信息,请参阅Setting the lifespan of a message。
并将其设置为您想要的时间。基本上它只确定 FCM 将消息保留在队列中的时间。
【讨论】:
对于那些使用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);
}
}
【讨论】:
有两种选择:
TTL 标头(以秒为单位),这样如果在该时间内未送达,它就会过期tag 通知when you display them 来隐藏属于同一组(标签)的旧通知【讨论】: