jazzband / django-push-notifications

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

Wrong implementation of FCM #343

Closed axsapronov closed 7 years ago

axsapronov commented 7 years ago

Hello,

I think your implementation of FCM (#302) is wrong.

2 reasons:

First: In docs we have requests with Content-Type: application/json Your implementation - application/x-www-form-urlencoded;charset=UTF-8 Docs - https://firebase.google.com/docs/cloud-messaging/send-message

Second: A body is wrong - FCM expects JSON with keys: to, data, notification. But you send dict with key registration_id

Docs - https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

As result, I wrote trash-implementation in commit https://github.com/WarmongeR1/django-push-notifications/commit/7fdad2a2079db1049c7c807b287d62672bf5e611 I didn't fix tests, I only demonstrate work version of code

And current interface send_message is unsuitable for FCM. Because if we send Push-notification with FCM we should write

python device.send_message(None, extra={'notification': {'body': 'lfdlfds'}})

Sorry for my bad english.

jamaalscarlett commented 7 years ago

@WarmongeR1 thanks for this. I think that while GCM has been deprecated, Google has designed the FCM to accept GCM style messages. Here is there troubleshooting curl command from the FCM page: FCM server

Checking the validity of a server key If you receive authentication errors when sending messages, check the validity of your Server key. For example, on Android, run the following command:

api_key=YOUR_SERVER_KEY
curl --header "Authorization: key=$api_key" \
       --header Content-Type:"application/json" \
       https://fcm.googleapis.com/fcm/send \
       -d "{\"registration_ids\":[\"ABC\"]}"

As you see, it uses the old GCM style request structure. So, while #336 does work, I would agree that the implementation is not correct. Please note that plaint text requests are supported. Again from the FCM docs:

Authentication

To send a message, the app server issues a POST request. For example:

https://fcm.googleapis.com/fcm/send A message request consists of two parts: the HTTP header and the HTTP body.

The HTTP header must contain the following headers:

Authorization: key=YOUR_SERVER_KEY Make sure this is the server key, whose value is available in the Cloud Messaging tab of the Firebase console Settings pane. Android, iOS, and browser keys are rejected by FCM. Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text. If Content-Type is omitted, the format is assumed to be plain text.

debugger22 commented 7 years ago

Any tentative date for when is it getting a release?

jamaalscarlett commented 7 years ago

See #360