无法发送iOS推送通知-400 BadDeviceToken响应

时间:2020-06-07 09:12:07

标签: apple-push-notifications

我已经在Mac终端中使用以下命令生成了用于开发的.pem文件:

openssl x509 -inform der -outform pem -in aps_development.cer -out PushNotificationAppCertificate.pem
openssl pkcs12 -in PushNotification.p12 -out PushNotificationAppKey.pem -nocerts
cat PushNotificationAppCertificate.pem PushNotificationAppKey.pem > PushNotificationAppCertificateKey.pem

在服务器上,我使用PHP:

$curl = curl_init();

$device_token   = "oVVEyzOm8LYn1nEYQLUcp1nBQw+UKHUeJbVHxcqr3ls=";
$pem_file       = "PushNotificationAppCertificateKey.pem";
$pem_secret     = "<password>";
$apns_topic     = "<myapp.id>";    

$sample_alert = '{"aps":{"alert":"hi","sound":"default"}}';
$url = "https://api.development.push.apple.com/3/device/".urlencode($device_token);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

但是,BadDeviceToken是响应,我没有在设备上收到通知。

根据Apple文档,设备令牌无效,或者我正在使用开发服务器的生产证书,反之亦然;这里都不是这样。 我该如何工作?

1 个答案:

答案 0 :(得分:0)

我解决了。

问题出在设备令牌上-如果您知道设备令牌的外观,您会立即知道它的获取方式不正确。不应包含=或+字符,并且长度应为64个字符。

在我的RegisteredForRemoteNotifications()函数(Xamarin.iOS)中,我使用了

deviceToken.GetBase64EncodedString(NSDataBase64EncodingOptions.None)

函数以字符串形式获取令牌。但是正确的程序是

byte[] bytes = deviceToken.ToArray<byte>();
string[] hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
string tokenString = string.Join(string.Empty, hexArray);
相关问题