相争不足 共享有余

php ios 群发 push apns

PHP moonygao 293℃ 0评论
 $send_push_list=get_ids($token_list,'app_id');
 $deviceToken =$send_push_list;//隐藏了自己的token
    //ck.pem通关密码  
    $pass = '****200825';//最新ios 密码
    //消息内容
    $message['title'] = $msg['title'];
    $message['body'] = $msg['body'];

    //建设的通知有效载荷(即通知包含的一些信息)
    $body = array();
    $body['aps'] = array(
        'alert' => $message,
        'badge'=>6,
        'sound'=>!empty($msg['sound'])?$msg['sound']:''  //default : 系统声音,0或者空:静音  default
    );
    $data['deep_link']=urlencode($data['deep_link']);
    $body['deep_link']=$data['deep_link'];
    $body['other_para']=$data['other_para'];;
    $data['message_id']=$message_id; // 建议最好不要重复
    //把数组数据转换为json数据

    $payload = json_encode($body);
    //echo strlen($payload),"\r\n";
    //下边的写法就是死写法了,一般不需要修改,
    //唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195这个是沙盒测试地址,ssl://gateway.push.apple.com:2195正式发布地址
    $ctx = stream_context_create();
    $pem = dirname(__FILE__) .'/'.'ApnsDev.pem'; //秘钥文件,需要ios开发工具打包生成。

    //echo 'Pem文件:'.$pem.'<br/>';
    //echo 'ios 口令:'.$v.'<br/>';
    stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
    //测试地址
    //$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    //正式地址
    $fp = stream_socket_client('tls://gateway.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    if (!$fp) {
        //print "Failed to connect $err $errstr\n";
        return "Failed to connect $err $errstr";
    }
    else {
        print "Connection OK\n<br/>";
    }

    foreach ($deviceToken as  $k=>$v){     
        // send message 
        $push_msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $v))) . pack("n",strlen($payload)) . $payload;  //循环写入ios设备的app id
        //print "Sending message :" . $payload . "\n";
        fwrite($fp, $push_msg);
    }
    fclose($fp);

参考文档:
http://www.52im.net/thread-345-1-1.html
https://www.oschina.net/translate/http2-protocol-for-apns
http://seme.me/article/19
https://www.cnblogs.com/chen1987lei/archive/2011/05/09/2041090.html
https://blog.csdn.net/weixin_33889245/article/details/94754265
https://segmentfault.com/q/1010000010581760
https://www.jianshu.com/p/b700f0237b0e
https://www.jianshu.com/p/faabce135b20
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW15
https://api.development.push.apple.com/3/device/00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0

转载请注明:IT起义 » php ios 群发 push apns

喜欢 (20)or分享 (0)