uni-app 使用個推推送系統消息
- 2020 年 7 月 9 日
- 筆記
原文可查看此處 ,搜索 uni-app 使用個推推送系統消息
//mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg3NTAzMzAxNA==&scene=124#wechat_redirect
如果此鏈接在瀏覽器中無法打開,可將此鏈接發送給微信好友或者發送到微信文件傳輸助手。在點擊打開
前置條件:
1.1 開發環境:windows
1.2 開發框架 : uni-app 、H5+
1.3 編輯器 : hbuilderx
2. 進入個推官網 ,註冊申請帳號,登錄進入開發者中心,創建應用。
2.1 創建應用步驟:我的/應用管理/創建應用
2.2 創建完應用以後,查看應用資訊,得到appId、appKey、appSecret
3. 打開 hbuilderx ,在 manifest.json 文件裡面的源碼視圖下面配置 sdkConfigsd 對象下的 push 裡面配置 igexin對象的屬性值
程式碼如下:manifest.json
"push" : { "igexin" : { "appid" : "", "appkey" : "", "appsecret" : "" } }
appid 、appkey 、appsecret 的獲取請閱讀步驟 2.2 獲取
程式碼示例截圖 manifest.json
4. 在APP客戶端監聽推送消息
可參考此處文檔
Push推送開發指南 : //ask.dcloud.net.cn/article/34
h5+推送消息監聽 //www.html5plus.org/doc/zh_cn/push.html#plus.push.addEventListener
4.1 配置完igexin的屬性值以後,在app.vue文件裡面監聽推送消息事件
程式碼示例 app.vue 文件, 在 onLaunch 裡面
// console.log('App Launch') const _self = this; //監聽系統通知欄消息點擊事件 plus.push.addEventListener('click', function(message) { //處理點擊消息的業務邏輯程式碼 console.log(message); plus.nativeUI.toast('click:'+JSON.stringify(message)); //plus.nativeUI.toast('push click'); _self._handlePush(message); }, false); //監聽接收透傳消息事件 plus.push.addEventListener('receive', function(message) { console.log(message); plus.nativeUI.toast('receive:'+JSON.stringify(message)); //處理透傳消息的業務邏輯程式碼 //plus.nativeUI.toast('push receive'); _self._handlePush(message); }, false);
獲取 cid(clientid),需要在App端自行獲取,程式碼如下
//獲取cid var pinf = plus.push.getClientInfo(); var cid = pinf.clientid;//客戶端標識
注意:
1.使用uni-app開發不需要引入SDK文件,在HbuilderX雲打包時,雲打包會自動根據manifest裡面配置的 appid 、appkey 、appsecret 按需引入相關SDK
獲取cid(clientid) : 在App內使用H5+ 進行獲取
2.1 H5+獲取 cid 文檔 :
//www.html5plus.org/doc/zh_cn/push.html#plus.push.getClientInfo
3. uni-app使用plus注意事項 //uniapp.dcloud.io/use-html5plus
uni-app不需要 plus ready 在html中使用plus的api,需要等待plus ready。而uni-app不需要等,可以直接使用。而且如果你調用plus ready,反而不會觸發
4. manifest.json 配置完成後,打包為測試包(自定義基座),測試推送消息
4.1 測試推送消息,如果不知道App設備的cid,可通過個推指定的 獲取cid的app工具,安裝到手機上,在開發者中心,選擇創建推送/配置管理/故障排查菜單,輸入cid工具里的驗證碼
4.2 手機上安裝的App必須配置了 appid 、appkey 、appsecret,否則通過cid工具裡面的驗證碼取不到CID