uni-push.uvue 6.1 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
    </button>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
15
    <text class="instructions">
DCloud-WZF's avatar
DCloud-WZF 已提交
16
      不同手机厂商的角标显示规则不同,有部分设备的rom版本不支持显示角标,另有部分rom需要在应用的通知管理里开启`桌面角标`配置,才可以设置角标成功。\n
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
17
      部分rom需要在设置中同时开启`通知开关`和`桌面角标`配置,才允许设置角标,例如鸿蒙4.2。 \n
DCloud-WZF's avatar
DCloud-WZF 已提交
18
      另外针对高版本小米设备,会借助创建通知栏消息来设置角标数,所以设置时需要注意是否有权限创建通知栏消息。
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
19 20
    </text>

DCloud-WZF's avatar
DCloud-WZF 已提交
21
    <button class="normal-button" type="default" @click="handleSetBadge">
22
      设置角标为5 | setAppBadgeNumber(5)
DCloud-WZF's avatar
DCloud-WZF 已提交
23 24
    </button>
    <button class="normal-button" type="default" @click="handleCleanBadge">
25
      清空角标 | setAppBadgeNumber(0)
DCloud-WZF's avatar
DCloud-WZF 已提交
26 27
    </button>
    <button class="normal-button" type="default" @click="handleGetClientId">
28
      获取cid | getPushClientId
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
29 30
    </button>

31
    <textarea style="flex: 1;width: 100%;" :disabled="true" :value="channelInfo"></textarea>
DCloud-WZF's avatar
DCloud-WZF 已提交
32 33 34 35
  </scroll-view>
  <!-- #endif -->
</template>

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
36 37
<script setup>
  const channelInfo = ref("")
38

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
39 40 41 42 43 44 45 46 47 48
  onMounted(() => {
    uni.onPushMessage((res : OnPushMessageCallbackResult) => {
      uni.showModal({
        title: "onPushMessage回调信息",
        content: `type:${res.type} \n data:${JSON.stringify(res.data)}`
      })
    })
  })

  const handleCreateChannel = (showToast : boolean) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
49
    // #ifdef APP-ANDROID
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
50
    const manager = uni.getPushChannelManager()
DCloud-WZF's avatar
DCloud-WZF 已提交
51 52 53
    manager.setPushChannel({
      channelId: "msg-pass",
      channelDesc: "留言审核通过",
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
54 55 56 57 58 59 60 61 62 63
      soundName: "#填写配置的声音文件名#",
      enableLights: true,
      enableVibration: true,
      importance: 4,
      lockscreenVisibility: 1
    } as SetPushChannelOptions)
    if (showToast) {
      uni.showToast({
        title: "设置渠道成功"
      })
64
    }
DCloud-WZF's avatar
DCloud-WZF 已提交
65 66 67 68
    // #endif
  }
  const handleGetAllChannels = () => {
    // #ifdef APP-ANDROID
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
69
    const manager = uni.getPushChannelManager()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
70
    console.log("channels : " + manager.getAllChannels());
71
    channelInfo.value = `渠道信息为: \n ${manager.getAllChannels()}`
DCloud-WZF's avatar
DCloud-WZF 已提交
72 73 74 75
    // #endif
  }
  const handleCreateLocalNotification = () => {
    if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
76 77 78 79 80 81 82
      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}`
83
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
84
      uni.createPushMessage({
85 86
        title: "主标题(title)",
        content: `内容(content),创建时间: ${formateTime(hour)}:${formateTime(minute)}:${formateTime(second)}`,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
87
        cover: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
88 89
        channelId: "msg-pass",
        when: Date.now() + 10000,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
90 91 92
        icon: "/static/uni.png",
        sound: "system",
        delay: 1,
DCloud-WZF's avatar
DCloud-WZF 已提交
93 94 95 96 97
        payload: {
          pkey: "pvalue1"
        },
        category: "IM",
        success(res) {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
98 99 100 101
          console.log("res: " + res);
          uni.hideToast()
          uni.showToast({
            title: "创建本地通知消息成功"
102
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
103 104
        },
        fail(e) {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
105 106 107 108 109
          console.log("fail :" + e);
          uni.hideToast()
          uni.showToast({
            title: "创建本地通知消息失败",
            icon: "error"
110
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
111 112 113 114 115 116 117 118 119 120
        }
      })
    } else {
      uni.showToast({
        title: "请在设置中开启通知权限",
        icon: "error"
      })
    }
  }
  const handleGetClientId = () => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    uni.showLoading({
      title: "正在获取cid",
    })
    uni.getPushClientId({
      success: (res : GetPushClientIdSuccess) => {
        uni.hideLoading()
        uni.showModal({
          title: "信息",
          content: `cid : ${res.cid}`
        })
      },
      fail: () => {
        uni.hideLoading()
        uni.showToast({
          title: `获取cid失败`,
          icon: "error"
        })
DCloud-WZF's avatar
DCloud-WZF 已提交
138 139 140 141
      }
    })
  }
  const handleSetBadge = () => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    if (uni.getDeviceInfo().deviceBrand?.toLowerCase() == "xiaomi") {
      if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
        uni.setAppBadgeNumber(5, {
          title: "AppName",
          content: "您有5条未读消息"
        } as BadgeOptions)
        uni.showToast({
          title: "设置应用角标数为5"
        })
      } else {
        uni.showToast({
          title: "请在设置中开启通知权限",
          icon: "error"
        })
      }

    } else {
      uni.setAppBadgeNumber(5)
      uni.showToast({
        title: "设置应用角标数为5"
      })
    }
DCloud-WZF's avatar
DCloud-WZF 已提交
164 165
  }
  const handleCleanBadge = () => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
166 167 168 169 170 171
    if (uni.getDeviceInfo().deviceBrand?.toLowerCase() == "xiaomi") {
      if (uni.getAppAuthorizeSetting().notificationAuthorized == "authorized") {
        uni.setAppBadgeNumber(0, {} as BadgeOptions)
        uni.showToast({
          title: "清空应用角标数"
        })
DCloud-WZF's avatar
DCloud-WZF 已提交
172 173 174 175 176
      } else {
        uni.showToast({
          title: "请在设置中开启通知权限",
          icon: "error"
        })
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
177 178 179 180 181 182 183 184
      }
    } else {
      uni.setAppBadgeNumber(0)
      uni.showToast({
        title: "清空应用角标数"
      })
    }

DCloud-WZF's avatar
DCloud-WZF 已提交
185 186 187 188 189 190 191
  }
</script>

<style>
  .normal-button {
    width: 100%;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
192 193 194 195 196 197 198

  .instructions {
    margin-top: 10px;
    margin-left: 10px;
    margin-right: 10px;
    background-color: #eee;
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
199
</style>