V2.0 - APP推送+版本检测
推送之个推
官方文档:https://docs.getui.com/getui/server/php/usermanage/
uni-app 资料: https://ask.dcloud.net.cn/article/35622
【uni-app】APP处理个推
JavaScript Code复制内容到剪贴板
- // #ifdef APP-PLUS
- console.log("开始测试push点击事件")
- //监听系统通知栏消息点击事件
- plus.push.addEventListener('click', function(msg){
- //处理点击消息的业务逻辑代码
- // {"aps":{"alert":{"body":"如果在线情况下接收透传消息","title":"点击通知?"},"badge":3,"content-available":0,"mutable-content":1},"content":"如果在线情况下接收透传消息","payload":"/daiban","title":"工地管理","type":"click"}
- // 根据payload处理事件信息
- uni.showModal({
- title: '点击通知栏消息',
- content: JSON.stringify(msg),
- showCancel: false,
- cancelText: '',
- confirmText: '去处理',
- success: res => {
- },
- fail: () => {},
- complete: () => {}
- });
- console.log("Listener push click", JSON.stringify(msg))
- }, false);
- //监听接收透传消息事件
- plus.push.addEventListener('receive', function(msg){
- //处理透传消息的业务逻辑代码
- // {"aps":null,"content":"如果在线情况下接收透传消息","payload":"如果在线情况下接收透传消息","title":"工地管理","type":"receive"}
- if(msg.aps){
- // 透传消息,非APNS
- }
- uni.showModal({
- title: '收到透传消息',
- content: JSON.stringify(msg),
- showCancel: false,
- cancelText: '',
- confirmText: '去处理',
- success: res => {
- },
- fail: () => {},
- complete: () => {}
- });
- console.log("Listener push receive", JSON.stringify(msg))
- }, false);
- // #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复制内容到剪贴板
- checkUpdate: async function({
- dispatch,
- commit,
- state
- }, params) {
- // #ifdef APP-PLUS
- return await new Promise((resolve, reject) => {
- plus.runtime.getProperty(plus.runtime.appid, async (wgtinfo) => {
- const appId = wgtinfo.appid
- const versionCode = wgtinfo.versionCode
- let ifNeedRes = await dispatch("main/getLastVersion", {"app_id":appId,"version_code":versionCode});
- if(ifNeedRes.if_need == true){
- // 热更新
- if (ifNeedRes.type == 0 && ifNeedRes.wgt_url) {
- uni.showModal({
- title: '发现新版本'+ ifNeedRes.new_version,
- content: ifNeedRes.description,
- // showCancel: true,
- cancelText: '暂不升级',
- confirmText: '静默升级',
- success: res => {
- if (res.confirm) {
- uni.downloadFile({
- url: ifNeedRes.wgt_url,
- success: (downloadResult) => {
- console.log(downloadResult);
- if (downloadResult.statusCode === 200) {
- plus.runtime.install(downloadResult.tempFilePath, {
- force: false
- }, function() {
- uni.showModal({
- title: '成功',
- content: '升级成功,即将重启应用',
- showCancel: false,
- cancelText: '',
- confirmText: '好的',
- success: res => {
- plus.runtime.restart();
- },
- fail: () => {},
- complete: () => {}
- });
- }, function(e) {
- uni.showToast({
- title: e.message,
- icon: "none"
- });
- // reject(e.message)
- // console.log("安装失败的原因", e);
- // console.error('安装失败...');
- });
- }
- }
- });
- }
- },
- fail: () => {},
- complete: () => {}
- });
- } else if(ifNeedRes.link_url) {
- // 整包更新
- uni.showModal({
- title: '发现新版本'+ ifNeedRes.new_version,
- content: ifNeedRes.description,
- // showCancel: true,
- cancelText: '暂不升级',
- confirmText: '去下载',
- success: res => {
- if (res.confirm) {
- plus.runtime.openURL(res.link_url);
- }
- },
- fail: () => {},
- complete: () => {}
- });
- }
- }
- // console.log("是否需要更新",ifNeedRes);
- resolve(ifNeedRes);
- })
- })
- // #endif
- }
在App.vue的onLaunch中检测:
XML/HTML Code复制内容到剪贴板
- import store from './store'
- export default {
- onLaunch:async function() {
- console.log('App Launch')
- store.dispatch("checkUpdate"); //检测版本号
如果需要加点击事件也可以加:
XML/HTML Code复制内容到剪贴板
- checkUpdate:function(){
- uni.showLoading({
- title: '检测中',
- mask: false
- });
- this.$store.dispatch("checkUpdate").then(ifNeedRes => {
- uni.hideLoading()
- if(ifNeedRes.if_need == false){
- uni.showModal({
- title: '最新版本',
- content: '您的版本已经为最新版本V' + ifNeedRes.new_version,
- showCancel: false,
- cancelText: '',
- confirmText: '好的',
- });
- }
- }, response => {
- uni.hideLoading()
- // console.log("安装失败",response);
- uni.showToast({
- title: response,
- icon: 'none'
- });
- })
- }
APP在登录的时候即需要获取client id用于存储:
JavaScript Code复制内容到剪贴板
- async setPushAlias(userid){
- // 页面加载时触发
- var pinf = plus.push.getClientInfo();
- var cid = pinf.clientid;//客户端标识
- let api = loginAPI.setAliasAPI;
- let res = await this.$request.post(api, {
- data: {
- "userid": userid,
- "alias": cid
- }
- });
- console.log(api);
- console.log({
- "userid": userid,
- "alias": cid
- });
- console.log("设置用户ID为:" + userid + "的别名为"+cid);
- if (res.errcode != 0) {
- uni.showModal({
- title: '设置别名失败',
- content: res.errmsg,
- showCancel: false,
- success: res => {
- },
- fail: () => {},
- complete: () => {}
- });
- }
- }
上一篇 V2.0 - 工作流使用教程
下一篇 select2picker联动