已连接到APNS但未发送消息

时间:2016-05-30 14:39:46

标签: php ios apple-push-notifications

我正在尝试在我的应用中实施APNS一周。我设法通过联系我的远程服务器的支持来打开端口2195/6来解决连接超时问题。

现在我的下一个问题是,当我执行我的PHP脚本时,它成功连接到APNS服务器但没有将消息传递给iOS设备。这是我的PHP脚本:

<?php

// Put your device token here (without spaces):
$deviceToken = '---------devicetokenwithnospace-----';

// Put your private key's passphrase here:
$passphrase = 'mypassword';

// Put your alert message here:
$message = "New dispatched load!"; 


////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PushCertificateAndKey.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
  'ssl://gateway.sandbox.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',
  'category' => "NEWS_CATEGORY",
  );

// Encode the payload as JSON
$payload = '{"aps" : { "alert" : { "title" : "My Title", "body" : "My Message" }, "badge" : 2, "sound" : "default" }}';

echo $payload;

// 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));
echo $result;
if (!$result)
  echo 'Message not delivered' . PHP_EOL;
else
  echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

结果将是:Connected to APNS {"aps" : { "alert" : { "title" : "My Title", "body" : "My Message" }, "badge" : 2, "sound" : "default" }}142Message successfully delivered

如您所见,$result变量返回142,我不知道它是什么意思。

我通过使用APN Tester测试了我使用的deviceToken是否正确(它正在运行,我能够收到通知: enter image description here

我现在不知道如何解决这个问题。

0 个答案:

没有答案