IOS推送消息,PHP作为推送服务器进行分批推送

 

IOS 推送消息是许多 IOS 应用都具备的功能,之前我们已经做了 IOS 推送消息的准备工作 -- 搭建 APNS 环境 ,到这里证书已经准备完毕, 在应用服务器采用php 的方式将消息推送给APNS

 

<?php
// 这里是我们上面ios工程连接苹果服务器服务器返回的一个deviceToken,直接复制过来(记得去掉空格)
$deviceToken = '2cca7dff6e086e876c128ee5a60521527b2cc2fed923cb708335ca8ee2b7027b';
// Put your private key’s passphrase here:
$passphrase = 'abc123456';
// Put your alert message here:
$message = ‘My first push test!’;
////////////////////////////////////////////////////////////////////////////////
$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.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
//这个是沙盒测试地址,发布到appstore后记得修改哦
$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’
);
// 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);
?>





 

 

接下来我们访问http://localhost/push/push.php

 

iphone 就会接收到一条推送消息了,如果有问题的话就检查上面的操作步骤,

注意:进行APNS 推送沙盒消息测试的地址的devictoken 和发布正式appstoredevicetoken 是不同的,由于苹果服务器接收推送消息一次只可以接收7000 字节,每次发送消息又必须少于256 字节,所以我们对于多用户推送消息必须解决这一难题,则采用分批发送。

 

 

//进行IOS 推送信息

	$rs = DBHelper :: db_likeQuery("devicetokens", array ());
//读取数据库数据统计总条数
	if(pg_num_rows($rs)){
//30一组,可以分成几组
		if(pg_num_rows($rs)%30){
			$device_count = pg_num_rows($rs)/30 + 1;
		}else{
			$device_count = pg_num_rows($rs)/30;
		}
		$device_count = (int)$device_count;
	}
	
	ob_end_clean();
	echo str_pad('', 2048);// 设置足够大,受output_buffering影响
	for($i = 0; $i < $device_count; $i ++){

		$passphrase = 'anhuioss-avene';
		$pem_path = 'pro_ck.pem';
		//$sslclient = 'ssl://gateway.sandbox.push.apple.com:2195';
//这个是沙盒测试地址,发布到appstore后记得修改哦
		$sslclient = 'ssl://gateway.push.apple.com:2195';
//正式发布appstore后的地址
		$fp = connectAPSN($sslclient,$pem_path,$passphrase);
		//PUSH内容設定

		$message = “IOS push推送消息测试!”;

		//PUSH内容をJSON化
		$size = 0;

		$array = array();
		$j = $i * 30;
		//进行分批发送,每30人为一组
		$link = DBHelper::db_pageQueryByTimeasc('devicetokens',$array, $j, 30,'id');
			if(pg_num_rows($link)){
				$devi = pg_fetch_all($link);
					foreach ($devi as $key => $value) {

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

							//$payload = json_encode($body);
//由于转换json格式字节码转换一个汉字占4个字节,所以选择了直接使用json格式的数据类型,这样的话减少字节的转换,
							$payload = '{"aps":{"alert":"'.$message.'","sound":"default","badge":"1"}}';
							$json_size = strlen($payload);

							$msg = chr(0) . pack('n', 32) . pack('H*', trim($value['token'])) . pack('n', $json_size) . $payload;
						    $msg_size = strlen($msg);
						    $result = fwrite($fp, $msg, $msg_size);
						    if (!$result){
						        echo 'Message not delivered' . PHP_EOL . '<br>';
						        fclose($fp);
						        sleep(1);
						        $fp = connectAPSN($sslclient,$pem_path,$passphrase);
						        
						        $msg = chr(0) . pack('n', 32) . pack('H*', trim($value['token'])) . pack('n', $json_size) . $payload;
							    $msg_size = strlen($msg);
							    $result = fwrite($fp, $msg, $msg_size);
							    
						    }else{
						        echo 'Message successfully delivered' . PHP_EOL .'<br>';
						    }
						    fclose($fp);
						    sleep(1);
					}
					echo 30+$j."人发送成功!<br><br>";
					fclose($fp);
					sleep(10);
			}else{

			}
	}


	function connectAPSN($sslclient,$pem_path,$passphrase){
		$ctx = stream_context_create();
		stream_context_set_option($ctx, 'ssl', 'local_cert', $pem_path);
		stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
		$fp = stream_socket_client($sslclient, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
		
		if (!$fp){
			echo "Failed to connect: $err.$errstr" . PHP_EOL . "<br>";
			sleep(1);
			$fp = connectAPSN($sslclient,$pem_path,$passphrase);
			return $fp;
		}else{
			echo "AConnected to APNS" . PHP_EOL . "<br>";
			return $fp;
		}
	 
	}





 IOS push测试效果图


你可能感兴趣的:(JavaScript,Ajax)