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

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

上级 782e7d2b
......@@ -28,9 +28,18 @@
清空角标 | setAppBadgeNumber(0)
</button>
<!-- #endif -->
<button class="normal-button" type="default" @click="handleSendPushMessage">
发送通知消息 | sendPushMessage
</button>
<button class="normal-button uni-common-mb" type="default" @click="handleGetClientId">
获取cid | getPushClientId
</button>
<button class="normal-button" type="default" @click="handleOnPushMessage">
注册回调 | onPushMessage
</button>
<button class="normal-button" type="default" @click="handleOffPushMessage">
注销回调 | offPushMessage
</button>
</view>
<!-- #ifdef APP -->
</scroll-view>
......@@ -40,14 +49,43 @@
<script setup>
const channelInfo = ref("")
onMounted(() => {
uni.onPushMessage((res : OnPushMessageCallbackResult) => {
const onPushMessageCallback = (res : OnPushMessageCallbackResult) => {
uni.showModal({
title: "onPushMessage回调信息",
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) => {
// #ifdef APP-ANDROID
......@@ -121,31 +159,76 @@
})
}
}
const handleGetClientId = () => {
async function getPushClientId(): Promise<string>{
let pushClientId = '';
let res:void = await new Promise(resolve => {
uni.getPushClientId({
success: (res: GetPushClientIdSuccess) => {
console.log(res.cid)
pushClientId = res.cid
resolve()
},
fail: (err: GetPushClientIdFail) => {
resolve()
console.error(err);
if (err.message.includes('uniPush is not enabled')) {
uni.showModal({
title: '获取cid失败',
content: '当前项目未启用uni-push,检查manifest.json中的uni-push配置',
showCancel: false
});
} else if (err.message.includes('getPushClientId:fail register fail: {\"errorCode\":1,\"errorMsg\":\"\"}')) {
uni.showModal({
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",
})
uni.getPushClientId({
success: (res : GetPushClientIdSuccess) => {
uni.hideLoading()
const cid = await getPushClientId()
if (cid != '') {
uni.showModal({
title: "信息",
content: `cid : ${res.cid}`
title: "获取cid",
content: "获取cid成功" + cid,
showCancel: false
})
},
fail: (err) => {
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');
} else {
console.error(err);
}
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: `获取cid失败`,
title: "发送通知消息成功"
})
} catch (e) {
console.error(e);
// console.error(e["errorCode"] as Number );
uni.showToast({
title: "发送通知消息失败",
icon: "error"
})
}
})
}
const handleSetBadge = () => {
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.
先完成此消息的编辑!
想要评论请 注册