当前位置: 首页 > Web前端 > vue.js

Unipush消息推送及遇到的问题

时间:2023-03-31 16:45:50 vue.js

1.在app.vue页面添加onLaunchonLaunch:function(){varplatform=uni.getSystemInfoSync().platform;//#ifdefAPP-PLUSplus.screen.lockOrientation("portrait-primary");//推送varpinf=plus.push.getClientInfo();varcid=pinf.clientid;//客户端IDconsole.log('cid:'+cid);const_self=这个;const_handlePush=function(payload){varpages=getCurrentPages();console.log('----------',payload)//页面栈为0,标记app被push唤醒,在欢迎页面执行跳转逻辑if(payload.alive===false){console.log('setstorage')uni.setStorage({key:'appLaunchedByPush',data:payload})}else{//app在线,可以直接跳转到uni.navigateTo({url:'/pages/index'});}};plus.push.addEventListener('click',function(message){console.log('pushclick=======',message)if(platform=='ios'){//离线时,payload是一个对象,在线时,本地创建的是一个字符串(本地创建的notifications只能传字符串,否则什么都没有传递有效载荷的方法)if(typeofmessage.payload=='string'){console.log('string')varpayload=JSON.parse(message.payload);}else{console.log('obj')varpayload=message.payload;}_handlePush(有效载荷);return}//这里可以写跳转业务代码_handlePush(message.payload);});plus.push.addEventListener('receive',function(message){console.log('pushreceive======',message);if(message.type=="receive"){//判断触发来源在这里,否则继续推动if(platform=='ios'){console.log('iosreceive',message);if(typeofmessage.payload=='string'){plus.push.createMessage(message.content,message.payload,{cover:false,title:message.title,});}else{plus.push.createMessage(message.content,JSON.stringify(message.payload),{cover:false,title:message.title,});}}else{console.log('androidreceive======',message);//执行跳转判断或添加本地通知plus.push.createMessage(message.content,JSON.stringify(message.payload),{cover:false,title:message.title});}}});}2。处理离线消息跳转将我添加到应用默认跳转页面。这里是登录页面uni.switchTab({url:'/pages/workplace/index',success:()=>{//处理push唤醒app时消息push的跳转constpayload=uni.得到StorageSync('appLaunchedByPush')console.log('appLaunchedByPush',payload)if(payload){setTimeout(()=>{console.log(payload+'setTimeout')uni.navigateTo({url:'/pages/index'});uni.setStorage({key:'appLaunchedByPush',data:null})})}}})3.manifest.json="App模块配置中,勾选消息推送和uniPush选项,开启消息推送功能4.在unicloud开发者中心添加项目应用,添加后进入应用,在unipush="中发送消息messagepush测试是否能收到推送消息5.在unicloud开发者中心=”你创建的应用=”unipush=”在厂商推送设置中配置各个厂商的推送设置(app上线时,消息通过第三方消息推送平台,离线时通过手机厂商的推送通道,所以这个必须配置,如果不配置,应用离线时会收到不是离线消息),配置完成后应用离线也可以接收推送消息。遇到的问题:1.无法接收华为离线消息推送?(1)厂商配置是否正确r的推送完成,agconnect-services.json文件是否上传(2)打包打印自定义base必须使用自己的证书,公共证书收不到消息推送(3)华为手机会发离线消息默认归类为营销通知,离线时收到消息,但不显示。只有在手机设置中打开营销通知选项=》notification=》yourapp=》才能正常显示离线消息。2.iOS在线离线消息收不到?(1)是否申请ioscertificate(2)检查是否在开发者中心配置了ios相关的证书=》消息推送=》应用配置=》(3)安装自定义库时证书文件和私钥证书是否正确上传3.收到离线消息,但无法正常重定向。点击后只能跳转到首页?离线消息点击后,默认跳转到首页或欢迎页面。这时app.vue中写的跳转消息详情页也会跳转。页面会报错提示不要重复跳转到详情页。点击跳转到首页后,需要判断应用首页是否有离线消息,然后执行消息详情页的跳转。

猜你喜欢