2

我正在开发移动设备管理系统,但我的推送通知不起作用。我的代码是

//$apnsHost = 'gateway.push.apple.com';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'PushCert.pem';

// Put your device token here (without spaces):
$deviceToken = 'my-device-token'; //I have replaced my actual token here

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

// Put your alert message here:
$message = 'My first push notification!';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
//stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp)
    exit("Failed to connect: $error $errorString" . 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);

我收到类似“连接到 APNS 消息已成功传递”的消息。但通知没有出现在 iPhone 设备上。我还应该检查什么以及解决方案是什么?我还在配置实用程序中检查了 Iphone 日志,但运行脚本时没有看到日志。

4

0 回答 0