APNS_CERTIFICATE - 推送通知不会在生产中发送
APNS_CERTIFICATE - Push Notification does not send in production
当我突然停止在生产环境中发送通知时,我已经遇到这个问题大约 2 周了。我正在使用 django-push-notifications 库,通过 django admin 我可以发送一条测试消息,但它不会通过系统发送消息。
在我的本地计算机上,一切正常。我发现了一个测试证书的命令:
openssl s_client -connect gateway.push.apple.com:2195 -cert apns-cert.pem
With this one I had the return: Timeout: 7200 (sec) Verify return
code: 20 (unable to get local issuer certificate) Extended master
secret: yes
所以经过大量研究,我发现我需要把 "CA" 的路径:
openssl s_client -CApath /etc/ssl/certs/ -connect gateway.push.apple.com:2195 -cert apns-cert.pem
Who was taking me to: Verify return code: 0 (ok)
但是,为了在库中使用,我需要输入 .pem 文件的完整路径。然后我找到了这个命令:
ls /etc/ssl/certs/Entrust*
我测试了那里所有的 .pem 文件,直到我达到了看起来完美的效果:
openssl s_client -CAfile /etc/ssl/certs/Entrust.net_Premium_2048_Secure_Server_CA.pem -connect gateway.push.apple.com:2195 -cert apns-cert.pem
很快,我格式化了我的 PUSH_NOTIFICATIONS_SETTINGS:
PUSH_NOTIFICATIONS_SETTINGS = {
"GCM_API_KEY": "xxxx",
"APNS_CERTIFICATE": os.path.join(BASE_DIR, "apns-cert.pem"),
"APNS_CA_CERTIFICATES": "/etc/ssl/certs/Entrust.net_Premium_2048_Secure_Server_CA.pem",
"APNS_ERROR_TIMEOUT": 3,
}
IOS_VERIFY_RECEIPT_API = 'https://buy.itunes.apple.com/verifyReceipt'
ANDROID_VERIFY_RECEIPT_API = 'https://www.googleapis.com/androidpublisher/v2/applications/{packageName}/purchases/subscriptions/{subscriptionId}/tokens/{token}'
不幸的是,它仍然没有发送 PUSH,也没有错误,因为我已经将它配置为弹出错误以通过电子邮件发送。
PS:记住通过 django admin 发送测试文本:好的。通过沙箱发送(调试):OK。
其实这不是 SSL 问题,而是图书馆的批量上传错误。
系统中注册的令牌已过期,图书馆不知道如何使用它并取消了操作,导致没有其他令牌被尝试。我通过将测试发送到我的电子邮件来循环并忽略单个错误来纠正问题:
def send_push(self):
errors = []
# IOS
queryset_ios = APNSDevice.objects.filter(user=self.authentication)
for device in queryset_ios:
try:
device.send_message(self.subject, badge=1, sound=self.kind.sound)
except APNSServerError as e:
errors.append(APNS_ERROR_MESSAGES[e.status])
except Exception:
pass
# ANDROID
queryset_android = GCMDevice.objects.filter(user=self.authentication)
extra = {'notification': self.pk, 'kind': self.kind.kind, 'sound': self.kind.sound}
for device in queryset_android:
try:
queryset_android.send_message(self.subject, badge=1, extra=extra)
except GCMError as e:
errors.append(str(e))
except Exception:
pass
if errors:
send_mail("Push Error",
"Push: %s \n User: %s \n\n Errors: %s" % (self.subject, self.authentication.full_name, errors),
settings.DEFAULT_FROM_EMAIL, ["my@mail.com"])
当我突然停止在生产环境中发送通知时,我已经遇到这个问题大约 2 周了。我正在使用 django-push-notifications 库,通过 django admin 我可以发送一条测试消息,但它不会通过系统发送消息。
在我的本地计算机上,一切正常。我发现了一个测试证书的命令:
openssl s_client -connect gateway.push.apple.com:2195 -cert apns-cert.pem
With this one I had the return: Timeout: 7200 (sec) Verify return code: 20 (unable to get local issuer certificate) Extended master secret: yes
所以经过大量研究,我发现我需要把 "CA" 的路径:
openssl s_client -CApath /etc/ssl/certs/ -connect gateway.push.apple.com:2195 -cert apns-cert.pem
Who was taking me to: Verify return code: 0 (ok)
但是,为了在库中使用,我需要输入 .pem 文件的完整路径。然后我找到了这个命令:
ls /etc/ssl/certs/Entrust*
我测试了那里所有的 .pem 文件,直到我达到了看起来完美的效果:
openssl s_client -CAfile /etc/ssl/certs/Entrust.net_Premium_2048_Secure_Server_CA.pem -connect gateway.push.apple.com:2195 -cert apns-cert.pem
很快,我格式化了我的 PUSH_NOTIFICATIONS_SETTINGS:
PUSH_NOTIFICATIONS_SETTINGS = {
"GCM_API_KEY": "xxxx",
"APNS_CERTIFICATE": os.path.join(BASE_DIR, "apns-cert.pem"),
"APNS_CA_CERTIFICATES": "/etc/ssl/certs/Entrust.net_Premium_2048_Secure_Server_CA.pem",
"APNS_ERROR_TIMEOUT": 3,
}
IOS_VERIFY_RECEIPT_API = 'https://buy.itunes.apple.com/verifyReceipt'
ANDROID_VERIFY_RECEIPT_API = 'https://www.googleapis.com/androidpublisher/v2/applications/{packageName}/purchases/subscriptions/{subscriptionId}/tokens/{token}'
不幸的是,它仍然没有发送 PUSH,也没有错误,因为我已经将它配置为弹出错误以通过电子邮件发送。
PS:记住通过 django admin 发送测试文本:好的。通过沙箱发送(调试):OK。
其实这不是 SSL 问题,而是图书馆的批量上传错误。
系统中注册的令牌已过期,图书馆不知道如何使用它并取消了操作,导致没有其他令牌被尝试。我通过将测试发送到我的电子邮件来循环并忽略单个错误来纠正问题:
def send_push(self):
errors = []
# IOS
queryset_ios = APNSDevice.objects.filter(user=self.authentication)
for device in queryset_ios:
try:
device.send_message(self.subject, badge=1, sound=self.kind.sound)
except APNSServerError as e:
errors.append(APNS_ERROR_MESSAGES[e.status])
except Exception:
pass
# ANDROID
queryset_android = GCMDevice.objects.filter(user=self.authentication)
extra = {'notification': self.pk, 'kind': self.kind.kind, 'sound': self.kind.sound}
for device in queryset_android:
try:
queryset_android.send_message(self.subject, badge=1, extra=extra)
except GCMError as e:
errors.append(str(e))
except Exception:
pass
if errors:
send_mail("Push Error",
"Push: %s \n User: %s \n\n Errors: %s" % (self.subject, self.authentication.full_name, errors),
settings.DEFAULT_FROM_EMAIL, ["my@mail.com"])