Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
61a2e8ac
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
61a2e8ac
编写于
11月 21, 2024
作者:
DCloud_JSON
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update uni-push补充sendPushMessage、sendPushMessage、offPushMessage示例
上级
782e7d2b
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
147 addition
and
35 deletion
+147
-35
pages/API/uni-push/uni-push.uvue
pages/API/uni-push/uni-push.uvue
+118
-35
uniCloud-aliyun/cloudfunctions/uni-push-co/index.obj.js
uniCloud-aliyun/cloudfunctions/uni-push-co/index.obj.js
+21
-0
uniCloud-aliyun/cloudfunctions/uni-push-co/package.json
uniCloud-aliyun/cloudfunctions/uni-push-co/package.json
+8
-0
未找到文件。
pages/API/uni-push/uni-push.uvue
浏览文件 @
61a2e8ac
...
@@ -28,9 +28,18 @@
...
@@ -28,9 +28,18 @@
清空角标 | 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>
<button
class=
"normal-button"
type=
"default"
@
click=
"handleOnPushMessage"
>
注册回调 | onPushMessage
</button>
<button
class=
"normal-button"
type=
"default"
@
click=
"handleOffPushMessage"
>
注销回调 | offPushMessage
</button>
</view>
</view>
<!-- #ifdef APP -->
<!-- #ifdef APP -->
</scroll-view>
</scroll-view>
...
@@ -40,14 +49,43 @@
...
@@ -40,14 +49,43 @@
<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
...
@@ -121,31 +159,76 @@
...
@@ -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
({
uni
.
showLoading
({
title
:
"
正在获取cid
"
,
title
:
"
正在获取cid
"
,
})
})
uni.getPushClientId({
const
cid
=
await
getPushClientId
()
success: (res : GetPushClientIdSuccess) => {
if
(
cid
!=
''
)
{
uni.hideLoading()
uni
.
showModal
({
uni
.
showModal
({
title: "信息",
title
:
"
获取cid
"
,
content: `cid : ${res.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
()
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
({
uni
.
showToast
({
title: `获取cid失败`,
title
:
"
发送通知消息成功
"
})
}
catch
(
e
)
{
console
.
error
(
e
);
// console.error(e["errorCode"] as Number );
uni
.
showToast
({
title
:
"
发送通知消息失败
"
,
icon
:
"
error
"
icon
:
"
error
"
})
})
}
}
})
}
}
const
handleSetBadge
=
()
=>
{
const
handleSetBadge
=
()
=>
{
if
(
uni
.
getDeviceInfo
().
deviceBrand
?.
toLowerCase
()
==
"
xiaomi
"
)
{
if
(
uni
.
getDeviceInfo
().
deviceBrand
?.
toLowerCase
()
==
"
xiaomi
"
)
{
...
...
uniCloud-aliyun/cloudfunctions/uni-push-co/index.obj.js
0 → 100644
浏览文件 @
61a2e8ac
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
}
}
uniCloud-aliyun/cloudfunctions/uni-push-co/package.json
0 → 100644
浏览文件 @
61a2e8ac
{
"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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录