app推送監(jiān)聽(vue)

<script>

var u = navigator.userAgent;

isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android安卓

var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios蘋果

if(isAndroid){

alert("安卓手機"+u.substring(u.indexOf('Android')+1));

}

if(isiOS){

alert("蘋果手機") ;

}

mui.plusReady(function(){

? alert(u);?

message = document.getElementById("message");

var pinf = plus.push.getClientInfo();

var cid=pinf.clientid;

console.log("-------------"+cid);

// 監(jiān)聽點擊消息事件

plus.push.addEventListener( "click", function( msg ) {

alert(cid);

? ? // 判斷是從本地創(chuàng)建還是離線推送的消息

? ? switch(msg.payload ) {

? ? ? ? case "LocalMSG":

? ? ? ? ? ? mui.toast( "點擊本地創(chuàng)建消息啟動:" );

? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? ? mui.toast( "點擊離線推送消息啟動:");

? ? ? ? break;

? ? }

? ? // 提示點擊的內(nèi)容

? ? plus.ui.alert(msg.content);

}, false );

// 監(jiān)聽在線消息事件

plus.push.addEventListener( "receive", function( msg ) {

console.log('------------:'+cid);

alert(cid);

? ? if (msg.aps) {? // Apple APNS message ? ?

? ? ? ? ? mui.toast( "接收到在線APNS消息:"+msg.payload );

? ? } else { ? ?

? ? ? ? ? mui.toast( "接收到在線透傳消息:" );

? ? }

? ? logoutPushMsg(msg);

? ? }, false );?

});

</script>

onLaunch() {

? ? ? // #ifdef APP-PLUS?

? ? ? const _self = this;?

? ? ? const _handlePush = function(message) {?

? ? ? ? ? /**?

? ? ? ? ? * 通過 vuex 來同步頁面的數(shù)據(jù),僅做演示。?

? ? ? ? ? * 實際開發(fā)中,這里可能是跳轉(zhuǎn)到某個頁面等操作,請根據(jù)自身業(yè)務(wù)需求編寫。?

? ? ? ? ? */?

? _self.$router.push({? ?

? ? ? ? ? ? ? ? ? name: message.page,

? ? ? ? ? ? ? ? ? query:{id:message.parameter}

? ? ? ? ? ? ? })

? ? ? };?

? ? ? plus.push.addEventListener('click', function(message) {

? alert("跳轉(zhuǎn)鏈接:"+message.page+"---跳轉(zhuǎn)參數(shù):"+message.parameter);

? ? ? ? ? plus.nativeUI.toast('push click');?

? ? ? ? ? _handlePush(message);?

? ? ? });?

? ? ? plus.push.addEventListener('receive', function(message) {

? alert("跳轉(zhuǎn)鏈接:"+message.page+"===跳轉(zhuǎn)參數(shù):"+message.parameter);

? ? ? ? ? plus.nativeUI.toast('push receive');?

? ? ? ? ? _handlePush(message);?

? ? ? });?

? ? ? // #endif?

? },

? ? ? <script>

? ? mui.plusReady(function(){

? ? getPushInfo();

message = document.getElementById("message");

var pinf = plus.push.getClientInfo();

var cid=pinf.clientid;

// 監(jiān)聽點擊消息事件

plus.push.addEventListener( "click", function( msg ) {

? ? // 判斷是從本地創(chuàng)建還是離線推送的消息

? ? ? //mui.alert&#40;"點擊處理消息 " + JSON.stringify(msg&#41;);

? ? switch(msg.payload ) {

? ? ? ? case "LocalMSG":

? ? ? ? ? ? mui.toast( "點擊本地創(chuàng)建消息啟動:" );

? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? ? mui.toast( "點擊離線推送消息啟動:");

? ? ? ? break;

? ? }

? ? // 提示點擊的內(nèi)容

? ? plus.ui.alert(msg.content);

}, false );

// 監(jiān)聽在線消息事件

plus.push.addEventListener( "receive", function( msg ) {

console.log('------------:'+cid);

alert(cid);

? ? if (msg.aps) {? // Apple APNS message

? ? var msgs = plus.push.getAllMessage();?

for(var i in msgs){? ?

var msg = msgs[i];? ?

console.log( i+": "+msg.title+" - "+msg.content );? ?

}

? ? ? ? ? mui.toast( "接收到在線APNS消息:" );

? ? } else {

? ? var options = {cover:false};? ?

var str = dateToStr(new Date());? ?

str += ": 歡迎使用Html5 Plus創(chuàng)建本地消息!";? ?

plus.push.createMessage(str, "LocalMSG", options);?

? ? ? ? ? mui.toast( "接收到在線透傳消息:" );

? ? }

? ? logoutPushMsg(msg);

? ? }, false );?

});

/**

* 日志輸入推送消息內(nèi)容

*/

function logoutPushMsg(msg) {

alert("title: " + msg.title);

alert"content: " + msg.content);

if (msg.payload) {

if (typeof (msg.payload) == "string") {

alert("payload(String): " + msg.payload);

} else {

alert("payload(JSON): " + JSON.stringify(msg.payload));

}

} else {

alert("payload: undefined");

}

if (msg.aps) {

alert("aps: " + JSON.stringify(msg.aps));

}

}

/**

* 獲取本地推送標(biāo)識信息

*/

function getPushInfo() {

var info = plus.push.getClientInfo();

outSet("獲取客戶端推送標(biāo)識信息:");

alert("id: " + info.id);

alert("token: " + info.token);

alert("clientid: " + info.clientid);

alert("appid: " + info.appid);

alert("appkey: " + info.appkey);

}

</script>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容