苹果推送PHP代码
代码如下:
<?php
/**
* apple推送
* p12转换成pem命令:openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes
*
* @author kevinxie
* @date 2020/04/30
*/
class Lib_Applepush
{
protected static $_instance = array();
public function __construct()
{
}
/**
* 对像生产器
*
* @return object
*/
public static function factory()
{
if ( !isset( self::$_instance[__METHOD__] ) || !is_object( self::$_instance[__METHOD__] ) )
{
self::$_instance[__METHOD__] = new self();
}
return self::$_instance[__METHOD__];
}
/**
* 苹果推送-单条
*
* @param string $deviceToken
* @param array $arrContent 推送内容 array('content'=>string, 'payload'=>array())
* @param bool $isFormal true:正式 false:测试
* @return bool
*/
public function pushOne( $deviceToken, $arrContent, $isFormal=true)
{
//推送目标设备号(测试环境和正式环境不一样)
if( $deviceToken == '')
{
return false;
}
if( $isFormal == true)
{
//证书路径
$pem = APP_WWWROOT . 'config/apple/apple_push_formal.pem';
//正式服务器
$apnsHost = 'ssl://gateway.push.apple.com:2195';
}
else
{
//证书路径
$pem = APP_WWWROOT . 'config/apple/apple_push_test.pem';
//测试服务器
$apnsHost = 'ssl://gateway.sandbox.push.apple.com:2195';
}
if( $arrContent['content'] == '')
{
return false;
}
if(( ! isset( $arrContent['payload'])) || ( ! is_array( $arrContent['payload'])))
{
$arrContent['payload'] = array();
}
$content = $arrContent['content'];
$body = array("aps" => array("alert" => $content,"badge" => 5,"sound"=>'default'),'payload'=> $arrContent['payload']);
$ctx = stream_context_create();
stream_context_set_option($ctx,"ssl","local_cert",$pem);
$pass = 'apple push pass'; //如果有密码的话
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client($apnsHost, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if ( ! $fp)
{
return false;
}
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
fwrite($fp, $msg);
fclose($fp);
return true;
}
/**
* 苹果推送-多条
*
* @param array $arrParam array(array('deviceToken'=>string, 'content'=>string, 'payload'=>array()))
* @param bool $isFormal true:正式 false:测试
* @return bool
*/
public function pushMany( $arrParam, $isFormal=true)
{
if( ! is_array( $arrParam))
{
return false;
}
if( $isFormal == true)
{
//证书路径
$pem = APP_WWWROOT . 'config/apple/apple_push_formal.pem';
//正式服务器
$apnsHost = 'ssl://gateway.push.apple.com:2195';
}
else
{
//证书路径
$pem = APP_WWWROOT . 'config/apple/apple_push_test.pem';
//测试服务器
$apnsHost = 'ssl://gateway.sandbox.push.apple.com:2195';
}
$ctx = stream_context_create();
stream_context_set_option($ctx,"ssl","local_cert",$pem);
$pass = 'apple push pass'; //如果有密码的话
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client( $apnsHost, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if ( ! $fp)
{
return false;
}
foreach((array)$arrParam as $key => $value)
{
//推送目标设备号(测试环境和正式环境不一样)
if( $value['deviceToken'] == '')
{
return false;
}
if( $value['content'] == '')
{
return false;
}
if(( ! isset( $value['payload'])) || ( ! is_array( $value['payload'])))
{
$value['payload'] = array();
}
$content = $value['content'];
$body = array("aps" => array("alert" => $content,"badge" => 5,"sound"=>'default'),'payload'=> $value['payload']);
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $value['deviceToken'])) . pack("n",strlen($payload)) . $payload;
fwrite($fp, $msg);
}
fclose($fp);
return true;
}
}
?>
推送单条:
$deviceToken = '用户TOKEN';
$arrContent = array('title'=>'title x1', 'body'=>'body x1', 'data'=>array('参数名'=>'值'));
Lib_Googlepush::factory()->pushOne( $deviceToken, $arrContent);
批量推送:
$arrParam = array(
array('deviceToken'=>'用户token','content'=>'hello apple push !','payload'=>array(1=>2)),
array('deviceToken'=>'用户token','content'=>'hello apple push 2!','payload'=>array(1=>2)),
array('deviceToken'=>'用户token','content'=>'hello apple push 3!','payload'=>array(1=>2)),
);
Lib_Applepush::factory()->pushMany( $arrParam);