在过去的一周里,我一直在用头撞墙,试图完成这项工作。 我刚刚从头开始并记录了我所做的一切,但它仍然不起作用。 以下是我采取的步骤:
1)使用新的捆绑ID创建一个新的应用程序ID标识符(旧的没有推送通知)
a) made sure push notifications were added to the app id and proceeded to the last step, this resulted in an app id with push notifications in the ‘configurable’ state.
b) I then went to edit the app id and saw that i could create a new production or development certificate so i did both using the steps listed
2)创建了两个新的配置文件,一个用于开发,一个用于生产,两者都使用新的捆绑包 ID
3) 将新的配置文件导入 Xcode 并确保在构建设置中选择了这些配置文件
4) 使用钥匙串创建 .p12 文件并导出 .cer 文件以用于开发和生产
5) 使用以下命令从 .p12 文件创建 .pem 文件:
openssl pkcs12 -in VX-Mobile-Dev.p12 -out apns-dev.pem -nodes -clcerts
openssl pkcs12 -in VX-Mobile-Prod.p12 -out apns-prod.pem -nodes -clcerts
6) 接下来创建了一个 php 脚本(我公然从另一个博客中抄袭了它),我从我的一个虚拟机的命令行运行该脚本。 我将 pans-prod.pem 文件放在与该文件相同的文件夹中。 这是该文件的内容
注意,对于设备令牌,我只是从 Xcode 运行该应用程序,它已经成功连接到 apns 服务器并获取一个设备令牌,该令牌已注销到控制台,出于测试目的,我只是将新令牌复制到每个文件中时间。
文件内容:
// set time limit to zero in order to avoid timeout
set_time_limit(0);
// charset header for output
header('content-type: text/html; charset: utf-8');
// this is the pass phrase you defined when creating the key
$passphrase = '{passphrase is hidden to you!}';
// you can post a variable to this string or edit the message here
if (!isset($_POST['msg'])) {
$_POST['msg'] = "Notification message here!";
}
// function for fixing Turkish characters
function tr_to_utf($text) {
$text = trim($text);
$search = array('Ü', 'Þ', 'Ð', 'Ç', 'Ý', 'Ö', 'ü', 'þ', 'ð', 'ç', 'ý', 'ö');
$replace = array('Ü', 'Åž', 'Ğž', 'Ç', 'İ', 'Ö', 'ü', 'ÅŸ', 'ÄŸ', 'ç', 'ı', 'ö');
$new_text = str_replace($search, $replace, $text);
return $new_text;
}
// tr_to_utf function needed to fix the Turkish characters
$message = tr_to_utf($_POST['msg']);
// load your device ids to an array
$deviceIds = array(
'97c8c0a77e1380d052d17352e33b1f2224f082a3436ee90310cf88c7278329e7'
);
// this is where you can customize your notification
$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';
$result = 'Start' . "\r\n";
////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-prod.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
echo count($deviceIds) . " devices will receive notifications.\r\n";
foreach ($deviceIds as $item) {
// wait for some time
sleep(1);
// 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" . "\r\n");
} else {
echo 'Apple service is online. ' . "\r\n";
}
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
echo 'Undelivered message count: ' . $item . "\r\n";
} else {
echo("\r\nRESULT\r\n\r\n");
var_dump($result);
echo("\r\n\r\n");
echo 'Delivered message count: ' . $item . "\r\n";
}
if ($fp) {
fclose($fp);
echo 'The connection has been closed by the client' . "\r\n";
}
}
echo count($deviceIds) . " devices have received notifications.\r\n";
// set time limit back to a normal value
set_time_limit(30);
请帮忙。
事实证明我就快到了。 唯一的问题是我应该使用沙箱端点。
ssl://gateway.sandbox.push.apple.com:2195
另外,为了回应 RJV Kumar,一旦我让它工作起来,我就用我的产品和开发证书进行了尝试,并且在这两种情况下都工作了。 这对我来说似乎很可疑,但无论如何我很高兴它能起作用。
[更新]
虽然这篇文章中的步骤在技术上是正确的,但我了解到,如果您确实希望能够发布应用程序的新版本,则不应创建新的应用程序 ID。 您应该简单地将推送通知添加到您当前的应用程序 ID,而不是这样做。