Home >Backend Development >PHP Tutorial >ios推送消息php做推送服务器

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

WBOY
WBOYOriginal
2016-06-23 13:54:21794browse

    /**
     * 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);

    }


?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn