【PHP】iOS推播通知以及反饋服務

來源:互聯網
上載者:User

標籤:android   style   blog   color   os   使用   io   for   ar   

近來項目是完成一個PHP的推送伺服器,無論是PHP,APNs還是GCM基本上都是從零開始。

寫下一點見解,方便以後繼續做代碼的搬運工。

因為對PHP跟iOS都不熟悉,可能有錯漏。。。窮孩子沒有用過iOS的東西。。。

 

裝置如果希望能夠及時收到伺服器的訊息,大概有三種方式:

1)輪詢(Pull)方式:用戶端與伺服器主動串連查詢。因為及時性以及耗電量等要求不可得兼,一般不考慮。

2)SMS(Push)方式:在Android平台,可以通過攔截SMS訊息並且解析訊息內容來瞭解伺服器的意圖,並擷取其顯示內容進行處理。但是這個方案的成本相對較高,需向移動公司繳納費用

3)持久串連(Push)方式:在Android上有GCM,設定可以跑自己的背景程式連結自己的伺服器。在iOS上就限定了只能使用蘋果自身的APNs(Apple Push Notification Service)

 

【PHP實現APNs的推播通知】

準備好pem檔案之後,php方面發送推播通知的代碼如下:

private static function pushIOS($deviceToken=‘‘,$message=‘‘){        $body = array("aps" => array("alert" => $message,"badge" => 2,"sound"=>‘default‘));  //推送方式,包含內容和聲音        $ctx = stream_context_create();        //如果在Windows的伺服器上,尋找pem路徑會有問題,路徑修改成這樣的方法:        $pem = dirname(__FILE__) . ‘/‘ . ‘apns-dev.pem‘;        stream_context_set_option($ctx,"ssl","local_cert",$pem);        //linux 的伺服器直接寫pem的路徑即可        //stream_context_set_option($ctx,"ssl","local_cert","apns-dev.pem");        $pass = "1234";        stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass);        //must select between two servers:if for testing,select sandbox server and Dev pem file ; if for relese,use Product pem and server        //$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);        $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);        if (!$fp)         {            echo "Failed to connect $err $errstrn";            return;        }        print "Connection OK\n";        $payload = json_encode($body);        $msg = chr(0) . pack("n",32) . pack("H*", str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload;        echo "sending message :" . $payload ."\n";        fwrite($fp, $msg);        fclose($fp);    }

 

【PHP實現APNs的Feedback Service】

function send_feedback_request() {    //connect to the APNS feedback servers    //make sure you‘re using the right dev/production server & cert combo!    $stream_context = stream_context_create();    stream_context_set_option($stream_context, ‘ssl‘, ‘local_cert‘, ‘/path/to/my/cert.pem‘);    $pass = "1234";    stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass);        $apns = stream_socket_client(‘ssl://feedback.push.apple.com:2196‘, $errcode, $errstr, 60, STREAM_CLIENT_CONNECT, $stream_context);//如果是開發中的話,地址應該是:feedback.sandbox.push.apple.com:2196    if(!$apns) {        echo "ERROR $errcode: $errstr\n";        return;    }    $feedback_tokens = array();    //and read the data on the connection:    while(!feof($apns)) {        $data = fread($apns, 38);        if(strlen($data)) {            $feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data);        }    }    fclose($apns);    return $feedback_tokens;}

順利的話,這個方法的傳回值應該是如下形式:

Array(    Array    (        [timestamp] => 1266604759        [length] => 32        [devtoken] => abc1234..............etcetc    ),    Array    (        [timestamp] => 1266604922        [length] => 32        [devtoken] => def56789..............etcetc    ),)

蘋果的尿性,一點都不人性化。Google的GCM每次推送過去都會返回每條訊息的發送結果,如果裝置已經卸載了應用導致id不再可用也馬上可以看到,群發訊息也支援一千條一千條地發送。

蘋果的只能一條條發送,並且調用這個不知所謂的反饋服務。

果粉看到這裡可以能罵人,不過我是果黑,不管。

【PHP】iOS推播通知以及反饋服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.