提交 dc7715a0 编写于 作者: taohebin@dcloud.io's avatar taohebin@dcloud.io

update: unipush示例调整

上级 f247ce2e
...@@ -2,67 +2,95 @@ ...@@ -2,67 +2,95 @@
<!-- #ifdef APP --> <!-- #ifdef APP -->
<scroll-view style="flex: 1"> <scroll-view style="flex: 1">
<!-- #ifdef APP-ANDROID --> <!-- #ifdef APP-ANDROID -->
<button class="normal-button" type="default" @click="handleCreateChannel"> <button class="normal-button" type="default" @click="handleCreateChannel(true)">
创建通知渠道 创建通知渠道 | setPushChannel
</button> </button>
<button class="normal-button" type="default" @click="handleGetAllChannels"> <button class="normal-button" type="default" @click="handleGetAllChannels">
获取所有通知渠道信息 获取所有通知渠道信息 | getAllChannels
</button> </button>
<!-- #endif --> <!-- #endif -->
<button class="normal-button" type="default" @click="handleCreateLocalNotification"> <button class="normal-button" type="default" @click="handleCreateLocalNotification">
创建本地通知消息 创建本地通知消息 | createPushMessage
</button> </button>
<button class="normal-button" type="default" @click="handleSetBadge"> <button class="normal-button" type="default" @click="handleSetBadge">
设置角标 设置角标为5 | setAppBadgeNumber(5)
</button> </button>
<button class="normal-button" type="default" @click="handleCleanBadge"> <button class="normal-button" type="default" @click="handleCleanBadge">
清空角标 清空角标 | setAppBadgeNumber(0)
</button> </button>
<button class="normal-button" type="default" @click="handleGetClientId"> <button class="normal-button" type="default" @click="handleGetClientId">
获取cid 获取cid | getPushClientId
</button> </button>
<textarea style="flex: 1;width: 100%;" :disabled="true" :value="channelInfo"></textarea>
</scroll-view> </scroll-view>
<!-- #endif --> <!-- #endif -->
</template> </template>
<script setup> <script setup>
const handleCreateChannel = () => {
const channelInfo = ref("")
const handleCreateChannel = (showToast: boolean) => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const manager = uni.getChannelManager() const manager = uni.getChannelManager()
manager.setPushChannel({ manager.setPushChannel({
channelId: "msg-pass", channelId: "msg-pass",
channelDesc: "留言审核通过", channelDesc: "留言审核通过",
} as SetPushChannelOptions) } as SetPushChannelOptions)
if(showToast){
uni.showToast({
title: "设置渠道成功"
})
}
// #endif // #endif
} }
const handleGetAllChannels = () => { const handleGetAllChannels = () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const manager = uni.getChannelManager() const manager = uni.getChannelManager()
console.log("channels : " + manager.getAllChannels()); console.log("channels : " + manager.getAllChannels());
channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}`
// #endif // #endif
} }
const handleCreateLocalNotification = () => { const handleCreateLocalNotification = () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") { if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
// #endif // #endif
handleCreateChannel() handleCreateChannel(false)
const date = new Date();
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
const formateTime = (target: number): string =>{
return target < 10 ? `0${target}` : `${target}`
}
uni.createPushMessage({ uni.createPushMessage({
title: "halo", title: "主标题(title)",
content: "world", content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`,
// cover:true, cover:false,
channelId: "msg-pass", channelId: "msg-pass",
when: Date.now() + 10000, when: Date.now() + 10000,
icon: "/static/uni.png", icon: "/static/uni.png",
// delay:5, sound:"system",
delay:1,
payload: { payload: {
pkey: "pvalue1" pkey: "pvalue1"
}, },
category: "IM", category: "IM",
success(res) { success(res) {
console.log("res: " + res); console.log("res: " + res);
uni.hideToast()
uni.showToast({
title:"创建本地通知消息成功"
})
}, },
fail(e) { fail(e) {
console.log("fail :" + e); console.log("fail :" + e);
uni.hideToast()
uni.showToast({
title:"创建本地通知消息失败",
icon:"error"
})
} }
}) })
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
...@@ -75,17 +103,32 @@ ...@@ -75,17 +103,32 @@
// #endif // #endif
} }
const handleGetClientId = () => { const handleGetClientId = () => {
uni.getPushClientId({ uni.getPushClientId({
complete(e : any) { success:(res : GetPushClientIdSuccess)=>{
console.log(e); uni.showModal({
title:"信息",
content: `获取cid : ${res.cid}`
})
},
fail:()=>{
uni.showToast({
title:`获取cid失败`,
icon: "error"
})
} }
}) })
} }
const handleSetBadge = () => { const handleSetBadge = () => {
uni.setAppBadgeNumber(5) uni.setAppBadgeNumber(5)
uni.showToast({
title:"设置应用角标数为5"
})
} }
const handleCleanBadge = () => { const handleCleanBadge = () => {
uni.setAppBadgeNumber(0) uni.setAppBadgeNumber(0)
uni.showToast({
title:"清空应用角标数"
})
} }
</script> </script>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册