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

urllib.error.HTTPError: HTTP Error 400: Bad Request #512

Closed kirtan73 closed 5 years ago

kirtan73 commented 5 years ago

While sending push notification to the android device via GCM token it gives the following error:

File "/usr/local/lib/python3.7/site-packages/push_notifications/gcm.py", line 209, in send_message chunk, data, cloud_type=cloud_type, application_id=application_id, *kwargs File "/usr/local/lib/python3.7/site-packages/push_notifications/gcm.py", line 158, in _cm_send_request json_payload, "application/json", application_id=application_id File "/usr/local/lib/python3.7/site-packages/push_notifications/gcm.py", line 59, in _gcm_send request, timeout=get_manager().get_error_timeout("GCM", application_id) File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/usr/local/lib/python3.7/urllib/request.py", line 531, in open response = meth(req, response) File "/usr/local/lib/python3.7/urllib/request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "/usr/local/lib/python3.7/urllib/request.py", line 569, in error return self._call_chain(args) File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/usr/local/lib/python3.7/urllib/request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 400: Bad Request

mcarim commented 5 years ago

I've just found out a band-aid solution.

Just add "GCM_POST_URL": "https://fcm.googleapis.com/fcm/send" to PUSH_NOTIFICATIONS_SETTINGS and it will work.

It seems google deprecated "android.googleapis.com/gcm/send" endpoint.

zacid commented 5 years ago

In the case above you are simply adjusting the GCM_POST_URL to rather call Googles new FCM service. The suggestion below might be the better way to fix your issue.

In my case it was that we had loads of GCMDevices set to cloud_message_type='GCM' and this caused the HTTP Error 400: Bad Request

See code snippet here: https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/gcm.py#L153-L164

Fixed this by GCMDevice.objects.filter(cloud_message_type='GCM').update(cloud_message_type='FCM')

Now the request is calling FCM rather than the old GCM service.

Might be helpful to someone else.

kirtan73 commented 5 years ago

Thanks, @zac156 it worked for me, it might be possible that GCM has been deprecated as @mcarim has commented. Closing this issue.