发送多个iPhone推送通知+ APNS + PHP(Sending multiple iPhone

2019-07-18 20:17发布

我工作的一个PHP网站+ iPhone应用程序和API的iPhone应用程序,对学生和医生的消息系统,当任何一个发送消息(从网站或iPhone)其他用户应该得到他的iPhone推送通知。 例如,如果学生增加了对教师的新问题,对教师的iPhone / iPad的将被发送到老师和老师时,回复学生的答案,学生将获得一个推送通知推送通知。

由于是对教师和学生注册到网站的数量没有限制,我的问题是如何把消息发送到注册用户的iPhone? 我想尽快发送推送消息有人回复或增加的问题。 用于发送多个推送消息请给我提供的PHP代码。 我的同时登记每个用户节省了设备令牌。

当老师回答问题,我将邮件发送到学生,我想也发送推送通知到学生,反之亦然,所以请指定能够管理错误条件代码。

Answer 1:

简单的方法来做到这一点不使用任何文件。 你可以用不同的tokeid多次调用它。

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ckipad.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 amarnew: $err $errstr" . PHP_EOL);

//echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'badge' => +1,
    'alert' => $message,
    'sound' => 'default'
);

$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 amar'.$message. PHP_EOL;

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


Answer 2:

你应该更好地利用APNS库PHP。 你可以找到它在这里 。 通过看开发商提供样品。

我也有使用证书的问题。 我的行为是:

  1. 找到文件ApnsPHP/Abstract.php
  2. 做一些更改_connect()方法中,粘贴此线

     $streamContext = stream_context_create( array( 'ssl' => array( 'local_cert' => $this->_sProviderCertificateFile, 'passphrase' => '' ) ) ); $this->_hSocket = @stream_socket_client( $sURL, $nError, $sError, $this->_nConnectTimeout, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext); 

    而不是原来的上市有

  3. 现在你可以使用*。质子交换膜证书,而无需entrust_root_certification_authority的。

这对我来说工作得很好。



Answer 3:

这是我终于完成了它的方式

  1. 下载APNS-PHP
  2. PHP代码

  set_time_limit(0); $root_path = "add your root path here"; require_once($root_path."webroot\cron\library\config.php"); require_once($root_path."Vendor\ApnsPHP\Autoload.php"); global $obj_basic; // Basic settings $timezone = new DateTimeZone('America/New_York'); $date = new DateTime(); $date->setTimezone($timezone); $time = $date->format('H:i:s'); //Get notifications data to send push notifications $queueQuery = " SELECT `notifications`.*, `messages`.`mes_message`, `messages`.`user_id`, `messages`.`mes_originated_from` FROM `notifications` INNER JOIN `messages` ON `notifications`.`message_id` = `messages`.`mes_id` WHERE `notifications`.`created` <= NOW()"; $queueData = $obj_basic->get_query_data($queueQuery); if(!empty($queueData)) { // Put your private key's passphrase here: $passphrase = 'Push'; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'server_certificates_bundle_sandbox.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 '<br>'.date("Ymd H:i:s").' Connected to APNS' . PHP_EOL; foreach($queueData as $val) { // Put your device token here (without spaces): $deviceToken = $val['device_token']; // Create message // Get senders name $sql = "SELECT `name` FROM `users` WHERE id =".$val['user_id']; $name = $obj_basic->get_query_data($sql); $name = $name[0]['name']; $message = $name." : "; // Get total unread messaged for receiver $query = "SELECT COUNT(*) as count FROM `messages` WHERE mes_parent = 0 AND user_id = ".$val['user_id']." AND mes_readstatus_doc != 0 AND mes_status = 1"; $totalUnread = $obj_basic->get_query_data($query); $totalUnread = $totalUnread[0]['count']; $message .= " This is a test message."; // Create the payload body $body['aps'] = array( 'alert' => $message, 'badge' => $totalUnread, '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 '<br>'.date("Ymd H:i:s").' Message not delivered' . PHP_EOL; } else { $sqlDelete = "DELETE FROM `notifications` WHERE id = ".$val['id']; $query_delete = $obj_basic->run_query($sqlDelete,'DELETE'); echo '<br>'.date("Ymd H:i:s").' Message successfully delivered' . PHP_EOL; } } // Close the connection to the server fclose($fp); echo '<br>'.date("Ymd H:i:s").' Connection closed to APNS' . PHP_EOL; } else { echo '<br>'.date("Ymd H:i:s").' Queue is empty!'; } 


文章来源: Sending multiple iPhone push notifications + APNS + PHP