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

update: unipush示例修改

上级 4aad3542
...@@ -12,6 +12,12 @@ ...@@ -12,6 +12,12 @@
<button class="normal-button" type="default" @click="handleCreateLocalNotification"> <button class="normal-button" type="default" @click="handleCreateLocalNotification">
创建本地通知消息 | createPushMessage 创建本地通知消息 | createPushMessage
</button> </button>
<text class="instructions">
不同手机厂商的角标显示规则不同,有部分设备的rom版本不支持显示角标,另有部分rom需要在应用的通知管理里开启`桌面角标`配置,才可以设置角标成功。\n
部分rom需要在设置中同时开启`通知开关`和`桌面角标`配置,才允许设置角标,例如鸿蒙4.2。 \n
另外针对高版本小米设备,会借助创建通知栏消息来设置角标数,所以设置时需要注意是否有权限创建通知栏消息。
</text>
<button class="normal-button" type="default" @click="handleSetBadge"> <button class="normal-button" type="default" @click="handleSetBadge">
设置角标为5 | setAppBadgeNumber(5) 设置角标为5 | setAppBadgeNumber(5)
</button> </button>
...@@ -20,115 +26,162 @@ ...@@ -20,115 +26,162 @@
</button> </button>
<button class="normal-button" type="default" @click="handleGetClientId"> <button class="normal-button" type="default" @click="handleGetClientId">
获取cid | getPushClientId 获取cid | getPushClientId
</button> </button>
<textarea style="flex: 1;width: 100%;" :disabled="true" :value="channelInfo"></textarea> <textarea style="flex: 1;width: 100%;" :disabled="true" :value="channelInfo"></textarea>
</scroll-view> </scroll-view>
<!-- #endif --> <!-- #endif -->
</template> </template>
<script setup> <script setup>
const channelInfo = ref("")
const channelInfo = ref("")
const handleCreateChannel = (showToast: boolean) => { onMounted(() => {
uni.onPushMessage((res : OnPushMessageCallbackResult) => {
uni.showModal({
title: "onPushMessage回调信息",
content: `type:${res.type} \n data:${JSON.stringify(res.data)}`
})
})
})
const handleCreateChannel = (showToast : boolean) => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const manager = uni.getPushChannelManager() const manager = uni.getPushChannelManager()
manager.setPushChannel({ manager.setPushChannel({
channelId: "msg-pass", channelId: "msg-pass",
channelDesc: "留言审核通过", channelDesc: "留言审核通过",
} as SetPushChannelOptions) soundName: "#填写配置的声音文件名#",
if(showToast){ enableLights: true,
uni.showToast({ enableVibration: true,
title: "设置渠道成功" importance: 4,
}) lockscreenVisibility: 1
} as SetPushChannelOptions)
if (showToast) {
uni.showToast({
title: "设置渠道成功"
})
} }
// #endif // #endif
} }
const handleGetAllChannels = () => { const handleGetAllChannels = () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const manager = uni.getPushChannelManager() const manager = uni.getPushChannelManager()
console.log("channels : " + manager.getAllChannels()); console.log("channels : " + manager.getAllChannels());
channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}` channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}`
// #endif // #endif
} }
const handleCreateLocalNotification = () => { const handleCreateLocalNotification = () => {
// #ifdef APP-ANDROID
if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") { if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
// #endif handleCreateChannel(false)
handleCreateChannel(false) const date = new Date();
const date = new Date(); const hour = date.getHours()
const hour = date.getHours() const minute = date.getMinutes()
const minute = date.getMinutes() const second = date.getSeconds()
const second = date.getSeconds() const formateTime = (target : number) : string => {
const formateTime = (target: number): string =>{ return target < 10 ? `0${target}` : `${target}`
return target < 10 ? `0${target}` : `${target}`
} }
uni.createPushMessage({ uni.createPushMessage({
title: "主标题(title)", title: "主标题(title)",
content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`, content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`,
cover:false, cover: false,
channelId: "msg-pass", channelId: "msg-pass",
when: Date.now() + 10000, when: Date.now() + 10000,
icon: "/static/uni.png", icon: "/static/uni.png",
sound:"system", sound: "system",
delay:1, 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.hideToast()
uni.showToast({ uni.showToast({
title:"创建本地通知消息成功" title: "创建本地通知消息成功"
}) })
}, },
fail(e) { fail(e) {
console.log("fail :" + e); console.log("fail :" + e);
uni.hideToast() uni.hideToast()
uni.showToast({ uni.showToast({
title:"创建本地通知消息失败", title: "创建本地通知消息失败",
icon:"error" icon: "error"
}) })
} }
}) })
// #ifdef APP-ANDROID
} else { } else {
uni.showToast({ uni.showToast({
title: "请在设置中开启通知权限", title: "请在设置中开启通知权限",
icon: "error" icon: "error"
}) })
} }
// #endif
} }
const handleGetClientId = () => { const handleGetClientId = () => {
uni.getPushClientId({ uni.showLoading({
success:(res : GetPushClientIdSuccess)=>{ title: "正在获取cid",
uni.showModal({ })
title:"信息", uni.getPushClientId({
content: `获取cid : ${res.cid}` success: (res : GetPushClientIdSuccess) => {
}) uni.hideLoading()
}, uni.showModal({
fail:()=>{ title: "信息",
uni.showToast({ content: `cid : ${res.cid}`
title:`获取cid失败`, })
icon: "error" },
}) fail: () => {
uni.hideLoading()
uni.showToast({
title: `获取cid失败`,
icon: "error"
})
} }
}) })
} }
const handleSetBadge = () => { const handleSetBadge = () => {
uni.setAppBadgeNumber(5) if (uni.getDeviceInfo().deviceBrand?.toLowerCase() == "xiaomi") {
uni.showToast({ if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
title:"设置应用角标数为5" uni.setAppBadgeNumber(5, {
}) title: "AppName",
content: "您有5条未读消息"
} as BadgeOptions)
uni.showToast({
title: "设置应用角标数为5"
})
} else {
uni.showToast({
title: "请在设置中开启通知权限",
icon: "error"
})
}
} else {
uni.setAppBadgeNumber(5)
uni.showToast({
title: "设置应用角标数为5"
})
}
} }
const handleCleanBadge = () => { const handleCleanBadge = () => {
uni.setAppBadgeNumber(0) if (uni.getDeviceInfo().deviceBrand?.toLowerCase() == "xiaomi") {
uni.showToast({ if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
title:"清空应用角标数" uni.setAppBadgeNumber(0, {} as BadgeOptions)
}) uni.showToast({
title: "清空应用角标数"
})
}else{
uni.showToast({
title: "请在设置中开启通知权限",
icon: "error"
})
}
} else {
uni.setAppBadgeNumber(0)
uni.showToast({
title: "清空应用角标数"
})
}
} }
</script> </script>
...@@ -136,4 +189,11 @@ ...@@ -136,4 +189,11 @@
.normal-button { .normal-button {
width: 100%; width: 100%;
} }
.instructions {
margin-top: 10px;
margin-left: 10px;
margin-right: 10px;
background-color: #eee;
}
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册