V2.0 - APP推送+版本检测
yoyocmf 2020-12-23 11:02:48

推送之个推

官方文档:https://docs.getui.com/getui/server/php/usermanage/

uni-app 资料: https://ask.dcloud.net.cn/article/35622

 

 

【uni-app】APP处理个推 

JavaScript Code复制内容到剪贴板
  1. // #ifdef APP-PLUS  
  2. console.log("开始测试push点击事件")  
  3. //监听系统通知栏消息点击事件  
  4. plus.push.addEventListener('click'function(msg){    
  5.     //处理点击消息的业务逻辑代码   
  6.     // {"aps":{"alert":{"body":"如果在线情况下接收透传消息","title":"点击通知?"},"badge":3,"content-available":0,"mutable-content":1},"content":"如果在线情况下接收透传消息","payload":"/daiban","title":"工地管理","type":"click"}  
  7.     // 根据payload处理事件信息  
  8.     uni.showModal({  
  9.         title: '点击通知栏消息',  
  10.         content: JSON.stringify(msg),  
  11.         showCancel: false,  
  12.         cancelText: '',  
  13.         confirmText: '去处理',  
  14.         success: res => {  
  15.               
  16.         },  
  17.         fail: () => {},  
  18.         complete: () => {}  
  19.     });  
  20.       
  21.     console.log("Listener push click", JSON.stringify(msg))  
  22. }, false);  
  23.   
  24. //监听接收透传消息事件  
  25. plus.push.addEventListener('receive'function(msg){    
  26.     //处理透传消息的业务逻辑代码   
  27.     // {"aps":null,"content":"如果在线情况下接收透传消息","payload":"如果在线情况下接收透传消息","title":"工地管理","type":"receive"}  
  28.     if(msg.aps){  
  29.         // 透传消息,非APNS  
  30.     }  
  31.       
  32.     uni.showModal({  
  33.         title: '收到透传消息',  
  34.         content: JSON.stringify(msg),  
  35.         showCancel: false,  
  36.         cancelText: '',  
  37.         confirmText: '去处理',  
  38.         success: res => {  
  39.               
  40.         },  
  41.         fail: () => {},  
  42.         complete: () => {}  
  43.     });  
  44.       
  45.     console.log("Listener push receive", JSON.stringify(msg))  
  46. }, false);  
  47. // #endif  

 

 


 

APP版本升级检测

 

包含了整包升级,以及热更新

整包升级方案:https://ask.dcloud.net.cn/article/34972

热更新方案:https://ask.dcloud.net.cn/article/35667

 

【uni-app】 store/index.js,添加action

XML/HTML Code复制内容到剪贴板
  1. checkUpdate: async function({  
  2.     dispatch,  
  3.     commit,  
  4.     state  
  5. }, params) {  
  6.       
  7.     // #ifdef APP-PLUS  
  8.     return await new Promise((resolve, reject) => {  
  9.         plus.runtime.getProperty(plus.runtime.appid, async (wgtinfo) => {  
  10.             const appId = wgtinfo.appid  
  11.             const versionCode = wgtinfo.versionCode  
  12.             let ifNeedRes = await dispatch("main/getLastVersion", {"app_id":appId,"version_code":versionCode});  
  13.             if(ifNeedRes.if_need == true){  
  14.                 // 热更新  
  15.                 if (ifNeedRes.type == 0 && ifNeedRes.wgt_url) {  
  16.                     uni.showModal({  
  17.                         title: '发现新版本'+ ifNeedRes.new_version,  
  18.                         content: ifNeedRes.description,  
  19.                         // showCancel: true,  
  20.                         cancelText: '暂不升级',  
  21.                         confirmText: '静默升级',  
  22.                         success: res => {  
  23.                             if (res.confirm) {  
  24.                                 uni.downloadFile({  
  25.                                     url: ifNeedRes.wgt_url,  
  26.                                     success: (downloadResult) => {  
  27.                                         console.log(downloadResult);  
  28.                                         if (downloadResult.statusCode === 200) {  
  29.                                             plus.runtime.install(downloadResult.tempFilePath, {  
  30.                                                 force: false  
  31.                                             }, function() {  
  32.                                                 uni.showModal({  
  33.                                                     title: '成功',  
  34.                                                     content: '升级成功,即将重启应用',  
  35.                                                     showCancel: false,  
  36.                                                     cancelText: '',  
  37.                                                     confirmText: '好的',  
  38.                                                     success: res => {  
  39.                                                         plus.runtime.restart();  
  40.                                                     },  
  41.                                                     fail: () => {},  
  42.                                                     complete: () => {}  
  43.                                                 });  
  44.                           
  45.                                             }, function(e) {  
  46.                                                 uni.showToast({  
  47.                                                     title: e.message,  
  48.                                                     icon: "none"  
  49.                                                 });  
  50.                                                 // reject(e.message)  
  51.                                                 // console.log("安装失败的原因", e);  
  52.                                                 // console.error('安装失败...');    
  53.                                             });  
  54.                                         }  
  55.                                     }  
  56.                                 });  
  57.                             }  
  58.                         },  
  59.                         fail: () => {},  
  60.                         complete: () => {}  
  61.                     });  
  62.                 } else if(ifNeedRes.link_url) {  
  63.                     // 整包更新  
  64.                     uni.showModal({  
  65.                         title: '发现新版本'+ ifNeedRes.new_version,  
  66.                         content: ifNeedRes.description,  
  67.                         // showCancel: true,  
  68.                         cancelText: '暂不升级',  
  69.                         confirmText: '去下载',  
  70.                         success: res => {  
  71.                             if (res.confirm) {  
  72.                                 plus.runtime.openURL(res.link_url);   
  73.                             }  
  74.                         },  
  75.                         fail: () => {},  
  76.                         complete: () => {}  
  77.                     });  
  78.                 }  
  79.             }  
  80.             // console.log("是否需要更新",ifNeedRes);  
  81.             resolve(ifNeedRes);  
  82.         })  
  83.     })  
  84.     // #endif  
  85. }  

 

在App.vue的onLaunch中检测:

XML/HTML Code复制内容到剪贴板
  1. import store from './store'  
  2.   
  3. export default {  
  4.         onLaunch:async function() {  
  5.             console.log('App Launch')  
  6.             store.dispatch("checkUpdate"); //检测版本号  
  7.               

 

如果需要加点击事件也可以加:

XML/HTML Code复制内容到剪贴板
  1. checkUpdate:function(){  
  2.     uni.showLoading({  
  3.         title: '检测中',  
  4.         mask: false  
  5.     });  
  6.     this.$store.dispatch("checkUpdate").then(ifNeedRes => {  
  7.         uni.hideLoading()  
  8.         if(ifNeedRes.if_need == false){  
  9.             uni.showModal({  
  10.                 title: '最新版本',  
  11.                 content: '您的版本已经为最新版本V' + ifNeedRes.new_version,  
  12.                 showCancel: false,  
  13.                 cancelText: '',  
  14.                 confirmText: '好的',  
  15.             });  
  16.         }  
  17.     }, response => {  
  18.         uni.hideLoading()  
  19.         // console.log("安装失败",response);  
  20.         uni.showToast({  
  21.             title: response,  
  22.             icon: 'none'  
  23.         });  
  24.     })  
  25. }  

 

 

APP在登录的时候即需要获取client id用于存储:

JavaScript Code复制内容到剪贴板
  1. async setPushAlias(userid){  
  2.     // 页面加载时触发    
  3.     var pinf = plus.push.getClientInfo();    
  4.     var cid = pinf.clientid;//客户端标识  
  5.     let api = loginAPI.setAliasAPI;  
  6.     let res = await this.$request.post(api, {  
  7.         data: {  
  8.             "userid": userid,  
  9.             "alias": cid  
  10.         }  
  11.     });  
  12.     console.log(api);  
  13.     console.log({  
  14.             "userid": userid,  
  15.             "alias": cid  
  16.         });  
  17.     console.log("设置用户ID为:" + userid + "的别名为"+cid);  
  18.     if (res.errcode != 0) {  
  19.         uni.showModal({  
  20.             title: '设置别名失败',  
  21.             content: res.errmsg,  
  22.             showCancel: false,  
  23.             success: res => {  
  24.                   
  25.             },  
  26.             fail: () => {},  
  27.             complete: () => {}  
  28.         });  
  29.       
  30.     }  
  31. }  

 

 

本文来自于:http://www.yoyo88.cn/note/yoyocmf/557.html

下一篇 返回列表
Powered by yoyo苏ICP备15045725号-1