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

学习路之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>

三、效果

在这里插入图片描述

在这里插入图片描述

后期展望:

使用自已服务器接口获取CID集合,然后存在deviceIds数组,
再调用云函数

uniCloud.callFunction({name: "server-push", // 填写你自己的云函数名称data: {body: JSON.stringify(send_data) // 将数据转换为 JSON 字符串并赋值给 body},success(dataRes) {console.log("云函数返回的参数", dataRes)},fail(err) {console.log("云函数报错", err)},complete() {}})

就可以实现全局、点对点通知。

相关文章:

  • PostGIS实现栅格数据导出PNG应用实践【ST_AsPNG 】
  • 【Redis8】最新安装版与手动运行版
  • springboot3+vue3融合项目实战-大事件文章管理系统-更新文章分类和增加文章分类优化-分组校验
  • FlashAttention:传统自注意力( Self-Attention)优化加速实现
  • BEVDet
  • 实战5:个性化数字艺术生成与销售
  • 【泛微系统】后端开发Action常用方法
  • 项目交付标准不明确,如何确保验收顺利
  • 谷歌I/O 2025 完全指南:由Gemini开创的AI新时代及其对我们未来的影响
  • Bently Nevada 3500/61 非隔离I/O模块 (133819-02)
  • c++11特性——可变参数模板及emplace系列接口
  • 电子电路:怎么理解放大电路中集电极电流Ic漂移?
  • 命令行删除node_modules
  • 系统工程与一般系统理论 | 技术 / 应用 / 跨领域认知融合
  • 时源芯微|六大步骤解决EMC问题
  • 【AI流程应用】智能知识库搭建与实战应用
  • 【Linux】借助gcc源码修改,搜索头文件当前进展
  • 6-码蹄集600题基础python篇
  • 为什么可以不重写m1方法
  • 英伟达显卡驱动怎么安装 使用驱动人生轻松安装
  • 电脑做系统教学网站/百度搜索引擎怎么弄
  • 如何做1个手机网站/网页优化seo公司
  • 怎么查网站做404页面没/独立站建站平台有哪些
  • 潭州教育网站开发/永久免费国外域名注册
  • 河南郑州百度网站建设/广州疫情最新动态
  • 曲靖做网站公司/seo搜索引擎优化的内容