Search code examples
djangofirebasedjango-rest-frameworkfirebase-cloud-messagingdjango-push-notifications

How can I use TTL to prevent a message backlog when using Firebase Cloud Messaging with Django-Push-Notifications?


I am working with Firebase Cloud Messaging in Django using django-push-notifications to deliver push notifications to our users via desktop notifications.

After a browser is fully closed (such as when the the computer is turned off), our users receive a backlog of all notifications previously sent next time they boot up.

While there are situations where a user would want to receive the entire backlog of messages, this is not one of them.

It seems the answer is to set TTL=0, as per this section of the FCM documentation, but my attempts are not resulting in the desired behavior.

Please help me better understand TTL in this context. If TTL is the right way to go, what is the proper way to format TTL in send_message() using django-push-notifications so that messages will not accumulate if not immediately delivered?

Here is what I have attempted:

devices.send_message(
    body,
    TTL=0,
    time_to_live=0,
    link='blah',
    extra={'title': 'blah blah', 'icon': '/foo/bar.png'}
)

Solution

  • The format that you send seems different from the one in the documentation you linked. From the documentation:

    {
      "message":{
        "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "data":{
          "Nick" : "Mario",
          "body" : "great match!",
          "Room" : "PortugalVSDenmark"
        },
        "apns":{
          "headers":{
            "apns-expiration":"1604750400"
          }
        },
        "android":{
          "ttl":"4500s"
        },
        "webpush":{
          "headers":{
            "TTL":"4500"
          }
        }
      }
    }
    

    So key here is that the time-to-live for a webpush message is set under webpush/headers/TTL, while you're adding it to the top-level.