uniapp vue3 unipush2.0 调用系统通知功能流程

进入dcloud官网:

复制代码
https://dev.dcloud.net.cn/

添加uniCloud服务空间:

这里是勾选了两个

选择刚刚创建的服务空间

根据官方提供添加3个表

开始编写代码

复制代码
// 简单的使用示例
'use strict';
const uniPush = uniCloud.getPushManager({appId:"__UNI__xxxxxxx"}) //注意这里需要传入你的应用appId,用于指定接收消息的客户端
exports.main = async (event, context) => {
	return await uniPush.sendMessage({
		"push_clientid": "d946cbxxxxxxxxxxxxxxxxx",//在APP.vue文件调用uni.getPushClientId方法中获取
		"title": "系统通知",	
		"content": "系统通知内容",
		"payload": {
			"text":"体验一下uni-push2.0"
		}
	})
};

App.vue代码:

复制代码
<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
			//#ifdef APP-PLUS
			checkNotificationAuthorized();
			//#endif
			uni.getPushClientId({
				success: (res) => {
					console.log(11111, res);
					uni.setStorageSync("cid", res.cid)
				},
				fail(err) {
					console.log(err)
				}
			});
			uni.onPushMessage((res) => {
				// 监听通知栏消息的点击  
				if (res.type == 'click') {
					// 如果需要跳转app内指定页面,则自己实现下方的跳转代码。  
					uni.navigateTo({
						url: '点击跳转的页面'
					})
				}
				// 监听在线推送消息,若云函数设置了 "force_notification":true,则不会触发此 receive。  
				if (res.type == 'receive') {
					console.log("接收到的消息内容", res.payload);
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},


	}
	// 检查app是否开启了通知权限 安卓苹果通用
	function checkNotificationAuthorized() {
		const notificationAuthorized = uni.getAppAuthorizeSetting().notificationAuthorized
		if (notificationAuthorized !== 'authorized') {
			uni.showModal({
				title: '通知权限',
				content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
				confirmText: '去设置',
				showCancel: false,
				success: (res) => {
					if (res.confirm) uni.openAppAuthorizeSetting()
				}
			});
		}
	}
</script>

发送通知代码:

复制代码
<template>
	<view class="head">
		<button type="default" @click="onMessageInform">通知</button>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		watch
	} from "vue"
	// 通知
	const onMessageInform = async () => {
		uni.createPushMessage({
			title: "测试通知功能",
			content: "发送成功啦",
			success: (res) => {
				console.log("发送成功", res);
			},
			fail(err) {
				console.log(err)
			}
		})
	}
</script>

然后我们用手机运行app

相关推荐
小菜今天没吃饱1 小时前
DVWA-XSS(DOM)
前端·javascript·xss·dvwa
q***04631 小时前
Spring Cloud Alibaba 组件版本选择
android·前端·后端
李少兄1 小时前
解决 `npm install` 卡在 `idealTree: sill idealTree buildDeps` 的排查与修复
前端·npm·node.js
毕设十刻1 小时前
基于Vue的企业管理系统pk6uy(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
27669582921 小时前
雷池waf 逆向
java·开发语言·前端·python·wasm·waf·雷池waf
w***48821 小时前
解决报错net.sf.jsqlparser.statement.select.SelectBody
android·前端·后端
MisterZhang6661 小时前
vue3 markMap使用
vue.js·markmap
u***42071 小时前
【学术会议论文投稿】Spring Boot实战:零基础打造你的Web应用新纪元
前端·spring boot·后端
v***91301 小时前
数据库高安全—openGauss安全整体架构&安全认证
android·前端·后端