我的应用在App Store上,推送通知无法正常工作。
在开发环境下,我的应用可以正常推送通知。我猜应该是我没有通过Ad Hoc部署进行测试。这是我所了解到的情况:
应用ID
我的应用ID是com.MyName.My-App。
已启用开发和发布的推送通知功能。
APNs证书
我有开发和发布证书,但我关心的是发布证书。
证书名称为com.MyName.My-App。
导出PEM格式文件
我选择了证书和私钥,并将其导出为PEM格式文件:
并设置了密码保护。
然后运行:
openssl pkcs12 -in Certificates.p12 -out pushcert.pem -nodes -clcerts
输入密码,并成功获取pushcert.pem
输出。
下载应用程序
我清除了我的设备的服务器端设备令牌,从应用商店下载我的应用程序,打开它并接受推送通知,然后登录到我的服务器以检查我的设备令牌。现在我有了我的生产设备令牌。我运行了这个简单的php脚本(当我提供我的开发设备令牌时它有效),但是使用我的生产设备令牌失败。
<?php
// Put your device token here (without spaces):
$deviceToken = 'myProductionDeviceTokenInHere';
// Put your private key's passphrase here:
$passphrase = 'myPasswordIsInHere';
// Put your alert message here:
$message = 'Test';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
为什么会失败?使用我的开发设备令牌可以正常工作,但使用生产设备令牌却不行。我有没有做错什么?