question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disconnected From APNS due to Bad Tokens

See original GitHub issue

Hey folks, I’ve run into this issue during my development. I decided to open an issue just in case others run into the same issue.

I did some testing to see how PyAPNs would react to bad tokens. Let’s say there is a token that is invalid (ie. user may have deleted your application, token hex some how got messed up or cut off, wrong token, a token that doesn’t exist, you accidentally sent a push to a production token etc…).

Let’s make an arbitrary token:

token = 'hecbadd4ed53h45b60f53ed79fef30g11ffddca7abffd0a7f90391ec245fhje'

Great, now that we have our ‘bad’ token. Let’s setup the configuration & payload details.

>>> from apns import *
>>> apns = APNs(use_sandbox=True, cert_file='apns-dev-cert.pem', key_file='apns-dev-key') 
>>> payload = Payload(alert = 'hello', sound="default", badge=0)

Next, let’s actually fire off the push notifications.

>>> apns.gateway_server.send_notification(token, payload)
>>> apns.gateway_server.send_notification(token, payload)
>>> apns.gateway_server.send_notification(token, payload)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "apns.py", line 381, in send_notification
    self.write(self._get_notification(token_hex, payload))
  File "apns.py", line 174, in write
    return self._connection().write(string)
  File "/usr/lib/python2.7/ssl.py", line 172, in write
    return self._sslobj.write(data)
socket.error: [Errno 32] Broken pipe

Notice how it didn’t raise any errors until the 3rd push notification was sent. Now if I send a 4th one after that:

>>> apns.gateway_server.send_notification(token, payload)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "apns.py", line 381, in send_notification
    self.write(self._get_notification(token_hex, payload))
  File "apns.py", line 174, in write
    return self._connection().write(string)
  File "/usr/lib/python2.7/ssl.py", line 172, in write
    return self._sslobj.write(data)
ssl.SSLError: [Errno 1] _ssl.c:1296: error:1409F07F:SSL     routines:SSL3_WRITE_PENDING:bad write retry

It seems I have been disconnected by APNS because of the bad token. Any pushes that I try to send to VALID tokens will not work because the socket object that keeps the connection is gone. I think using a try and except block would be nice somewhere.

Once the exception is raised, the user can then remove the invalid token and then reconnect to APNS.

But the problem here is that it only raises the error after the third Push notification. Is there some way to detect a bad token right off the start? and reconnect? It gets pretty bad especially if you have apns declared as a global variable.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
HarrisonJacksoncommented, May 6, 2014

If you catch the exception you can also re-create your apns object

self.apns = APNs(use_sandbox=True, cert_file='dev_cert.pem', key_file='dev_key.pem')
try:
  self.apns.gateway_server.send_notification(token, payload)
except Exception as e:
  self.apns = APNs(use_sandbox=True, cert_file='dev_cert.pem', key_file='dev_key.pem')

You should probably add some checking to see what the exception was before blindly creating a new connection - but this was a simple fix for us.

0reactions
5fcgdaebcommented, Mar 13, 2017

I agree, no error notifications when using send_notification_multiple. Is there a workaround to this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Repeated invalid APNs feedback service notifications
For now, we have disconnected from the APNs feedback service since it appears to be, in these cases only, providing bad feedback.
Read more >
What can cause a connection to APNS to intermittently ...
The problem was caused by sending an invalid device token to the APNS server. ... When an invalid device token is sent to...
Read more >
BadDeviceToken from APNs gateway - Google Groups
I have attempted both authentication methods, TLS and Token with the same response. So, it would seem that there really is a bad...
Read more >
Php – APNS: invalid token (8) - iTecNote
Either you are using an old device token that was sent to your server by your app while you were testing it in...
Read more >
Notifications - Expo Documentation
The expo-notifications provides an API to fetch push notification tokens and to ... where the request itself fails (like due to the device...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found