提交 61a2e8ac 编写于 作者: DCloud_JSON's avatar DCloud_JSON

update uni-push补充sendPushMessage、sendPushMessage、offPushMessage示例

上级 782e7d2b
...@@ -27,9 +27,18 @@ ...@@ -27,9 +27,18 @@
<button class="normal-button" type="default" @click="handleCleanBadge"> <button class="normal-button" type="default" @click="handleCleanBadge">
清空角标 | setAppBadgeNumber(0) 清空角标 | setAppBadgeNumber(0)
</button> </button>
<!-- #endif --> <!-- #endif -->
<button class="normal-button" type="default" @click="handleSendPushMessage">
发送通知消息 | sendPushMessage
</button>
<button class="normal-button uni-common-mb" type="default" @click="handleGetClientId"> <button class="normal-button uni-common-mb" type="default" @click="handleGetClientId">
获取cid | getPushClientId 获取cid | getPushClientId
</button>
<button class="normal-button" type="default" @click="handleOnPushMessage">
注册回调 | onPushMessage
</button>
<button class="normal-button" type="default" @click="handleOffPushMessage">
注销回调 | offPushMessage
</button> </button>
</view> </view>
<!-- #ifdef APP --> <!-- #ifdef APP -->
...@@ -39,15 +48,44 @@ ...@@ -39,15 +48,44 @@
<script setup> <script setup>
const channelInfo = ref("") const channelInfo = ref("")
onMounted(() => { const onPushMessageCallback = (res : OnPushMessageCallbackResult) => {
uni.onPushMessage((res : OnPushMessageCallbackResult) => { uni.showModal({
uni.showModal({ title: "onPushMessage回调信息",
title: "onPushMessage回调信息", content: `type:${res.type} \n data:${JSON.stringify(res.data)}`
content: `type:${res.type} \n data:${JSON.stringify(res.data)}`
})
}) })
}) }
let isRegister = false
const handleOnPushMessage = () => {
if (isRegister) {
uni.showToast({
icon: "error",
title: "无需重复注册"
})
return
}
uni.onPushMessage(onPushMessageCallback)
isRegister = true
uni.showToast({
title: "成功注册"
})
}
const handleOffPushMessage = () => {
if (!isRegister) {
uni.showToast({
icon: "error",
title: "未注册, 无需注销"
})
return
}
uni.offPushMessage(onPushMessageCallback)
isRegister = false
uni.showToast({
title: "成功注销"
})
}
const handleCreateChannel = (showToast : boolean) => { const handleCreateChannel = (showToast : boolean) => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
...@@ -120,32 +158,77 @@ ...@@ -120,32 +158,77 @@
icon: "error" icon: "error"
}) })
} }
} }
const handleGetClientId = () => {
uni.showLoading({ async function getPushClientId(): Promise<string>{
title: "正在获取cid", let pushClientId = '';
}) let res:void = await new Promise(resolve => {
uni.getPushClientId({ uni.getPushClientId({
success: (res : GetPushClientIdSuccess) => { success: (res: GetPushClientIdSuccess) => {
uni.hideLoading() console.log(res.cid)
uni.showModal({ pushClientId = res.cid
title: "信息", resolve()
content: `cid : ${res.cid}` },
}) fail: (err: GetPushClientIdFail) => {
}, resolve()
fail: (err) => { console.error(err);
if (err.message.includes('uniPush is not enabled')) { if (err.message.includes('uniPush is not enabled')) {
console.error('请先开通uni-push,详见文档:https://uniapp.dcloud.net.cn/unipush-v2.html#%E7%AC%AC%E4%B8%80%E6%AD%A5-%E5%BC%80%E9%80%9A'); uni.showModal({
} else { title: '获取cid失败',
console.error(err); content: '当前项目未启用uni-push,检查manifest.json中的uni-push配置',
} showCancel: false
uni.hideLoading() });
uni.showToast({ } else if (err.message.includes('getPushClientId:fail register fail: {\"errorCode\":1,\"errorMsg\":\"\"}')) {
title: `获取cid失败`, uni.showModal({
icon: "error" title: '获取cid失败',
}) content: '当前项目未开通uni-push,开通文档:https://uniapp.dcloud.net.cn/unipush-v2.html#%E7%AC%AC%E4%B8%80%E6%AD%A5-%E5%BC%80%E9%80%9A',
} showCancel: false
}) });
} else {
uni.showToast({
title: `获取cid失败`,
icon: "error"
})
}
}
})
})
return pushClientId
}
const handleGetClientId = async():Promise<void> =>{
uni.showLoading({
title: "正在获取cid",
})
const cid = await getPushClientId()
if (cid != '') {
uni.showModal({
title: "获取cid",
content: "获取cid成功" + cid,
showCancel: false
})
}
uni.hideLoading()
}
const handleSendPushMessage = async():Promise<void>=> {
const pushClientId = await getPushClientId()
if (pushClientId == ''){
return
}
const uniPushCo = uniCloud.importObject("uni-push-co")
try {
await uniPushCo.sendPushMessage(pushClientId)
uni.showToast({
title: "发送通知消息成功"
})
} catch (e) {
console.error(e);
// console.error(e["errorCode"] as Number );
uni.showToast({
title: "发送通知消息失败",
icon: "error"
})
}
} }
const handleSetBadge = () => { const handleSetBadge = () => {
if (uni.getDeviceInfo().deviceBrand?.toLowerCase() == "xiaomi") { if (uni.getDeviceInfo().deviceBrand?.toLowerCase() == "xiaomi") {
......
module.exports = {
async sendPushMessage(cid) {
// 防止非法调用
if (!cid) {
throw new Error('请传入推送客户端ID')
}
if (typeof cid !== 'string') {
throw new Error('推送客户端ID必须为字符串')
}
const pushManager = uniCloud.getPushManager({appId: '__UNI__HelloUniAppX'})
const res = await pushManager.sendMessage({
push_clientid: cid,
title: '测试推送标题',
content: '测试推送内容',
payload: {
data: '测试推送数据'
}
})
return res
}
}
{
"name": "uni-push-co",
"dependencies": {},
"extensions": {
"uni-cloud-jql": {},
"uni-cloud-push": {}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册