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

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

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

1.app模块配置开通推送

2.应用开通推送

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

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

4.配置各厂商离线推送

  • 暂未配置

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

1.引入依赖

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

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

yaml 复制代码
getui:  #个推推送
    appId: ************ 
    appKey: ************ 
    masterSecret: ************
    domin: https://restapi.getui.com/v2 

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

4.注入所有API实体类

  • UniPushConfiguration.java

    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;
        @Bean
        public GtApiConfiguration apiConfiguration() {
            GtApiConfiguration apiConfiguration = new GtApiConfiguration();
            //填写应用配置,参数在"Uni Push"下的"应用配置"页面中获取
            apiConfiguration.setAppId(appId);
            apiConfiguration.setAppKey(appKey);
            apiConfiguration.setMasterSecret(masterSecret);
            apiConfiguration.setDomain(domin);
            return apiConfiguration;
        }
    
        @Bean
        public PushApi PushApi(GtApiConfiguration apiConfiguration) {
            return ApiHelper.build(apiConfiguration).creatApi(PushApi.class);
        }
    
        @Bean
        public UserApi UserApi(GtApiConfiguration apiConfiguration) {
            return ApiHelper.build(apiConfiguration).creatApi(UserApi.class);
        }
        
        // 还有其他所需API实体类自行注入...
    
    }

5.封装工具类

  • UniPushUtils

    java 复制代码
    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

      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
          @NoArgsConstructor
          public static class Message{
              /**
               *  title
               * 必传
               */
              private String title;
              /**
               *  content
               * 必传
               */
              private String content;
      
              // 必传
              private Object payload;
          }
      
      
      }
    • CIdAliasDTO.java

      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.测试推送

java 复制代码
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 {

    @Autowired
    private UniPushUtils uniPushUtils;

    /**
     * 绑定别名测试
     */
    @Test
    public void boundingTest() {
        boolean result = uniPushUtils.boundingUserIdAndCId(CIdAliasDTO.builder()
                .alias("15065606797")
                .cId("75328b5a6f777a1c853810340d9404ac")
                .build());
        System.out.println("voidApiResult = " + result);
    }

    /**
     * 根据cid推送测试
     */
    @Test
    public 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);
    }

    /**
     * 根据别名单推推送测试
     */
    @Test
    public 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);
    }

    /**
     * 根据别名多推推送测试
     */
    @Test
    public 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);
    }


    /**
     * 单解绑测试
     */
    @Test
    public void unBoundingTest() {

        boolean result = uniPushUtils.unBoundingUserIdAndCId( CIdAliasDTO.builder()
                .alias("1790974474554609665")
                .cId("3d9d4ceecd51c5b147a0e3ab6edb72e0")
                .build());
        System.out.println("result = " + result);
    }
}

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

  • App.vue

    js 复制代码
    onLaunch: function() {
          service.saveLogInfo("********************** App启动 ************************")
          //#ifdef APP-PLUS
          var info = plus.push.getClientInfo() // 获取clientId
          console.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
    		
    
    		},
相关推荐
疯狂小小小码农26 分钟前
C语言的数据库交互
开发语言·后端·golang
蒜蓉大猩猩1 小时前
Node.js - Cookie与Session详解
分布式·后端·中间件·node.js
C++小厨神5 小时前
Java语言的循环实现
开发语言·后端·golang
Quantum&Coder6 小时前
Ruby语言的数据库编程
开发语言·后端·golang
ByteBlossom6666 小时前
Ruby语言的网络编程
开发语言·后端·golang
静水楼台x6 小时前
Java中json的一点理解
java·后端·json
晴空๓7 小时前
如何查看特定版本的Spring源码
java·spring boot·spring
macrozheng8 小时前
Jenkins+Docker一键打包部署项目!步骤齐全,少走坑路!
java·spring boot·后端·docker·jenkins
!!!5258 小时前
MyBatis-增删改查操作&一些细节
java·数据库·spring boot·mybatis