App SSE 消息推送

消息提醒模块

一、背景

服务端推送的消息通过 app 提示给用户 ( 仅适用于Apicloud APP开发 )

二、流程

通过建立SSE持续连接,后台推送消息后,App 拿到响应 JSON 数据,通过模块开启后台保活,消息通知提示,语音播报,屏幕唤醒进行通知使用者

三、使用方式

SSE 推送方法

服务器推送客户端方案 SSE 在规则引擎中的应用:http://119.96.220.140:9099/article/1629195920546
大屏后台数据推送报错时不继续进行推送的解决方法:http://119.96.220.140:9099/article/1657203261567

前端方法:

// 建立SSE推送  
methods:{
getMessagesBySSE(){  
 let source  = `${springbootUrl}` + "sseMsg/pushApp/" + '${user.loginName}';  
​ this.source   = source  
 if (server_push) {  
 if (typeof window.EventSource !== "undefined") {  
 console.log("连接EventSource")  
 // 后端接口,要配置允许跨域属性  
 const source = new EventSource(source);  
​  
 // 当SSE连接刚打开时被调用  
 source.addEventListener('open', function(e) {  
 console.log("Connection was opened.");  
 }, false);  
​  
 //source.close(); // 如果存在,则关闭连接,并且设置 readyState 属性为 CLOSED。如果连接已经被关闭,此方法不会再进行任何操作。  
​  
 // 当从事件源接收到数据时触发  
 source.addEventListener("message", (messages) => {  
 console.log(messages);  
 // 在这里处理接收到的消息  
 })  
   
 // 当SSE连接发生错误时触发  
 sshErrorListen(source, this.getMessagesBySSE, this)  
   
 } else {  
 console.log("当前浏览器不支持使用EventSource接收服务器推送事件!");  
 }  
 }  
}    
​ 
// 2022-10-19改 ssh 会自动触发重连
// 监听ssh管道是否需要报错重连  
// const sshErrorListen = (ssh, callback, ctx, params) => {  
//  if(Object.prototype.toString.call(ssh) === "[object EventSource]"){  
//  ssh.addEventListener("error", function () {  
 // 通道关闭需要重连  
//  ssh.readyState == 2 && callback.call(ctx, params)  
//  })  
//  }  
// }
},

// 需要在页面销毁之前关闭连接
 beforeDestroy() {       
     this.source.close()   // 调用SSE关闭方法
 }

调用守护进程 (后台保活)

startAlive(){  
 if (!window.navigator.userAgent.toLowerCase().includes("apicloud")) {  
 return  
 }  
 // eslint-disable-next-line no-undef  
 let moduleProcessAlive = api.require("moduleProcessAlive_2022");  
 let params = {};  
 moduleProcessAlive.startAlive(params);  
}

调用消息提示

notification(){  
 if (!window.navigator.userAgent.toLowerCase().includes("apicloud")) {  
 return  
 }  
 // eslint-disable-next-line no-undef  
 let moduleNotification = api.require("moduleNotification_2022");  
 let param = {  
 title: "来自MOM的异常消息", // 消息提示的标题  
 content: message, // 消息提示的内容  
 };  
 moduleNotification.showNotification(param))  
}  

调用语音播报

iflyRecognition(){  
 if (!window.navigator.userAgent.toLowerCase().includes("apicloud")) {  
 return  
 }  
 // 调用语音播报模块前需要初始化  
 // eslint-disable-next-line no-undef  
 let iflyRecognition = api.require("iflyRecognition");  
 iflyRecognition.createUtility(  
 {  
 android_appid: "5bd7b19a",  
 },  
 function (ret, err) {  
 if (ret.status) {  
 console.log("语音初始化成功");  
 } else {  
 console.log("语音初始化失败");  
 }  
 }  
 );  
 iflyRecognition.read({  
 readStr: message, // 阅读内容  
 speed: 60,      // 阅读速度 范围是0-100  
 volume: 60,   // 阅读音量 范围是0-100  
 voice: "xiaoyan", // 阅读角色  
 rate: 16000,    // 采样率  
 },(ret,err)=>{  
 console.log(ret)   
 /* ret :{  
 * status: //布尔类型;操作成功状态值,true|false  
 * speakProgress: //数字类型;朗读的进度  
 *}  
 */   
 console.log(err)  
 })  
}

调用屏幕唤醒

screenWake(){  
 if (!window.navigator.userAgent.toLowerCase().includes("apicloud")) {  
 return  
 }  
 // eslint-disable-next-line no-undef  
 let sw = api.require("screenWake");  
 console.log("导入了screenWake");  
 sw.addScreenWake()  
}