jazzband / django-push-notifications

Send push notifications to mobile devices through GCM or APNS in Django.
MIT License
2.26k stars 613 forks source link

Sending Data message with FCM not working #541

Closed nathando closed 4 years ago

nathando commented 4 years ago

Hi contributors, firstly thank you for your great package!
I have an issue regarding the "data" message format of FCM.

So following example in the doc, I send the data message only format (the only difference is that I send from a queryset instead of actual device):

# ==== Documentation ====
# Send a data message only
fcm_device.send_message(None, extra={"other": "content", "misc": "data"})

# ==== My code ====
devices.send_message(None, extra=data)

However, on my FCM console's report, it shows only Notification type and Data type is empty. The current version I am using is 1.6.1, which is supposed to be the latest as well. Not sure if anyone encountered the same issue and how to solve this. If it is really not working, then I believe it is a bug.

Screenshot 2019-12-11 at 1 52 02 PM

nathando commented 4 years ago

okay after digging a bit deeper it seems that it's because of this part of the code

def _cm_send_request(
    ...
    if cloud_type == "FCM" and use_fcm_notifications:
        notification_payload = {}
        if "message" in data:
            notification_payload["body"] = data.pop("message", None)

        for key in FCM_NOTIFICATIONS_PAYLOAD_KEYS:
            value_from_extra = data.pop(key, None)
            if value_from_extra:
                notification_payload[key] = value_from_extra
            value_from_kwargs = kwargs.pop(key, None)
            if value_from_kwargs:
                notification_payload[key] = value_from_kwargs
        if notification_payload:
            payload["notification"] = notification_payload
...

In my data, I have a title, which is one of the key in FCM_NOTIFICATIONS_PAYLOAD_KEYS, so it was automatically pulled out and insert into notification. Final payload looks like this:

{
  "data": {
    "data": "{}",
    "registration_ids": ["aaaa", "aaaa2"],
    "target": "main",
    "text": "The title",
    "type": "some_type"
  },
  "notification": {
    "title": "The title"
  },
  "registration_ids": ["aaaa", "aaaa2"]
}

I think it is better to at least highlight this behavior in the documentation so that it's less confusing for other users.

rollue commented 4 years ago

I agree. I had the same issue and solved the problem thanks to @nathando