jazzband / django-push-notifications

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

AttributeError: 'unicode' object has no attribute 'create_connection' #420

Open sorter opened 7 years ago

sorter commented 7 years ago

Hello,

I've been using django-push-notifications without issue for some time now without configuration change. Upgrading to 1.5.0 appears to introduce a breaking change with the apns2 library.

Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "/tmp/djangodev/local/lib/python2.7/site-packages/push_notifications/models.py", line 159, in send_message
    **kwargs
  File "/tmp/djangodev/local/lib/python2.7/site-packages/push_notifications/apns.py", line 113, in apns_send_message
    certfile=certfile, **kwargs
  File "/tmp/djangodev/local/lib/python2.7/site-packages/push_notifications/apns.py", line 66, in _apns_send
    client = _apns_create_socket(certfile=certfile, application_id=application_id)
  File "/tmp/djangodev/local/lib/python2.7/site-packages/push_notifications/apns.py", line 38, in _apns_create_socket
    use_alternative_port=get_manager().get_apns_use_alternative_port(application_id)
  File "/tmp/djangodev/local/lib/python2.7/site-packages/apns2/client.py", line 41, in __init__
    self._init_connection(use_sandbox, use_alternative_port, proto)
  File "/tmp/djangodev/local/lib/python2.7/site-packages/apns2/client.py", line 50, in _init_connection
    self._connection = self.__credentials.create_connection(server, port, proto)

Inserting some debug print statements reveals that self.__credentials is the absolute path to my APNS certificate.

I will also note that django-push-notifications==1.4.1 doesn't have this issue, the issue is with 1.5.0

Has anyone else run into this?

Kiirojin commented 7 years ago

I'm running into same issue. All circumstances seem to be consistent with the above post. Happens on 1.5.0 and not 1.4.1.

jamaalscarlett commented 7 years ago

@matthewh @kit-cat I see a lot of apns related issues related to 1.5.0. Has there been some upgrade to apns2 that has broken functionality in this package?

Kiirojin commented 7 years ago

This is just because the APNsClient call in apns2 doesn't support unicode strings for whatever reason. Calling .encode('utf-8') on the string passed into the APNsClient call fixes the issue.

carn1x commented 7 years ago

Confirmed, fixed by forcefully providing a bytestring for the certificate path b'/path/to/file.pem'

mhsiddiqui commented 6 years ago

I am getting this error in 1.6 version. Is it not fixed yet?

matthewh commented 6 years ago

@mhsiddiqui No changes have to made to the codebase for this issue. Did you try the workarounds suggested by @Kiirojin or @carn1x?

mhsiddiqui commented 6 years ago

I converted my apns token path to string in my settings.py file (which was unicode before this) and the error then I got is "APNSServerError: BadDeviceToken". I think this is expected behaviour. I was not getting this error when token has expired in 1.4.1 version. I think I had to put a try catch statement now.