生活随笔
收集整理的這篇文章主要介紹了
php使用个推
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,弄清楚概念
透傳:只用把服務器使用API發送的數據推到個推服務器,不管客戶端的情況(手機黑屏,或者app退出后臺),這時候客戶端可以拿到數據并且自己處理.
分為用戶有感知透傳(通知欄)和無感知透傳(微信朋友圈中經常出現的小紅點)
通知:直接把用戶的消息在通知欄展示出來,客戶端無法進行相應的處理操作
APN:我理解的就是蘋果手機都是用這種傳輸的
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
使用app推送的6中情況
1.手機黑屏,app不在后臺運行2.手機黑屏,app在后臺運行3.手機不黑屏,app不在后臺運行4.手機不黑屏,app在后臺運行5.手機黑屏,app正在運行(打開界面)6.手機不黑屏,app正在運行(打開當前app的界面)
除了第6種使用的是set_transmissionContent,其他的都使用了 apn
代碼
composer下載個推的文件夾放到如下文件夾下面
在控制器中創建如下代碼(支持安卓和IOS)
<?phpnamespace Service\Controller;
use Base\Webbase;class GetuiController extends Webbase {private $host;private $appkey;private $appid;private $mastersecret;public function __construct(){//http的域名$this->host = 'http://sdk.open.api.igexin.com/apiex.htm';//定義常量, appId、appKey、masterSecret 采用本文檔 "第二步 獲取訪問憑證 "中獲得的應用配置$this->appkey = '你自己的appkey ';$this->appid = '你自己的appid ';$this->mastersecret = '你自己的mastersecret ';}//群推接口案例public function pushMessageToApp($users,$listId){vendor("getuilaboratory.getui-pushapi-php-client.IGt#Push");$igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret);//消息模版:$template = $this->IGtNotificationTemplateDemo($listId);// IOS8.2 支持$apn = new \IGtAPNPayload();$alertmsg=new \DictionaryAlertMsg();$alertmsg->body= $listId['msg'];$alertmsg->actionLocKey="actionLocKey";$alertmsg->locKey='locKey';$alertmsg->locArgs=array("locargs");$alertmsg->launchImage="launchimage";$alertmsg->title=$listId['title'];$alertmsg->titleLocKey="titleLocKey";$alertmsg->titleLocArgs=array("TitleLocArg");$apn->alertMsg=$alertmsg;$apn->badge=1;$apn->add_customMsg("payload",$listId['id']);$apn->contentAvailable=1; //推送直接帶有透傳數據$apn->category="ACTIONABLE";$template->set_apnInfo($apn);//定義"SingleMessage"
// $message = new \IGtSingleMessage(); //單個用戶推送$message = new \IGtListMessage(); //多個用戶推送$message->set_isOffline(true);//是否離線$message->set_offlineExpireTime(3600*12*1000);//離線時間$message->set_data($template);//設置推送消息類型$contentId = $igt->getContentId($message);
// //接收方foreach ($users as $Alias){$target = new \IGtTarget();$target->set_appId($this->appid);$target->set_alias($Alias);$targetList[] = $target;}try {//$rep = $igt->pushMessageToSingle($message, $target);$rep = $igt->pushMessageToList($contentId, $targetList);var_dump($rep);echo ("<br><br>");}catch(RequestException $e){$requstId = $e.getRequestId();//失敗時重發$rep = $igt->pushMessageToSingle($message, $target,$requstId);var_dump($rep);echo ("<br><br>");}}public function IGtNotificationTemplateDemo($listId){// $template = new \IGtNotificationTemplate();$template = new \IGtTransmissionTemplate(); //**4個模板的類型一定要弄清楚** 上面的個推會發送兩條$template->set_appId($this->appid); //應用appid$template->set_appkey($this->appkey); //應用appkey$template->set_transmissionType(1); //透傳消息類型$listId = json_encode($listId,JSON_UNESCAPED_UNICODE);$template->set_transmissionContent($listId);//透傳內容return $template;}
總結
以上是生活随笔為你收集整理的php使用个推的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。