首页 >后端开发 >php教程 >ios推送消息php做推送服务器

ios推送消息php做推送服务器

WBOY
WBOY原创
2016-06-23 13:54:21801浏览

    /**
     * Main method to run the object
     * $message 消息内容
     * $deviceToken  这里是iphone手机唯一的Token码(记得去掉空格)
     * $badge  就是应用图标右上角那个数字
     * $sound  消息的声音
     * $apnsCert 证书路径
     * $passphrase  私钥的密码(可以不写)
     */
    public function iosPush($message,$deviceToken,$badge=1,$sound='Duck.wav',$apnsCert,$passphrase){
        $body['aps'] = array('alert' => $message);
        if ($badge)
          $body['aps']['badge'] = $badge;
        if ($sound)
          $body['aps']['sound'] = $sound;
        $payload = json_encode($body);
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert);
        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, $ctx);
        if (!$fp) {
            print_r();
            print "Failed to connect $err $errstr\n";
            return;
        }
        else {
           print $message;
           print "Connection OK\n
";
        }
        $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
        print "Sending message :" . $payload . "\n";
        fwrite($fp, $msg);
        fclose($fp);

    }


?>

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn