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

uniapp消息推送

基于uniPush2.0

首先需要在DCloud开发者后台开通服务空间

点击查看官方内容
在这里插入图片描述
填写相关信息

然后回到HburilderX里,右击项目《创建uniCloud云开发环境》,右击新建的uniCloud文件,选择《关联云服务空间或项目》,再uniCloud内的“cloudfunctions”文件右击《新建云函数》,“点击下面第二个箭头添加一键推送模块”

在这里插入图片描述

打开新建的云函数下的 index,添加下面代码

// 简单的使用示例
'use strict';
const uniPush = uniCloud.getPushManager({appId: "__UNI__4D91404"
}) //注意这里需要传入你的应用appId,用于指定接收消息的客户端
exports.main = async (event, context) => {console.log('请求体', event);try {let res = await uniPush.sendMessage({"push_clientid": JSON.parse(event.body).push_clientid, //填写上一步在uni-app客户端获取到的客户端推送标识push_clientid"title": JSON.parse(event.body).title,"content": JSON.parse(event.body).content,"payload": JSON.parse(event.body).payload,"force_notification": true,"channel": {"XM": "115318", //"HW": "NORMAL","OP": "115318","VV": 1},"options": {"OP": {"/channel_id": "", //打开OPPO开发者平台有推送id"/off_line_ttl": 86400 // 离线消息的存活时间(time_to_live) (单位:秒), 【最长10天】},"XM": {"/extra.channel_id": "" , //打开小米开发者平台有推送id},"HW": {"/message/android/notification/importance": "NORMAL"},"VV": {"/classification": 1, //0代表运营消息,1代表系统消息"/notifyType": 4 // 通知类型 1:无,2:响铃,3:振动,4:响铃和振动 注意:只对Android 8.0及以下系统有效}}})return {"data": {data: res.data,res: event.body},"errCode": res.errCode,"errMsg": '' + res.errMsg}} catch (e) {return {"data": {data: res.data,res: event.body},"errCode": 500,"errMsg": '传的数据不对' + e}}};

右击“cloudfunctions”,选择上传所有云函数在这里插入图片描述

配置完了,下面是uniapp里

点击查看官方内容

在首页获取推送cid传给后端

const plugin = uni.requireNativePlugin('DCloud-PushSound');
import push from '@/js_sdk/dc-push/push.js';
if (!push.isNotOn()) {uni.showModal({title: '提示',content: '未确保您即时收到消息,请打开APP通知权限',showCancel: true,success: function(res) {if (res.confirm) {if ('iOS' == plus.os.name) {var UIApplication = plus.ios.import('UIApplication');var NSURL = plus.ios.import('NSURL');var setting = NSURL.URLWithString('app-settings:');var application = UIApplication.sharedApplication();application.openURL(setting);plus.ios.deleteObject(setting);plus.ios.deleteObject(application);} else {var main = plus.android.runtimeMainActivity();var pkName = main.getPackageName();var Intent = plus.android.importClass('android.content.Intent');var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS'); //可设置表中所有Action字段intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);main.startActivity(intent);}}}});
}else{uni.getPushClientId({success: (ret) => {console.log(ret);upClientid({pushClientid: ret.cid}).then((res) => {uni.setStorageSync('tuisongCheck',true)// console.log('更新推送id', ret.cid, res);});},fail(err) {console.log(err);}
});
}

在APP.vue里获取推送内容

uni.onPushMessage((res) => {console.log('监听到推送消息1', res);if(res.data.payload.includes('\\')){res.data.payload = res.data.payload.replace(new RegExp(/\\/,'g'),'')}let datas = JSON.parse(res.data.payload)console.log('监听到推送消息2', datas);
})

打自定义包测试

http://www.lryc.cn/news/575985.html

相关文章:

  • 向量关于基的坐标向量
  • 图像分割模型中的空间信息、上下文信息、空间路径、上下文路径到底是什么?有什么作用?
  • 【C++】atoi和std::stoi
  • redisTemplate简单实现幂等性校验
  • 微信小程序进度条progress支持渐变色
  • 【vue3】打包配置webpack压缩,哈希值设置
  • CVE-2015-5531源码分析与漏洞复现(Elasticsearch目录遍历漏洞)
  • 高斯混合模型GMMK均值(十三-1)——K均值是高斯混合模型的特例
  • macOS,切换 space 失效,向右切换space(move right a space) 失效
  • [论文阅读] 人工智能 | 真实场景下 RAG 系统的工程实践指南
  • JUC:7线程的五种状态与六种状态
  • AI歌手Yuri出道:GenAI,透露着新的AI产业机遇?
  • 增加寒武纪MLU270视频转码
  • 大数据赋能智能家居:打造你贴心的“数字管家”
  • STM32安全固件升级:使用自定义 bootloader 实现SD卡固件升级,包含固件加密
  • 【stm32】HAL库开发——CubeMX配置串口通讯(中断方式)
  • virtual box 配置ubuntu 22.04网络与SSH服务
  • A模块 系统与网络安全 第三门课 网络通信原理-2
  • 24CJ87-4:圆拱型采光排烟天窗
  • Pytorch基础函数速查
  • A Machine Learning Approach for Non-blind Image Deconvolution论文阅读
  • AI助力基因数据分析:用Python玩转生命密码的秘密
  • 高标准+安全可控:关键领域研发选择什么软件?
  • QT编译wasm报错:The program “mingw32-make.exe“ does not exist or is not executable
  • 《二分枚举答案(配合数据结构)》题集
  • 数据透视表学习笔记
  • 如何将两个不同类性的类组合成一个json数据
  • Vue 3 计算属性的应用
  • Valkey与Redis评估对比:开源替代方案的技术演进
  • 基于Odoo 18的生产报工系统架构与开发