silhouette hustler
silhouette hustler

Reputation: 1763

FCM push notifications queuing up if browser is closed / Web-push

When browser is closed, every push notification that has been sent in the meantime is queued up, which leads to receiving hundreds of notifications when I open the browser for the first time next day. Is there a way of stopping push notifications queuing up?

Upvotes: 0

Views: 3164

Answers (3)

silhouette hustler
silhouette hustler

Reputation: 1763

For those using web push c# library the code reference to this is something like below:

    public void SendPushNotification(PushSubscription sub)
    {           

        var pushEndpoint = sub.EndPoint;
        var p256Dh = sub.P256dh;
        var auth = sub.Auth;
        const string subject = @"mailto:[email protected]";
        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);
        }
    }

Upvotes: 0

collimarco
collimarco

Reputation: 35400

There are two options:

  • when you make the request to the push service you can include a TTL header (in seconds) for the notification, so that it expires if it is not delivered within that time
  • you can use a tag for the notifications when you display them to hide the older notifications that belong to the same group (tag)

Upvotes: 1

AL.
AL.

Reputation: 37778

An option is to make use of time_to_live:

This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline. The maximum time to live supported is 4 weeks, and the default value is 4 weeks. For more information, see Setting the lifespan of a message.

and set it to your desired time. Basically it determines until only when FCM would keep the message on the queue.

Upvotes: 1

Related Questions