uni-push.uvue 3.9 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #ifdef APP-ANDROID -->
5 6
    <button class="normal-button" type="default" @click="handleCreateChannel(true)">
      创建通知渠道 | setPushChannel
DCloud-WZF's avatar
DCloud-WZF 已提交
7 8
    </button>
    <button class="normal-button" type="default" @click="handleGetAllChannels">
9
      获取所有通知渠道信息 | getAllChannels
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11 12
    </button>
    <!-- #endif -->
    <button class="normal-button" type="default" @click="handleCreateLocalNotification">
13
      创建本地通知消息 | createPushMessage
DCloud-WZF's avatar
DCloud-WZF 已提交
14 15
    </button>
    <button class="normal-button" type="default" @click="handleSetBadge">
16
      设置角标为5 | setAppBadgeNumber(5)
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18
    </button>
    <button class="normal-button" type="default" @click="handleCleanBadge">
19
      清空角标 | setAppBadgeNumber(0)
DCloud-WZF's avatar
DCloud-WZF 已提交
20 21
    </button>
    <button class="normal-button" type="default" @click="handleGetClientId">
22 23 24 25
      获取cid | getPushClientId
    </button>

    <textarea style="flex: 1;width: 100%;" :disabled="true" :value="channelInfo"></textarea>
DCloud-WZF's avatar
DCloud-WZF 已提交
26 27 28 29
  </scroll-view>
  <!-- #endif -->
</template>

30 31 32 33 34
<script setup>

  const channelInfo = ref("")

  const handleCreateChannel = (showToast: boolean) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
35
    // #ifdef APP-ANDROID
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
36
    const manager = uni.getPushChannelManager()
DCloud-WZF's avatar
DCloud-WZF 已提交
37 38 39
    manager.setPushChannel({
      channelId: "msg-pass",
      channelDesc: "留言审核通过",
40 41 42 43 44 45
    } as SetPushChannelOptions)
    if(showToast){
      uni.showToast({
        title: "设置渠道成功"
      })
    }
DCloud-WZF's avatar
DCloud-WZF 已提交
46 47 48 49
    // #endif
  }
  const handleGetAllChannels = () => {
    // #ifdef APP-ANDROID
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
50
    const manager = uni.getPushChannelManager()
51 52
    console.log("channels : " + manager.getAllChannels());
    channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}`
DCloud-WZF's avatar
DCloud-WZF 已提交
53 54 55 56 57 58
    // #endif
  }
  const handleCreateLocalNotification = () => {
    // #ifdef APP-ANDROID
    if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
      // #endif
59 60 61 62 63 64 65 66
      handleCreateChannel(false)
      const date = new Date();
      const hour = date.getHours()
      const minute = date.getMinutes()
      const second = date.getSeconds()
      const formateTime = (target: number): string =>{
        return target < 10 ? `0${target}` : `${target}`
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
67
      uni.createPushMessage({
68 69 70
        title: "主标题(title)",
        content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`,
        cover:false,
DCloud-WZF's avatar
DCloud-WZF 已提交
71 72
        channelId: "msg-pass",
        when: Date.now() + 10000,
73 74 75
        icon: "/static/uni.png",
        sound:"system",
        delay:1,
DCloud-WZF's avatar
DCloud-WZF 已提交
76 77 78 79 80
        payload: {
          pkey: "pvalue1"
        },
        category: "IM",
        success(res) {
81 82 83 84 85
          console.log("res: " + res);
          uni.hideToast()
          uni.showToast({
            title:"创建本地通知消息成功"
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
86 87
        },
        fail(e) {
88 89 90 91 92 93
          console.log("fail :" + e);
          uni.hideToast()
          uni.showToast({
            title:"创建本地通知消息失败",
            icon:"error"
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
94 95 96 97 98 99 100 101 102 103 104 105
        }
      })
      // #ifdef APP-ANDROID
    } else {
      uni.showToast({
        title: "请在设置中开启通知权限",
        icon: "error"
      })
    }
    // #endif
  }
  const handleGetClientId = () => {
106 107 108 109 110 111 112 113 114 115 116 117
    uni.getPushClientId({
      success:(res : GetPushClientIdSuccess)=>{
        uni.showModal({
          title:"信息",
          content: `获取cid : ${res.cid}`
        })
      },
      fail:()=>{
        uni.showToast({
          title:`获取cid失败`,
          icon: "error"
        })
DCloud-WZF's avatar
DCloud-WZF 已提交
118 119 120 121
      }
    })
  }
  const handleSetBadge = () => {
122 123 124 125
    uni.setAppBadgeNumber(5)
    uni.showToast({
      title:"设置应用角标数为5"
    })
DCloud-WZF's avatar
DCloud-WZF 已提交
126 127
  }
  const handleCleanBadge = () => {
128 129 130 131
    uni.setAppBadgeNumber(0)
    uni.showToast({
      title:"清空应用角标数"
    })
DCloud-WZF's avatar
DCloud-WZF 已提交
132 133 134 135 136 137 138 139
  }
</script>

<style>
  .normal-button {
    width: 100%;
  }
</style>