push.vue
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<template>
<view>
<page-head :title="title"></page-head>
<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">监听透传数据</button>
<button type="primary" @tap="removeTranMsg">移除监听透传数据</button>
</view>
<view class="uni-btn-v uni-common-mt">
<button type="primary" @tap="requireTranMsg">发送"透传数据"消息</button>
</view>
<view class="uni-title uni-common-mt">透传内容:</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:''
}
},
onLoad: function () {
uni.getProvider({
service: "push",
success: (e) => {
console.log("success", e);
this.provider = e.provider;
},
fail: (e) => {
console.log("获取推送通道失败", e);
}
});
},
onUnload:function(){
this.tranMsg = ''
},
methods: {
openPush() {
uni.subscribePush({
provider: this.provider[0],
success: (e) => {
uni.showToast({
title: "已开启push接收"
})
}
})
},
closePush() {
uni.unsubscribePush({
provider: this.provider[0],
success: (e) => {
uni.showToast({
title: "已关闭push接收"
})
}
})
},
listenTranMsg() {
uni.onPush({
provider: this.provider[0],
success: (e) => {
uni.showToast({
title: "开始监听透传数据"
})
},
callback: (e) => {
uni.showToast({
title: "接收到透传数据"
});
this.tranMsg = JSON.stringify(e.data);
}
})
},
removeTranMsg() {
uni.offPush({
provider: this.provider[0],
success: (e) => {
console.log("移除监听透传数据");
uni.showToast({
title: "移除监听透传数据"
})
}
})
},
requireTranMsg() { //请求‘透传数据’推送消息
var inf = plus.push.getClientInfo();
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('带透传数据推送通知!'));
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>