Print out APNS tokens in b64 format for ease of use

(imported from commit d38eb1629a599681c42f1f8d817c90229b547516)
This commit is contained in:
Leo Franchi 2014-01-22 11:57:09 -05:00
parent 6903d87809
commit 4dac0e7d37
1 changed files with 7 additions and 6 deletions

View File

@ -40,25 +40,26 @@ def send_apple_push_notification(user, alert, **extra_data):
logging.error("Attempting to send push notification, but no connection was found. This may be because we could not find the APNS Certificate file.") logging.error("Attempting to send push notification, but no connection was found. This may be because we could not find the APNS Certificate file.")
return return
tokens = [b64_to_hex(device.token) for device in b64_tokens = [device.token for device in PushDeviceToken.objects.filter(user=user, kind=PushDeviceToken.APNS)]
PushDeviceToken.objects.filter(user=user, kind=PushDeviceToken.APNS)] tokens = [b64_to_hex(token) for token in b64_tokens]
logging.info("APNS: Sending apple push notification to devices: %s" % (tokens,)) logging.info("APNS: Sending apple push notification to devices: %s" % (b64_tokens,))
message = Message(tokens, alert=alert, **extra_data) message = Message(tokens, alert=alert, **extra_data)
apns_client = APNs(connection) apns_client = APNs(connection)
ret = apns_client.send(message) ret = apns_client.send(message)
if not ret: if not ret:
logging.warning("APNS: Failed to send push notification for clients %s" % (tokens,)) logging.warning("APNS: Failed to send push notification for clients %s" % (b64_tokens,))
return return
for token, reason in ret.failed.items(): for token, reason in ret.failed.items():
code, errmsg = reason code, errmsg = reason
logging.warning("APNS: Failed to deliver APNS notification to %s, reason: %s" % (token, errmsg)) b64_token = hex_to_b64(token)
logging.warning("APNS: Failed to deliver APNS notification to %s, reason: %s" % (b64_token, errmsg))
if code == 8: if code == 8:
# Invalid Token, remove from our database # Invalid Token, remove from our database
logging.warning("APNS: Removing token from database due to above failure") logging.warning("APNS: Removing token from database due to above failure")
PushDeviceToken.objects.get(user=user, token=hex_to_b64(token)).delete() PushDeviceToken.objects.get(user=user, token=b64_token).delete()
# Check failures not related to devices. # Check failures not related to devices.
for code, errmsg in ret.errors: for code, errmsg in ret.errors: