【问题标题】:Forbidden access when connecting to Apple Push Notification service连接到 Apple Push Notification 服务时禁止访问
【发布时间】:2016-03-30 17:09:14
【问题描述】:

在尝试使用 APN 向我的 iPhone 推送通知时,我遇到了一些故障排除。我使用的 PHP 代码在 Internet 上一遍又一遍地看到,所以我认为问题不在于它:

<?php

// Put your device token here (without spaces):
$deviceToken = 'mytoken'; // Of course this is my correct token here

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

$message = $argv[1];
$url = $argv[2];

if (!$message || !$url)
    exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");

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

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', __DIR__.'/cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
  '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',
  'link_url' => $url,
);

// 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;

stream_set_blocking($fp, 0);

// 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);

我有输出“消息成功传递”,所以连接已经成功建立,发送消息同上。但是,设备没有收到任何东西。

我使用反馈功能试图知道哪里出了问题,但结果是空的(数组实际上是空的):

<?php

function send_feedback_request() {
    //connect to the APNS feedback servers
    //make sure you're using the right dev/production server & cert combo!
    $stream_context = stream_context_create();
    stream_context_set_option($stream_context, 'ssl', 'local_cert', __DIR__.'/cert3.pem');
    stream_context_set_option($stream_context, 'ssl', 'passphrase', 'meteor0405');

    $apns = stream_socket_client('ssl://feedback.sandbox.push.apple.com:2196', $errcode, $errstr, 60, STREAM_CLIENT_CONNECT, $stream_context);
    if(!$apns) {
        echo "ERROR $errcode: $errstr\n";
        return;
    }


    $feedback_tokens = array();
    //and read the data on the connection:
    while(!feof($apns)) {
        $data = fread($apns, 38);
        if(strlen($data)) {
            $feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data);
        }
    }
    fclose($apns);
    return $feedback_tokens;
}

var_dump(send_feedback_request());

您知道我可以做些什么来找出问题所在吗?会不会是证书的原因?已从 Apple 网站生成了一个 .cer 文件,并将其导入 Keychain Access 以制作一个 .p12 文件。然后,借助 openssl 命令,我将其转换为 .pem 格式,这是我在以下文件中使用的格式。

谢谢!

【问题讨论】:

    标签: php ios notifications apple-push-notifications apn


    【解决方案1】:

    如果您收到成功消息,则表示您的 PHP 代码正确,但 pem 文件中缺少某些内容。
    在此处查看 pem 文件创建步骤https://www.digicert.com/ssl-support/pem-ssl-creation.htm,然后重试。
    希望会有帮助的。

    【讨论】:

    • 确实有帮助,谢谢!问题确实来自 PEM 文件。我为遇到相同问题的任何人发布了问题的答案。
    【解决方案2】:

    问题确实来自我的 PEM 文件。对于任何有同样问题的人,这就是我所做的。

    从 iPhone 开发者连接门户下载 .p12 格式的证书和私钥后,我必须将它们转换为 .pem 格式。之后,我不得不合并这两个文件(先放证书的内容,然后放私钥)。这个合并必须放在一个新的 .pem 文件中,这是在 PHP 脚本中使用的文件。

    这是我遵循的完整教程:https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/

    此外,PHP 代码并不完全相同。我在第一篇文章中使用的不正确,请改用教程中的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 2017-08-25
      • 1970-01-01
      相关资源
      最近更新 更多