iPhone使用apple push很方便,而Android很多廠商刪除了google push,而且google在大陸連不上,所以要用別的辦法。
Android常見的推送服務商有:極光推送(https://www.jpush.cn/)、百度雲推送。
app加入推送的lib,會有一個常駐后台進程,通過推送服務商推送消息,無需啟動app。
百度雲推送官方文檔:http://developer.baidu.com/cloud/push
對於這種第三方庫,PHP官方稱之為PEAR,需要按照PEAR標准開發(標准URI)。
PEAR的優勢:一鍵安裝到php/lib/php目錄,require即可使用,很方便。
百度推送的官方sdk不符合PEAR標准,使用不便,不想為百度重寫,所以我改寫了一下(sdk中帶的很多垃圾沒去改寫),安裝步驟如下:
pear channel-discover sinkcup.github.io/pear pear install sinkcup/Services_Baidu_Push
使用步驟:
1、注冊登錄百度開發者后台,獲得api key和secret key,在app里添加lib。
2、demo(參考https://github.com/sinkcup/Services_Baidu_Push/blob/master/tests/Services/Baidu/PushTest.php)
<?php require_once 'Services/Baidu/Push.php'; $apiKey = 'asdf'; $secretKey = 'qwer'; $c = new Services_Baidu_Push($apiKey, $secretKey); $msg = array( "description"=> "testSimplePushToAndroid群發", "notification_basic_style"=>7, ); $r = $c->simplePushToAndroid($msg); var_dump($r); exit; ?>
輸出:
bool(true)
我的PEAR 頻道:http://sinkcup.github.io/pear/