index.uvue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <view>
  6. <!-- #ifdef APP-ANDROID -->
  7. <button class="normal-button" type="default" @click="handleCreateChannel(true)">
  8. 创建通知渠道 | setPushChannel
  9. </button>
  10. <button class="normal-button" type="default" @click="handleGetAllChannels">
  11. 获取所有通知渠道信息 | getAllChannels
  12. </button>
  13. <textarea style="width: 100%;" :disabled="true" :value="channelInfo"></textarea>
  14. <!-- #endif -->
  15. <!-- #ifdef APP -->
  16. <button class="normal-button" type="default" @click="handleCreateLocalNotification">
  17. 创建本地通知消息 | createPushMessage
  18. </button>
  19. <text class="instructions">
  20. 不同手机厂商的角标显示规则不同,有部分设备的rom版本不支持显示角标,另有部分rom需要在应用的通知管理里开启`桌面角标`配置,才可以设置角标成功。\n
  21. 部分rom需要在设置中同时开启`通知开关`和`桌面角标`配置,才允许设置角标,例如鸿蒙4.2。 \n
  22. 另外针对高版本小米设备,会借助创建通知栏消息来设置角标数,所以设置时需要注意是否有权限创建通知栏消息。
  23. </text>
  24. <!-- #endif -->
  25. <button class="normal-button uni-common-mb" type="default" @click="handleGetClientId">
  26. 获取cid | getPushClientId
  27. </button>
  28. <button class="normal-button" type="default" @click="handleOnPushMessage">
  29. 注册回调 | onPushMessage
  30. </button>
  31. <button class="normal-button" type="default" @click="handleOffPushMessage">
  32. 注销回调 | offPushMessage
  33. </button>
  34. </view>
  35. <!-- #ifdef APP -->
  36. </scroll-view>
  37. <!-- #endif -->
  38. </template>
  39. <script setup>
  40. // 自动化测试
  41. type TypeJestResult = {
  42. clientId : string,
  43. sendPushMessageRes : number,
  44. onPushMessageType:string,
  45. onPushMessageCallbackInfo:string
  46. }
  47. type TypeIsRegister = {
  48. state:boolean
  49. }
  50. const jestResult = reactive({
  51. clientId:"",
  52. sendPushMessageRes:-1,
  53. onPushMessageType:"",
  54. onPushMessageCallbackInfo:""
  55. } as TypeJestResult)
  56. // 自动化测试
  57. const autoTest = ref(false);
  58. const updateAutoTest = (value : boolean) => {
  59. autoTest.value = value
  60. }
  61. const channelInfo = ref("")
  62. const onPushMessageCallback = (res : OnPushMessageCallbackResult) => {
  63. // 自动化测试
  64. jestResult.onPushMessageType = res.type
  65. jestResult.onPushMessageCallbackInfo = JSON.stringify(res.data)
  66. if (!autoTest.value) {
  67. uni.showModal({
  68. title: "onPushMessage回调信息",
  69. content: `type:${res.type} \n data:${JSON.stringify(res.data)}`
  70. })
  71. }
  72. }
  73. // 为兼容Android测试例中能获取到,此处用reactive定义
  74. const isRegister = reactive({
  75. state:false
  76. } as TypeIsRegister);
  77. const handleOnPushMessage = () => {
  78. if (isRegister.state) {
  79. uni.showToast({
  80. icon: "error",
  81. title: "无需重复注册"
  82. })
  83. return
  84. }
  85. uni.onPushMessage(onPushMessageCallback)
  86. isRegister.state = true
  87. uni.showToast({
  88. title: "成功注册"
  89. })
  90. }
  91. const handleOffPushMessage = () => {
  92. if (!isRegister.state) {
  93. uni.showToast({
  94. icon: "error",
  95. title: "未注册, 无需注销"
  96. })
  97. return
  98. }
  99. uni.offPushMessage(onPushMessageCallback)
  100. isRegister.state = false
  101. uni.showToast({
  102. title: "成功注销"
  103. })
  104. }
  105. const handleCreateChannel = (showToast : boolean) => {
  106. // #ifdef APP-ANDROID
  107. const manager = uni.getPushChannelManager()
  108. manager.setPushChannel({
  109. channelId: "msg-pass",
  110. channelDesc: "留言审核通过",
  111. soundName: "#填写配置的声音文件名#",
  112. enableLights: true,
  113. enableVibration: true,
  114. importance: 4,
  115. lockscreenVisibility: 1
  116. } as SetPushChannelOptions)
  117. if (showToast) {
  118. uni.showToast({
  119. title: "设置渠道成功"
  120. })
  121. }
  122. // #endif
  123. }
  124. const handleGetAllChannels = () => {
  125. // #ifdef APP-ANDROID
  126. const manager = uni.getPushChannelManager()
  127. console.log("channels : " + manager.getAllChannels());
  128. channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}`
  129. // #endif
  130. }
  131. const handleCreateLocalNotification = () => {
  132. if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
  133. handleCreateChannel(false)
  134. const date = new Date();
  135. const hour = date.getHours()
  136. const minute = date.getMinutes()
  137. const second = date.getSeconds()
  138. const formateTime = (target : number) : string => {
  139. return target < 10 ? `0${target}` : `${target}`
  140. }
  141. uni.createPushMessage({
  142. title: "主标题(title)",
  143. content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`,
  144. cover: false,
  145. channelId: "msg-pass",
  146. when: Date.now() + 10000,
  147. icon: "/static/uni.png",
  148. sound: "system",
  149. delay: 1,
  150. payload: {
  151. pkey: "pvalue1"
  152. },
  153. // #ifdef APP-HARMONY
  154. category: "SOCIAL_COMMUNICATION",
  155. // #endif
  156. // #ifndef APP-HARMONY
  157. category: "IM",
  158. // #endif
  159. success(res) {
  160. console.log("res: " + res);
  161. uni.hideToast()
  162. uni.showToast({
  163. title: "创建本地通知消息成功"
  164. })
  165. },
  166. fail(e) {
  167. console.log("fail :" + e);
  168. uni.hideToast()
  169. uni.showToast({
  170. title: "创建本地通知消息失败",
  171. icon: "error"
  172. })
  173. }
  174. })
  175. } else {
  176. uni.showToast({
  177. title: "请在设置中开启通知权限",
  178. icon: "error"
  179. })
  180. }
  181. }
  182. async function getPushClientId(): Promise<string>{
  183. let pushClientId = '';
  184. let res:void = await new Promise<void>(resolve => {
  185. uni.getPushClientId({
  186. success: (res: GetPushClientIdSuccess) => {
  187. console.log(res.cid)
  188. pushClientId = res.cid
  189. resolve()
  190. },
  191. fail: (err: GetPushClientIdFail) => {
  192. resolve()
  193. console.error(err);
  194. if (err.message.includes('uniPush is not enabled')) {
  195. uni.showModal({
  196. title: '获取cid失败',
  197. content: '当前项目未启用uni-push,检查manifest.json中的uni-push配置',
  198. showCancel: false
  199. });
  200. } else if (err.message.includes('getPushClientId:fail register fail: {\"errorCode\":1,\"errorMsg\":\"\"}')) {
  201. uni.showModal({
  202. title: '获取cid失败',
  203. 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',
  204. showCancel: false
  205. });
  206. } else {
  207. uni.showToast({
  208. title: `获取cid失败`,
  209. icon: "error"
  210. })
  211. }
  212. }
  213. })
  214. })
  215. return pushClientId
  216. }
  217. const handleGetClientId = async():Promise<void> =>{
  218. uni.showLoading({
  219. title: "正在获取cid",
  220. })
  221. const cid = await getPushClientId()
  222. if (cid != '') {
  223. // 自动化测试
  224. jestResult.clientId = cid
  225. if (!autoTest.value) {
  226. uni.showModal({
  227. title: "获取cid",
  228. content: "获取cid成功" + cid,
  229. showCancel: false
  230. })
  231. }
  232. }
  233. uni.hideLoading()
  234. }
  235. // 自动化测试
  236. defineExpose({
  237. jestResult,
  238. autoTest,
  239. updateAutoTest,
  240. isRegister,
  241. handleGetClientId,
  242. handleOnPushMessage,
  243. handleOffPushMessage
  244. })
  245. </script>
  246. <style>
  247. .normal-button {
  248. width: 100%;
  249. }
  250. .instructions {
  251. margin-top: 10px;
  252. margin-left: 10px;
  253. margin-right: 10px;
  254. background-color: #eee;
  255. }
  256. </style>