service-workerweb-pushpush-apiweb-notifications

web push notification doesn't show if pc is off or sleeping


So I have finally implemented a successfull push notification for my website however when i push the notification and suppose the reciever's pc is off , or in sleep mode the gcm returns success but the notification is lost ,it never pops up for that user ...while on the other hand i noticed facebook seems to be doing something that helps it get past this problem .. you see a facebook chrome notification as soon as you open chrome the first thing after a fresh restart ... does any body have any idea as to how fb gets past this problem ??


Solution

  • If you're using a standard Web Push service (e.g. in Firefox), you need to define a header called 'TTL'. From the standard (https://datatracker.ietf.org/doc/html/draft-ietf-webpush-protocol-02):

    An application server can use the TTL header field to limit the time that a push message is retained by a push service. The TTL header field contains a value in seconds that describes how long a push message is retained by the push service.

    If you don't set it, the default is 0.

    GCM doesn't support the Web Push standard yet, but it does have a time_to_live option that should achieve the same result: https://developers.google.com/cloud-messaging/concept-options#ttl

    EDIT: The Mozilla push service now requires the TTL header: https://blog.mozilla.org/services/2016/02/20/webpushs-new-requirement-ttl-header/.