苹果推送通知 2021

时间:2021-06-29 09:16:02

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

几天前,我的苹果推送通知证书过期了。我更新了它,但推送通知没有通过。这段代码当时适用于我的证书。 现在新证书不适用于此代码..
我在这里错过了什么?

<?php

$deviceToken = 'tokenhere';

$passphrase = 'passphrase here';

$message = 'Received push notification!';

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

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

$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

$payload = json_encode($body);

$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

fclose($fp);

0 个答案:

没有答案
相关问题