app推送监听(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);

// 监听点击消息事件

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

alert(cid);

    // 判断是从本地创建还是离线推送的消息

    switch(msg.payload ) {

        case "LocalMSG":

            mui.toast( "点击本地创建消息启动:" );

        break;

        default:

              mui.toast( "点击离线推送消息启动:");

        break;

    }

    // 提示点击的内容

    plus.ui.alert(msg.content);

}, false );

// 监听在线消息事件

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 来同步页面的数据,仅做演示。 

          * 实际开发中,这里可能是跳转到某个页面等操作,请根据自身业务需求编写。 

          */ 

  _self.$router.push({   

                  name: message.page,

                  query:{id:message.parameter}

              })

      }; 

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

  alert("跳转链接:"+message.page+"---跳转参数:"+message.parameter);

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

          _handlePush(message); 

      }); 

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

  alert("跳转链接:"+message.page+"===跳转参数:"+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;

// 监听点击消息事件

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

    // 判断是从本地创建还是离线推送的消息

      //mui.alert&#40;"点击处理消息 " + JSON.stringify(msg&#41;);

    switch(msg.payload ) {

        case "LocalMSG":

            mui.toast( "点击本地创建消息启动:" );

        break;

        default:

              mui.toast( "点击离线推送消息启动:");

        break;

    }

    // 提示点击的内容

    plus.ui.alert(msg.content);

}, false );

// 监听在线消息事件

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创建本地消息!";   

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

          mui.toast( "接收到在线透传消息:" );

    }

    logoutPushMsg(msg);

    }, false ); 

});

/**

* 日志输入推送消息内容

*/

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));

}

}

/**

* 获取本地推送标识信息

*/

function getPushInfo() {

var info = plus.push.getClientInfo();

outSet("获取客户端推送标识信息:");

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

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

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

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

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

}

</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。