index.uvue 2.6 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2 3 4
<template>
	<view class="content">
		<button class="normal-button" type="default" @click="handleCreateChannel">创建通知渠道</button>
		<button class="normal-button" type="default" @click="handleGetAllChannels">获取所有通知渠道信息</button>
5 6 7
		<button class="normal-button" type="default" @click="handleCreateLocalNotification">创建本地通知消息</button>
		<button class="normal-button" type="default" @click="handleSetBadge">设置角标</button>
		<button class="normal-button" type="default" @click="handleCleanBadge">清空角标</button>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
		<button class="normal-button" type="default" @click="handleGetClientId">获取cid</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isXM: false
			}
		},
		onLoad() {
			uni.onPushMessage((res) => {
				console.log("onpushmessage1 : " + JSON.stringify(res));
			})
		},
		methods: {
25 26
			handleCreateChannel() {
				// #ifdef APP-ANDROID
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
27 28 29 30 31
				const manager = uni.getChannelManager()
				manager.setPushChannel({
					channelId: this.isXM ? "112735" : "msg-pass",
					channelDesc: "留言审核通过",
					soundName: "pushsound"
32 33
				} as SetPushChannelOptions)
				// #endif
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
34
			},
35 36
			handleGetAllChannels() {
				// #ifdef APP-ANDROID
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
37
				const manager = uni.getChannelManager()
38 39
				console.log("channels : " + manager.getAllChannels());
				// #endif
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
40
			},
41
			handleCreateLocalNotification() {
42
				// #ifdef APP-ANDROID
43
				if(uni.getAppAuthorizeSetting().notificationAuthorized == "authorized"){
44 45
				// #endif
				
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
					uni.createPushMessage({
						title: "halo",
						content: "world",
						// cover:true,
						channelId: this.isXM ? "112735" : "msg-pass",
						when: Date.now() + 10000,
						icon: "/static/logo.jpg",
						// delay:5,
						payload: {
							pkey: "pvalue1"
						},
						category: "IM",
						success(res) {
							console.log("res: " + res);
						},
						fail(e) {
							console.log("fail :" + e);
						}
					})
65
					// #ifdef APP-ANDROID
66 67 68 69 70 71
				}else{
					uni.showToast({
						title:"请在设置中开启通知权限",
						icon:"error"
					})
				}
72
				// #endif
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
73 74 75 76 77 78 79
			},
			handleGetClientId() {
				uni.getPushClientId({
					complete(e : any) {
						console.log(e);
					}
				})
80 81 82 83 84 85 86
			},
			handleSetBadge(){
				uni.setAppBadgeNumber(8)
			},
			handleCleanBadge(){
				uni.setAppBadgeNumber(0)
			}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		}
	}
</script>

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

	.content {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-bottom: 50rpx;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>