当前位置: 首页 > ds >正文

学习路之uniapp--unipush2.0推送功能--使用

学习路之uniapp--unipush2.0推送功能--服务端推送消息

  • 一、绑定云空间及创建云函数
  • 二、编写发送界面
  • 三、效果

一、绑定云空间及创建云函数

在这里插入图片描述

在这里插入图片描述

  • package.json
{"name": "server-push","dependencies": {},"main": "index.js","extensions": {"uni-cloud-push": {}}
}
  • index.js
'use strict';
const uniPush = uniCloud.getPushManager({appId:"__UNI__5630355"}) //注意这里需要传入你的应用appId,用于指定接收消息的客户端
exports.main = async (event, context) => {//event为客户端上传的参数console.log('event : ', event);let obj = JSON.parse(event.body)console.log('obj : ', obj);const res = await uniPush.sendMessage({"push_clientid": obj.cids, // 设备id,支持多个以数组的形式指定多个设备,如["cid-1","cid-2"],数组长度不大于1000  "title": obj.title, // 标题  "content": obj.content, // 内容  "payload": obj.data, // 数据  "force_notification": true, // 服务端推送 需要加这一句  "request_id": obj.request_id //请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失   })//返回数据给客户端return res
};

二、编写发送界面

<template><view class="content"><view class="text-area"><text class="title">通知标题:</text><input class="uni-input" v-model="title" placeholder="输入标题" /></view><view class="text-area"><text class="title">通知内容:</text><input class="uni-input" v-model="content" placeholder="输入标题" /></view><text class="title">接收的设备:</text><text v-for="(item, index) in deviceIds"  >{{item}}</text><button @click="send">发送</button></view>
</template><script>export default {data() {return {deviceIds:[],//接收消息的设备title: '我是通知标题',content:'我是通知内容'+Math.floor(Math.random() * 100),}},onLoad() {uni.getPushClientId({success: (res) => {let push_clientid = res.cidconsole.log('客户端推送标识cid:', push_clientid,typeof push_clientid)this.deviceIds.push(push_clientid)}})},methods: {send(){console.log('aaaaaaaa');let send_data = {cids: this.deviceIds, // 使用存储的设备 IDtitle: this.title,content: this.content,data: {},request_id: "",};uniCloud.callFunction({name: "server-push", // 填写你自己的云函数名称data: {body: JSON.stringify(send_data) // 将数据转换为 JSON 字符串并赋值给 body},success(dataRes) {console.log("云函数返回的参数", dataRes)},fail(err) {console.log("云函数报错", err)},complete() {}})}}}
</script><style>.content {display: flex;flex-direction: column;align-items: center;justify-content: center;}.text-area {display: flex;justify-content: center;}.title {font-size: 36rpx;color: #8f8f94;}
</style>

三、效果

http://www.xdnf.cn/news/8072.html

相关文章:

  • 什么是智能体?
  • 顺序表VS单链表
  • RuntimeError: Cannot find sufficient samples, consider increasing dataset size.
  • 【Tauri2】047——Image
  • gcc还会有自己的头文件呢?
  • CMake 跨平台构建系统详解
  • 友达15.6寸G156HAN02.3工业显示模组
  • 在Linux系统上备份另一个系统的做法
  • 数据库主从集群 + GTID 实现高可用
  • inlier_outlier
  • 视觉大模型学习总结
  • 通过 curl 精准定位问题
  • 从零开始的嵌入式学习day25
  • Java SSM与SpringBoot面试题全面解析:从基础到源码
  • 线性表数据结构-队列
  • 8:点云处理—常见的四种3D相机
  • 今日行情明日机会——20250521
  • 探索Puter:一个基于Web的轻量级“云操作系统”
  • Java基础 5.21
  • 重磅升级!Google Play商店改版上线
  • Web服务器
  • C++语言的跨平台挑战和应对策略
  • centos7 p8p1使用ip addr查看时有的时候有两个ip,有的时候只有一个ip,有一个ip快,有一个ip慢
  • 如何在 Windows 10 或 11 上使用命令提示符安装 Angular
  • Vue Router动态路由与导航守卫实战
  • RESTful风格
  • 从零基础到最佳实践:Vue.js 系列(6/10):《Composition API(组合式 API)》
  • 论文篇目录-研究生如何阅读编写论文
  • Linux系统编程-DAY02
  • 直播美颜SDK技术解析:滤镜渲染与动态贴纸引擎融合的底层实现