0

这是我第一次在堆栈溢出上发帖,所以如果这个问题非常明显,我深表歉意。

我正在为我的 iOS 应用程序设置推送通知,并且仅使用我自己的设备就成功完成了。我将很快在许多设备上进行测试,但我想知道我是否有正确的想法。

我会将所有设备令牌保留在我的数据库中,当需要发送推送通知时,我只需运行数据库中的所有行(包含令牌)并通过以下脚本发送它们,每次都替换设备令牌并且每次都保持我自己的私钥相同。这个对吗?

提前致谢!

           // Put your device token here (without spaces):
  $deviceToken = $device; // this will be run through a loop that 
                             changes this variable for every new device


   $passphrase = 'XXXXXXXXX'; //This is mine and remains always
                                the same despite which device... correct?

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

  $ctx = stream_context_create();
 stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.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',
'badge' => 1
);

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

0 回答 0