uni-push.uvue 6.5 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
4 5 6
  <!-- #endif -->
  <view>
    <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
7
    <!-- #ifdef APP-ANDROID -->
8 9
    <button class="normal-button" type="default" @click="handleCreateChannel(true)">
      创建通知渠道 | setPushChannel
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11
    </button>
    <button class="normal-button" type="default" @click="handleGetAllChannels">
12
      获取所有通知渠道信息 | getAllChannels
DCloud-WZF's avatar
DCloud-WZF 已提交
13
    </button>
14
    <textarea style="width: 100%;" :disabled="true" :value="channelInfo"></textarea>
DCloud-WZF's avatar
DCloud-WZF 已提交
15 16
    <!-- #endif -->
    <button class="normal-button" type="default" @click="handleCreateLocalNotification">
17
      创建本地通知消息 | createPushMessage
DCloud-WZF's avatar
DCloud-WZF 已提交
18
    </button>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
19
    <text class="instructions">
DCloud-WZF's avatar
DCloud-WZF 已提交
20
      不同手机厂商的角标显示规则不同,有部分设备的rom版本不支持显示角标,另有部分rom需要在应用的通知管理里开启`桌面角标`配置,才可以设置角标成功。\n
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
21
      部分rom需要在设置中同时开启`通知开关`和`桌面角标`配置,才允许设置角标,例如鸿蒙4.2。 \n
DCloud-WZF's avatar
DCloud-WZF 已提交
22
      另外针对高版本小米设备,会借助创建通知栏消息来设置角标数,所以设置时需要注意是否有权限创建通知栏消息。
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
23
    </text>
DCloud-WZF's avatar
DCloud-WZF 已提交
24
    <button class="normal-button" type="default" @click="handleSetBadge">
25
      设置角标为5 | setAppBadgeNumber(5)
DCloud-WZF's avatar
DCloud-WZF 已提交
26 27
    </button>
    <button class="normal-button" type="default" @click="handleCleanBadge">
28
      清空角标 | setAppBadgeNumber(0)
DCloud-WZF's avatar
DCloud-WZF 已提交
29
    </button>
30 31
    <!-- #endif -->
    <button class="normal-button uni-common-mb" type="default" @click="handleGetClientId">
32
      获取cid | getPushClientId
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
33
    </button>
34 35
  </view>
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
36 37 38 39
  </scroll-view>
  <!-- #endif -->
</template>

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
40 41
<script setup>
  const channelInfo = ref("")
42

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

DCloud-WZF's avatar
DCloud-WZF 已提交
194 195 196 197 198 199 200
  }
</script>

<style>
  .normal-button {
    width: 100%;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
201 202 203 204 205 206 207

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