Yii 1.x IOS推送

獲得IOS的pem文件後 可以對指定的用戶進行推送 IOS的具體推送原理就不詳細贅述了 直接上實現代碼 Windows系統要注意pem文件的路徑


<span style="white-space:pre">	</span>$deviceToken = '';//硬件token
        
        // Put your private key's passphrase here:
        $passphrase = '';

        // Put your alert message here:
        if($type==Constant::MESSAGE_TYPE_GOODS)
            $message = "'$senderNickName'評論了您的商品('$title'):'$contents'";
        else{
            $message = "'$senderNickName'評論了您的求購('$title'):'$contents'";
        }
        ////////////////////////////////////////////////////////////////////////////////

        $ctx = stream_context_create();
        $path=ROOT_PATH.'/protected/config/ck.pem';
        stream_context_set_option($ctx, 'ssl', 'local_cert',$path);
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

        // Open a connection to the APNS server
        $fp = stream_socket_client(
                'ssl://gateway.sandbox.push.apple.com', $err,
                $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
                exit("Failed to connect: $err $errstr" . PHP_EOL);

        $report= '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)
                $report.='Message not delivered' . PHP_EOL;
        else
                $report.='Message successfully delivered' . PHP_EOL;
        
        $report.=$message;
        // Close the connection to the server
        fclose($fp);
        return $report;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章