uniapp + a push, push to achieve

<template>
<view>
<textarea v-model="tranMsg" />
-------------------------
<textarea v-model="cid" />
+++++++++++++++++++++++
<textarea v-model="ccid" />
<!-- <view class="uni-padding-wrap" v-if="provider[0]">
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="openPush">开启push</button>
<button type="primary" @tap="closePush">关闭push</button>
<Button type = "primary" @ tap = "listenTranMsg"> monitor data pass-through </ button>
<Button type = "primary" @ tap = "removeTranMsg"> removed monitor data pass-through </ button>
</view>
<view class="uni-btn-v uni-common-mt">
<Button type = "primary" @ tap = "requireTranMsg"> Send "transparently transmit data" message </ button>
</view>
<View class = "uni-title uni-common-mt"> passthrough content: </ view>
<view class="uni-textarea">
<textarea v-model="tranMsg" />
</view>
</view> -->
</view>
</template> 
<script>
export default {
data() {
return {
title: 'push',
provider: [],
pushServer: 'http://demo.dcloud.net.cn/push/?',
tranMsg:'',
cid:'',
ccid:''
}
},
 
onLoad: function () {
uni.getProvider({
service: "push",
success: (e) => {
console.log("success", e);
this.provider = e.provider;
},
fail: (e) => {
console.log ( "feed channel acquisition failure", e);
}
});
},
onUnload:function(){
this.tranMsg = '';
 
},
mounted(){
// enabled by default to accept PUSH
this.openPush();
// default on monitor passthrough
this.listenTranMsg();
},
methods: {
 
openPush() {
var that=this;
was cid = plus.device.uuid;
was clientid;  
       was h = setInterval (function () {  
         if(clientid!=null && clientid!="null")  
         {  
            clearInterval(timer);  
            return;  
         }  
         clientid=plus.push.getClientInfo().clientid;  
that.ccid = clientid;
},1000); 
 
console.log('==CID=='+cid);
this.cid=cid;
uni.subscribePush({
provider: this.provider[0],
success: (e) => {
console.log ( 'open receiving PUSH');
// uni.showToast ({
// title: "is on receiving a push."
//  })
}
})
},
 
closePush() {
uni.unsubscribePush({
provider: this.provider[0],
success: (e) => {
uni.showToast ({
title: "Closed to receive push"
})
}
})
},
listenTranMsg() {
uni.onPush ({
provider: this.provider[0],
success: (e) => {
the console.log ( 'open listen');
// uni.showToast ({
// title: "start listening for transparent transmission of data."
//  })
},
callback: (e) => {
uni.showToast ({
title: "pass-through data is received"
});
 
this.tranMsg = JSON.stringify(e.data);
}
})
},
removeTranMsg() {
uni.offPush ({
provider: this.provider[0],
success: (e) => {
the console.log ( "pass-through data removed listen");
uni.showToast ({
title: "remove the monitor pass-through data"
})
}
})
},
requireTranMsg () {// Request 'pass-through data' push message
var plus.push.getClientInfo inf = ();
 
var url = this.pushServer + 'type=tran&appid=' + encodeURIComponent(plus.runtime.appid);
inf.id && (url += '&id=' + inf.id);
url += ('&cid=' + encodeURIComponent(inf.clientid));
if (plus.os.name == 'iOS') {
url += ('&token=' + encodeURIComponent(inf.token));
}
url += ('&title=' + encodeURIComponent('Hello uniapp'));
url + = ( '& content =' + encodeURIComponent ( 'with a pass-through data push notification!'));
if(plus.os.name === 'iOS'){
url += ('&payload=' + encodeURIComponent('{"title":"Hello uniapp Test","content":"test content"}'));
}else{
url += ('&payload=' + encodeURIComponent('\'{"title":"Hello uniapp Test","content":"test content"}\''));
}
url += ('&version=' + encodeURIComponent(plus.runtime.version));
plus.runtime.openURL(url);
}
}
}
</script>
 
<style>
 
</style>

Guess you like

Origin www.cnblogs.com/ydam/p/10983533.html