jazzband / django-push-notifications

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

Getting BadDeviceToken on remote server but works on local machine #416

Open Alir3z4 opened 7 years ago

Alir3z4 commented 7 years ago

I've configured my project for APNS. The notification goes to the iPhone device with the device registration_id on the local machine, but when I try to do the same thing on remote server it will give this error in the admin panel (using "send test message from admin actions"): Some messages could not be processed: 'BadDeviceToken', when trying to the same on the server shell (./manage.py shell remote machine) I get the following traceback:

In [8]: device.send_message('yellow')
[14/Jul/2017 19:41:11] INFO [hyper.http20.connection:724] Received unhandled event <RemoteSettingsChanged changed_settings:{ChangedSetting(setting=SettingCodes.HEADER_TABLE_SIZE, original_value=4096, new_value=4096), ChangedSetting(setting=SettingCodes.MAX_CONCURRENT_STREAMS, original_value=None, new_value=1000), ChangedSetting(setting=SettingCodes._max_frame_size, original_value=16384, new_value=16384), ChangedSetting(setting=SettingCodes._max_header_list_size, original_value=None, new_value=8000)}>
[14/Jul/2017 19:41:11] INFO [apns2.client:178] Connected to APNs
[14/Jul/2017 19:41:11] INFO [hyper.http20.connection:724] Received unhandled event <SettingsAcknowledged changed_settings:{ChangedSetting(setting=SettingCodes.ENABLE_PUSH, original_value=1, new_value=0)}>
[14/Jul/2017 19:41:11] INFO [hyper.http20.connection:724] Received unhandled event <SettingsAcknowledged changed_settings:{}>
[14/Jul/2017 19:41:11] INFO [hyper.http20.connection:724] Received unhandled event <RemoteSettingsChanged changed_settings:{ChangedSetting(setting=SettingCodes.HEADER_TABLE_SIZE, original_value=4096, new_value=4096), ChangedSetting(setting=SettingCodes.MAX_CONCURRENT_STREAMS, original_value=1000, new_value=1000), ChangedSetting(setting=SettingCodes.INITIAL_WINDOW_SIZE, original_value=65535, new_value=65535), ChangedSetting(setting=SettingCodes._max_frame_size, original_value=16384, new_value=16384), ChangedSetting(setting=SettingCodes._max_header_list_size, original_value=8000, new_value=8000)}>
---------------------------------------------------------------------------
BadDeviceToken                            Traceback (most recent call last)
/app/.heroku/python/lib/python3.6/site-packages/push_notifications/apns.py in apns_send_message(registration_id, alert, application_id, certfile, **kwargs)
    112                         registration_id, alert, application_id=application_id,
--> 113                         certfile=certfile, **kwargs
    114                 )

/app/.heroku/python/lib/python3.6/site-packages/push_notifications/apns.py in _apns_send(registration_id, alert, batch, application_id, certfile, **kwargs)
     93                 get_manager().get_apns_topic(application_id=application_id),
---> 94                 **notification_kwargs
     95         )

/app/.heroku/python/lib/python3.6/site-packages/apns2/client.py in send_notification(self, token_hex, notification, topic, priority, expiration, collapse_id)
     55         if result != 'Success':
---> 56             raise exception_class_for_reason(result)
     57

BadDeviceToken:

During handling of the above exception, another exception occurred:

APNSServerError                           Traceback (most recent call last)
<ipython-input-8-5da7ea2c015f> in <module>()
----> 1 device.send_message('yellow')

/app/.heroku/python/lib/python3.6/site-packages/push_notifications/models.py in send_message(self, message, certfile, **kwargs)
    157                         alert=message,
    158                         application_id=self.application_id, certfile=certfile,
--> 159                         **kwargs
    160                 )
    161

/app/.heroku/python/lib/python3.6/site-packages/push_notifications/apns.py in apns_send_message(registration_id, alert, application_id, certfile, **kwargs)
    118                         device.active = False
    119                         device.save()
--> 120                 raise APNSServerError(status=reason_for_exception_class(apns2_exception.__class__))
    121
    122

APNSServerError: BadDeviceToken

I'm using Heroku.

Versions:

matthewh commented 7 years ago

Hello @Alir3z4

I have encountered an issue very similar to what you describe when I have DEBUG=True on my local machine with the apple sandbox configured and DEBUG=False on the production server against the production apns api. In my situation we had to ensure the correct APNS cert and TOPIC were set in each environment and the device had the correct token registered.

michaelluk commented 7 years ago

@Alir3z4 Does your Django project remain DEBUG=True and your app built in release mode? I was having same error. I fix it by adding APNS_USE_SANDBOX = False in settings.

Alir3z4 commented 7 years ago

I had DEBUG=False in both production and staging environment of the app, however it was DEBUG=True in development for sure.

I had the Topic set correctly as well, but that didn't work as well, tried many different settings but I wasn't able to get it working.

At the end we decided to get to Pusher.com for this, however I really wanted to get notifications working with Django Push Notifications.

However I still think I haven't tried all possible fixes for this scenario, at the moment I'm not working on that part of project and cannot confirm any suggested solutions can resolve the problem.

Please feel free to close this issue since.

@matthewh and @michaelluk thank you very much for the help.

zixan commented 7 years ago

@Alir3z4

Is your device token from the debug build or release build? Make sure the environments are synchronized.

raminf commented 7 years ago

I'm having same issue. It works on local machine (Mac OS Sierra) but doesn't work from remote instance on AWS EC2 (Ubuntu). Checked all the ports on EC2 and regenerated the certs multiple times. Still doesn't work.

In both cases DEBUG=True, APNS_USE_SANDBOX=True and topic set to app bundle-ID. Same settings on both systems using the development push certificate. Not getting any errors. It just silently fails.

On a whim installed Houston CLI and used it to send a push using the very same dev certificate from EC2. It worked. Appears to be isolated to django-push-notifications.

aemdy commented 6 years ago

Exactly the same issue here as well! Waiting for help.

FahadAlbukhari commented 6 years ago

client = APNsClient(APNS_CERTIFICATE, use_sandbox=False, use_alternative_port=False) client.send_notification(token_hex, payload, APNS_TOPIC) Traceback (most recent call last): File "", line 1, in File "/Users/Documents/Development/backend/lib/python3.6/site-packages/apns2/client.py", line 56, in send_notification raise exception_class_for_reason(result) apns2.errors.BadDeviceToken

Appears to be the issue in the """from apns2.client import APNsClient"""

Exactly the same issue here as well! Anything yet???.

eg271 commented 6 years ago

same here

Update: fixed my issue - hadn't appreciated that a release build differs from an Ad Hoc distribution. Using the latter, submits the correct device token.

matt-dalton commented 5 years ago

I now seem to be getting this error for some users. I definitely have the topic set correctly, APNS_USE_SANDBOX = False and Debug = False. Is there anything else I can check?

RaD commented 5 years ago

The same error on Python 3.6 + App 1.6.1

rubnov commented 4 years ago

Same error, Python 3.6.7 + django-push-notifications 1.6.1

/usr/local/lib/python3.6/site-packages/push_notifications/apns.py in apns_send_message(registration_id, alert, application_id, certfile, **kwargs)
    118                         device.active = False
    119                         device.save()
--> 120                 raise APNSServerError(status=reason_for_exception_class(apns2_exception.__class__))
    121
    122

APNSServerError: BadDeviceToken

Both on STAGING and PROD environment. Regenerated and tested our .pem certificate as per these instructions, made sure the device is posting a fresh token to the server, but still getting this error.

Configuration on STAGING:

{'APNS_CERTIFICATE': '/code/gopublic/cert/apn_dev.pem',
 'APNS_HOST': 'gateway.push.apple.com',
 'APNS_TOPIC': <bundle_id>,
 'APNS_USE_ALTERNATIVE_PORT': False,
 'APNS_USE_SANDBOX': True,
 'CONFIG': 'push_notifications.conf.LegacyConfig',
 'FCM_API_KEY': <key>,
 'FCM_ERROR_TIMEOUT': None,
 'FCM_MAX_RECIPIENTS': 1000,
 'FCM_POST_URL': 'https://fcm.googleapis.com/fcm/send',
 'GCM_API_KEY': <key>,
 'GCM_ERROR_TIMEOUT': None,
 'GCM_MAX_RECIPIENTS': 1000,
 'GCM_POST_URL': 'https://android.googleapis.com/gcm/send',
 'UPDATE_ON_DUPLICATE_REG_ID': False,
 'USER_MODEL': 'users.User',
 'WNS_ACCESS_URL': 'https://login.live.com/accesstoken.srf',
 'WNS_PACKAGE_SECURITY_ID': None,
 'WNS_SECRET_KEY': None,
 'WP_CLAIMS': None,
 'WP_ERROR_TIMEOUT': None,
 'WP_POST_URL': {'CHROME': 'https://fcm.googleapis.com/fcm/send',
  'FIREFOX': 'https://updates.push.services.mozilla.com/wpush/v2',
  'OPERA': 'https://fcm.googleapis.com/fcm/send'},
 'WP_PRIVATE_KEY': None}

Has anyone figured this out yet?

wajidakhterabbasi commented 4 years ago

For me I had the same issue, I was using token based authentication and the error was regarding device tokens. Make sure your application setup is correct. Also you can try with use_sandbox flag as false. Sometimes the apps even on the test env are getting the tokens from the live account

russellhoff commented 1 year ago

What if you try to send a notification to a registration_id which doesn't exist?

russellhoff commented 1 year ago

I got the same error, BadDeviceToken, when trying to send push notifications to ios devices (APNS). I thought registration_ids didn't exist or wasn't registered in firebase, but that's not true.

I've tried to send a test message thu Firebase to a workmate's device and the push notification was received.

So, the problem should be configuration's. Which I don't know how to solve...

keyhanasga commented 7 months ago

I had this same issue and in my case, I was something like this:

[
...
"APNS_USE_SANDBOX": os.environ.get('APNS_USE_SANDBOX', False),
...
]

and the problem was that I was setting APNS_USE_SANDBOX to False in Heroku, and os.environ.get('APNS_USE_SANDBOX', False) was returning False as a String, and not a Boolean.

I'm not a professional developer so I don't know whether django-push-notifications should handle this or it's a pretty standard thing.