uniApp开通uniPush1.0个推,SpringBoot集成uniPush1.0个推

news/2025/1/17 17:33:10/

uniApp开通unipush1.0个推,SpringBoot程序集成

一、APP开通unipush1.0个推(商户App源码仅支持1.0个推)

1.app模块配置开通推送

在这里插入图片描述

2.应用开通推送

在这里插入图片描述

3.开通后点击消息推送菜单会看到如下页面 完成以上步骤后

在这里插入图片描述

  • 此时android 仅支持在线推送。

4.配置各厂商离线推送

  • 暂未配置

二、Java端SpringBoot集成uniPush1.0推送

1.引入依赖

		<!--	unipush 1.0个推	--><dependency><groupId>com.getui.push</groupId><artifactId>restful-sdk</artifactId><version>1.0.6.0</version></dependency>

2.spring配置yaml文件添加配置

getui:  #个推推送appId: ************ appKey: ************ masterSecret: ************domin: https://restapi.getui.com/v2 

3.参数可从uniapp开发者中心获取

在这里插入图片描述

4.注入所有API实体类

  • UniPushConfiguration.java

    package com.joolun.cloud.common.jiguang.config;import com.getui.push.v2.sdk.ApiHelper;
    import com.getui.push.v2.sdk.api.PushApi;
    import com.getui.push.v2.sdk.api.UserApi;
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import com.getui.push.v2.sdk.GtApiConfiguration;/*** @author yuanDing* @version 1.0* @date 2025-01-10 9:40* uniPush 1.0个推推送配置类*/
    @Data
    @RefreshScope
    @Configuration
    @ConfigurationProperties(prefix = "getui")
    public class UniPushConfiguration {private String appId;private String appKey;private String masterSecret;private String domin;@Beanpublic GtApiConfiguration apiConfiguration() {GtApiConfiguration apiConfiguration = new GtApiConfiguration();//填写应用配置,参数在“Uni Push”下的“应用配置”页面中获取apiConfiguration.setAppId(appId);apiConfiguration.setAppKey(appKey);apiConfiguration.setMasterSecret(masterSecret);apiConfiguration.setDomain(domin);return apiConfiguration;}@Beanpublic PushApi PushApi(GtApiConfiguration apiConfiguration) {return ApiHelper.build(apiConfiguration).creatApi(PushApi.class);}@Beanpublic UserApi UserApi(GtApiConfiguration apiConfiguration) {return ApiHelper.build(apiConfiguration).creatApi(UserApi.class);}// 还有其他所需API实体类自行注入...}
    

5.封装工具类

  • UniPushUtils

    package com.joolun.cloud.common.jiguang.util;import cn.hutool.core.collection.ListUtil;
    import cn.hutool.json.JSONObject;
    import com.getui.push.v2.sdk.api.PushApi;
    import com.getui.push.v2.sdk.api.UserApi;
    import com.getui.push.v2.sdk.common.ApiResult;
    import com.getui.push.v2.sdk.dto.req.Audience;
    import com.getui.push.v2.sdk.dto.req.AudienceDTO;
    import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
    import com.getui.push.v2.sdk.dto.req.Settings;
    import com.getui.push.v2.sdk.dto.req.message.PushDTO;
    import com.getui.push.v2.sdk.dto.req.message.PushMessage;
    import com.getui.push.v2.sdk.dto.res.TaskIdDTO;
    import com.joolun.cloud.common.jiguang.dto.CIdAliasDTO;
    import com.joolun.cloud.common.jiguang.dto.UniPushDTO;
    import lombok.AllArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;/*** @author yuanDing* @version 1.0* @date 2025-01-10 10:40* uniPush 1.0推送工具类*/
    @Slf4j
    @Configuration
    @AllArgsConstructor
    public class UniPushUtils {private final PushApi pushApi;private final UserApi userApi;/*** 单解绑* 解除绑定cid和userId(解除绑定别名)** @return*/public boolean unBoundingUserIdAndCId(CIdAliasDTO cIdAliasDTO) {return this.unBoundingUserIdAndCIdList(ListUtil.toList(cIdAliasDTO));}/*** 批量解绑* 解除绑定cid和userId(批量解除绑定别名)** @return*/public boolean unBoundingUserIdAndCIdList(List<CIdAliasDTO> cIdAliasDTOS) {return userApi.batchUnbindAlias(this.getCidAliasListDTO(cIdAliasDTOS)).isSuccess();}/*** 单绑* 绑定cid和userId(绑定别名)* 注意:1个cid只能绑定一个别名 1个别名最多只能绑定10个cid 超出以最后绑定为准** @return*/public boolean boundingUserIdAndCId(CIdAliasDTO cIdAliasDTO) {return this.boundingUserIdAndCIdList(new ArrayList<CIdAliasDTO>() {{add(cIdAliasDTO);}}).isSuccess();}/*** 多绑* 绑定cid和userId(绑定别名)* 注意:1个cid只能绑定一个别名 1个别名最多只能绑定10个cid 超出已最后绑定为准** @return*/public ApiResult<Void> boundingUserIdAndCIdList(List<CIdAliasDTO> cIdAliasDTOS) {return  userApi.bindAlias(this.getCidAliasListDTO(cIdAliasDTOS));}/*** 获取cid alias DTO* @param cIdAliasDTOS* @return*/private CidAliasListDTO getCidAliasListDTO(List<CIdAliasDTO> cIdAliasDTOS) {CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();// 0 普通别名 1系统别名
    //        cidAliasListDTO.setAliasType("1");ArrayList<CidAliasListDTO.CidAlias> cidAliases = new ArrayList<>();cIdAliasDTOS.forEach(aliasDTO -> {CidAliasListDTO.CidAlias cidAlias = new CidAliasListDTO.CidAlias();cidAlias.setAlias(aliasDTO.getAlias());cidAlias.setCid(aliasDTO.getCId());cidAliases.add(cidAlias);});cidAliasListDTO.setDataList(cidAliases);return cidAliasListDTO;}/*** 根据别名单推 alias参数必填* @param uniPushDTO* @return*/public boolean pushByAlias(UniPushDTO uniPushDTO) {return pushApi.pushToSingleByAlias(this.getPushDTO(uniPushDTO , "alias")).isSuccess();}/*** 根据CId单推 cid参数必填* @param uniPushDTO* @return*/public boolean pushByCId(UniPushDTO uniPushDTO) {return pushApi.pushToSingleByCid(this.getPushDTO(uniPushDTO , "cid")).isSuccess();}/*** 根据别名多推送 alias参数必填* @param uniPushDTO* @return*/public boolean pushByAliasList(UniPushDTO uniPushDTO) {PushDTO pushDTO = new PushDTO();PushMessage pushMessage = new PushMessage();pushMessage.setTransmission(new JSONObject(uniPushDTO.getMessage()).toString());pushDTO.setPushMessage(pushMessage);ApiResult<TaskIdDTO> msg = pushApi.createMsg(pushDTO);if (msg.isSuccess()) {AudienceDTO audienceDTO = new AudienceDTO();audienceDTO.setTaskid(msg.getData().getTaskId());audienceDTO.setAsync(false);Audience audience = new Audience();for (String alias : uniPushDTO.getAliasList()) {audience.addAlias(alias);}audienceDTO.setAudience(audience);// 发送推送消息return  pushApi.pushListByAlias(audienceDTO).isSuccess();}return false;}/*** 获取推送实体类* @param uniPushDTO* @param type* @return*/public PushDTO<Audience> getPushDTO(UniPushDTO uniPushDTO , String type) {//根据cid进行单推PushDTO<Audience> pushDTO = new PushDTO<Audience>();// 设置推送参数,requestid需要每次变化唯一pushDTO.setRequestId(System.currentTimeMillis() + "");Settings settings = new Settings();pushDTO.setSettings(settings);//消息有效期,走厂商消息必须设置该值settings.setTtl(uniPushDTO.getTtl());//在线走个推通道时推送的消息体PushMessage pushMessage = new PushMessage();pushDTO.setPushMessage(pushMessage);//此格式的透传消息由 unipush 做了特殊处理,会自动展示通知栏。开发者也可自定义其它格式,在客户端自己处理。pushMessage.setTransmission(new JSONObject(uniPushDTO.getMessage()).toString());// 设置接收人信息Audience audience = new Audience();pushDTO.setAudience(audience);switch (type) {case "cid": uniPushDTO.getCIdList().forEach(audience::addCid);break;case "alias": uniPushDTO.getAliasList().forEach(audience::addAlias);break;}/*** 离线推送,暂未对接*//*        //设置离线推送时的消息体PushChannel pushChannel = new PushChannel();//安卓离线厂商通道推送的消息体AndroidDTO androidDTO = new AndroidDTO();Ups ups = new Ups();ThirdNotification thirdNotification = new ThirdNotification();ups.setNotification(thirdNotification);thirdNotification.setTitle("安卓离线展示的标题");thirdNotification.setBody("安卓离线展示的内容");thirdNotification.setClickType("intent");//注意:intent参数必须按下方文档(特殊参数说明)要求的固定格式传值,intent错误会导致客户端无法收到消息thirdNotification.setIntent("请填写固定格式的intent");androidDTO.setUps(ups);pushChannel.setAndroid(androidDTO);*//*** ios暂未对接 后续对接完放开注释*///ios离线apn通道推送的消息体
    //        Alert alert = new Alert();
    //        alert.setTitle("苹果离线通知栏标题");
    //        alert.setBody("苹果离线通知栏内容");
    //        Aps aps = new Aps();
    //        aps.setContentAvailable(0);
    //        aps.setSound("default");
    //        aps.setAlert(alert);
    //        IosDTO iosDTO = new IosDTO();
    //        iosDTO.setAps(aps);
    //        iosDTO.setType("notify");//        pushChannel.setIos(iosDTO);
    //        pushDTO.setPushChannel(pushChannel);return pushDTO;}}
    • UniPushDTO.java

      package com.joolun.cloud.common.jiguang.dto;import cn.hutool.json.JSONObject;
      import lombok.AllArgsConstructor;
      import lombok.Builder;
      import lombok.Data;
      import lombok.NoArgsConstructor;import java.util.List;/*** @author yuanDing* @version 1.0* @date 2025-01-10 9:40* uniPush 1.0个推推送领域类*/
      @Data
      @Builder
      @AllArgsConstructor
      @NoArgsConstructor
      public class UniPushDTO {/*** 单推必填* 推送消息体 JSON格式*  title:标题*  content:内容*  payload:承载数据 最好传JSON,与前端接收初一致!*/private Message message;/**/*** cid或alias二选一* 设备cid*/private List<String> cIdList;/*** cid或alias二选一* 设备别名*/private List<String> aliasList;/*** 离线推送必填,在线推送非必填* 消息离线时间设置,单位毫秒,-1表示不设离线, -1 ~ 3 * 24 * 3600 * 1000之间*/private Integer ttl;@Data@Builder@AllArgsConstructor@NoArgsConstructorpublic static class Message{/***  title* 必传*/private String title;/***  content* 必传*/private String content;// 必传private Object payload;}}
    • CIdAliasDTO.java

      package com.joolun.cloud.common.jiguang.entity;import lombok.AllArgsConstructor;
      import lombok.Builder;
      import lombok.Data;
      import lombok.NoArgsConstructor;/*** @author yuanDing* @version 1.0* @date 2025-01-10 9:40* uniPush 1.0个推推送别名设备领域类*/
      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      @Builder
      public class CIdAliasDTO {/*** 设备cid*/private String cId;/*** 别名*/private String alias;}
      

6.测试推送


import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONObject;
import com.joolun.cloud.common.jiguang.dto.CIdAliasDTO;
import com.joolun.cloud.common.jiguang.dto.UniPushDTO;
import com.joolun.cloud.common.jiguang.util.UniPushUtils;
import com.joolun.cloud.mall.api.JooLunMallApiApplication;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;import java.util.ArrayList;/*** unipush 推送测试*/
@SpringBootTest(classes = JooLunMallApiApplication.class)
public class UniPushTest {@Autowiredprivate UniPushUtils uniPushUtils;/*** 绑定别名测试*/@Testpublic void boundingTest() {boolean result = uniPushUtils.boundingUserIdAndCId(CIdAliasDTO.builder().alias("15065606797").cId("75328b5a6f777a1c853810340d9404ac").build());System.out.println("voidApiResult = " + result);}/*** 根据cid推送测试*/@Testpublic void pushByCIdTest() {UniPushDTO uniPushDTO = UniPushDTO.builder().cIdList(ListUtil.toList("3d9d4ceecd51c5b147a0e3ab6edb72e0"))
//                .ttl(-1).message(UniPushDTO.Message.builder().content( "村里发金条了,速归!").title("速归").payload("测试").build()).build();boolean result = uniPushUtils.pushByCId(uniPushDTO);System.out.println("result = " + result);}/*** 根据别名单推推送测试*/@Testpublic void pushByAliasTest() {UniPushDTO uniPushDTO = UniPushDTO.builder().aliasList(ListUtil.toList("1790974474554609665"))
//                .ttl(-1).message(UniPushDTO.Message.builder().content( "村里发金条了,速归!").title("聚城测试").payload("测试").build()).build();boolean result = uniPushUtils.pushByAlias(uniPushDTO);System.out.println("result = " + result);}/*** 根据别名多推推送测试*/@Testpublic void pushByAliasListTest() {UniPushDTO uniPushDTO = UniPushDTO.builder().aliasList(ListUtil.toList("1790974474554609665" , "15065606797" , "4565456"))
//                .ttl(-1).message(UniPushDTO.Message.builder().content( "村里发金条了,速归!").title("聚城测试").payload("测试").build()).build();boolean result = uniPushUtils.pushByAliasList(uniPushDTO);System.out.println("result = " + result);}/*** 单解绑测试*/@Testpublic void unBoundingTest() {boolean result = uniPushUtils.unBoundingUserIdAndCId( CIdAliasDTO.builder().alias("1790974474554609665").cId("3d9d4ceecd51c5b147a0e3ab6edb72e0").build());System.out.println("result = " + result);}
}

三、uniapp端订阅消息事件(未配置不影响推送,用户收到消息后的后续操作)

  • App.vue

    onLaunch: function() {service.saveLogInfo("********************** App启动 ************************")//#ifdef APP-PLUSvar info = plus.push.getClientInfo() // 获取clientIdconsole.log(`info` , info)if (info && info.clientid) {Vue.prototype.$publicStore.cid = info.clientid}// 以下为订阅监听消息事件,如有需要放开// 使用5+App的方式进行监听消息推送 点击消息回调plus.push.addEventListener("click", function(msg) {if (msg.payload) {// 订单下单通知if (msg.payload.signature === 'order') {uni.navigateTo({url: '/pages/mall/order/list?status=1'})}}}, false);// 监听在线消息事件plus.push.addEventListener("receive", function(msg) {//业务代码console.log("recevice:" + JSON.stringify(msg))}, false);//#endif},
    

http://www.ppmy.cn/news/1563933.html

相关文章

VirtualBox环境中vscode报错:提取扩展时出错。Failed to fetch

问题现象&#xff1a; VSCode切换到扩展并筛选特色时(为了触发联网)&#xff0c;提示提取扩展时出错。Failed to fetch 拔网线复现不了&#xff0c;完全没有网络VSCode只会提示离线 虚拟机内Edge浏览器访问https://marketplace.visualstudio.com/直接报ERR_CONNECTION_REFUSED…

yt-dlp脚本下载音频可选设置代理

import yt_dlp# 配置:是否使用代理 use_proxy = True # 设置为 False 可关闭代理# 代理地址 proxy_url = socks5://127.0.0.1:1089URLS = [https://www.bilibili.com/video/BV1WTktYcEcQ/?spm_id_from=333.1007.tianma.6-2-20.click&vd_source=dcb58f8fe1faf749f438620b…

50_Lua垃圾回收

1.Lua垃圾回收机制概述 Lua采用了一种自动内存管理机制,称为垃圾回收(Garbage Collection, GC)。垃圾回收的主要目的是回收程序中不再被使用的内存,从而避免内存泄漏。Lua的垃圾回收器负责回收动态分配的对象,如函数、用户数据、表、字符串、线程、内部结构等。Lua的垃圾…

Grails应用http.server.requests指标数据采集问题排查及解决

问题 遇到的问题&#xff1a;同一个应用&#xff0c;Spring Boot(Java)和Grails(Groovy)混合编程&#xff0c;常规的Spring Controller&#xff0c;可通过Micromete Pushgateway&#xff0c; 采集到http.server.requests指标数据&#xff0c;注意下面的指标名称是点号&#…

防抖、节流知识详解:它们的应用场合有哪些?

在前端开发中&#xff0c;防抖&#xff08;Debounce&#xff09;和节流&#xff08;Throttle&#xff09;是两种常见的技术&#xff0c;用于优化性能和控制事件触发频率。尽管它们的目的相似&#xff0c;但在实现原理和适用场景上存在明显的区别。本文将详细介绍防抖和节流的概…

科研总结系列|2-GPT学术写作提示词集锦手册

科研总结系列&#xff5c;2-GPT学术写作提示词集锦手册 一、前言 随着生成式人工智能&#xff08;如 ChatGPT&#xff09;的迅速发展&#xff0c;学术写作正变得更加智能和高效。高质量的学术论文不仅能够准确传达研究成果&#xff0c;也会在学术圈内带来更广泛的影响力。借助…

25/1/14 算法笔记<强化学习> CBR加强化学习

CBR,基于案例的推理&#xff0c;它是一种基于过去的实际经验或经历的推理&#xff0c;他可以根据过往的案例找到与当前案例最相关的案例&#xff0c;然后对该案例做改动来解决当前的问题。 CBR的过程 CBR可以看作一个循环过程&#xff1a;相似按键检索-->案例重用-->案…

挖掘机检测数据集,准确识别率91.0%,4327张原始图片,支持YOLO,COCO JSON,PASICAL VOC XML等多种格式标注

挖掘机检测数据集&#xff0c;准确识别率91.0%&#xff0c;4327张图片&#xff0c;支持YOLO&#xff0c;COCO JSON&#xff0c;PASICAL VOC XML等多种格式标注 数据集详情 数据集分割 训练组70&#xff05; 3022图片 有效集20&#xff05; 870图片 测试集10&…