uniPush1.0 + springboot 实现消息推送(在线版的)

unipush + springboot 完成消息推送

  1. uniapp 开通 unipush 推送服务
  2. springboot 整合 unipush的 sdk
  3. springboot 调用相关的api 完成消息推送

开通 unipush

1. 开通服务在应用设置中开启 unipush1.0

在这里插入图片描述
在这里插入图片描述
这里只展示 uniPush 1.0
原因:
unipush 1.0 :可以用个推服务端api推送
unipush 2.0:不支持用个推服务端api推送,只能用dcloud提供的云函数
unipush 1.0 文档:https://uniapp.dcloud.net.cn/unipush-v1.html

2. 制作自定义的基座 (打包成 Android 应用)

在这里插入图片描述
在这里插入图片描述

4 申请证书

这里提示需要再开发者后台给打包的应用的开启 开通 unipush 服务
开发者后台:https://dev.dcloud.net.cn/pages/app/list
在开通之前向申请一个安卓证书先
网址:https://www.yunedit.com/
在这里插入图片描述
在这里插入图片描述

在开发者管理后台给应用开通 unipush1.0

在这里插入图片描述

在这里插入图片描述
提交完成就去给应用 开通 unipush1.0

在这里插入图片描述

在这里插入图片描述
点击开通应用就完成了 unipush1.0 的开通了
开通完成之后就可以打包应用了
在这里插入图片描述
确保是自定义基座和传统打包
点击打包即可。
运行到 安卓手机上
在这里插入图片描述

springboot 集成 unipush SDK

导入坐标:

 <!-- 消息推送 -->
        <dependency>
            <groupId>com.getui.push</groupId>
            <artifactId>restful-sdk</artifactId>
            <version>1.0.4.0</version>
        </dependency>

做配置:

# 消息推送
push:
  app_id: xxxxxxxxxx
  app_key: xxxxxx
  master_secret: xxxxxxx

这些配置从开发者后台中获取
在这里插入图片描述

定义配置 push配置类

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 个推推送类
 */
@Configuration
public class PushConfig {
    @Value("${push.app_id}")
    private String appId;

    @Value("${push.app_key}")
    private String appKey;

    @Value("${push.master_secret}")
    private String masterSecret;

    @Bean(name = "myApiHelper")
    public ApiHelper apiHelper() {
        // 设置httpClient最大连接数,当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");

        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        // 填写应用配置
        apiConfiguration.setAppId(appId);
        apiConfiguration.setAppKey(appKey);
        apiConfiguration.setMasterSecret(masterSecret);
        // 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
        // 默认为https://restapi.getui.com/v2
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");
        // 实例化ApiHelper对象,用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        return apiHelper;
    }
}

创建工具类 [ 非必须的 ]


import com.alibaba.druid.support.json.JSONUtils;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
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.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
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.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class PushMsgUtils {
    // 消息推送
    public static final String MESSAGE_PUSH = "1";
    // 离线推送
    public static final String OFFLINE_PUSH = "2";

    @Resource(name = "myApiHelper")
    private ApiHelper myApiHelper;


    /**
     * 消息推送(离线推送)单cid推送
     * cid 必填 设备
     * title 必填 标题
     * content 必填 内容
     * linkUrl 必填 跳转链接
     * type 必填 1:消息推送 2:离线推送
     */
    public ApiResult<Map<String, Map<String, String>>> pushToSingleByCid(String cId ,
                                                                         String title,
                                                                         String content,
                                                                         String type ) {
        PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, type);
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cId);
        // 进行cid单推
        PushApi pushApi = myApiHelper.creatApi(PushApi.class);
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);

        if (apiResult.isSuccess()) {
            // success
            log.info("推送成功");
            System.out.println(apiResult.getData());
        } else {
            // failed
            log.error("推送失败");
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
        return apiResult;
    }


    /**
     * 消息推送(离线推送)单cid推送
     * cid 必填 设备
     * title 必填 标题
     * content 必填 内容
     * linkUrl 必填 跳转链接
     * type 必填 1:消息推送 2:离线推送
     */
    public ApiResult<Map<String, Map<String, String>>> pushToSingleByCIds(List<String> cIds ,
                                                                         String title,
                                                                         String content,
                                                                         String type ) {
        PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, type);
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.setCid(cIds);
        // 进行cid单推
        PushApi pushApi = myApiHelper.creatApi(PushApi.class);
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);

        if (apiResult.isSuccess()) {
            // success
            log.info("推送成功");
            System.out.println(apiResult.getData());
        } else {
            // failed
            log.error("推送失败");
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
        return apiResult;
    }

    /**
     * 消息参数模板
     */
    private PushDTO<Audience> buildPushDTO(String title, String content, String type) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        //requestid需要每次变化唯一
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setGroupName("wxb-group");


        //配置推送条件
        // 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;
        // 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;
        // 3: 表示该消息只通过个推通道下发,不考虑用户是否在线;
        // 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
        Strategy strategy = new Strategy();
        strategy.setDef(1);
        strategy.setSt(1);
        Settings settings = new Settings();
        settings.setStrategy(strategy);
        pushDTO.setSettings(settings);
        //消息有效期,走厂商消息需要设置该值
        settings.setTtl(3600000);

        PushChannel pushChannel = new PushChannel();

        Map<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("content", content);
        map.put("type", type);
        // 转json对象
        String payload = JSONUtils.toJSONString(map);

        // ========================= ios离线配置 ==============================
        //推送苹果离线通知标题内容
        Alert alert = new Alert();
        //苹果离线通知栏标题
        alert.setTitle(title);
        //苹果离线通知栏内容
        alert.setBody(content);
        Aps aps = new Aps();
        //1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。
        // 苹果建议1小时最多推送3条静默消息
        aps.setContentAvailable(0);
        aps.setSound("default");
        aps.setAlert(alert);
        IosDTO iosDTO = new IosDTO();
        iosDTO.setPayload(payload);
        iosDTO.setAps(aps);
        iosDTO.setType("notify");
        pushChannel.setIos(iosDTO);

        // =================== 安卓离线厂商通道推送消息体 ===========================
        AndroidDTO androidDTO = new AndroidDTO();
        pushDTO.setPushChannel(pushChannel);
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification notification1 = new ThirdNotification();
        ups.setNotification(notification1);
        //安卓离线展示的标题
        notification1.setTitle(title);
        //安卓离线展示的内容
        notification1.setBody(content);
        notification1.setClickType("intent");

        notification1.setIntent("intent:#Intent;action=android.intent.action.oppopush;" +
                "launchFlags=0x14000000;" +
                "component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;" +
                "S.title=" + title + ";" +
                "S.content="+  content + ";" +
                "S.payload=" + payload + ";end");
        // notification1.setPayload(payload);
        //各厂商自有功能单项设置
//        ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry ");
//        ups.addOption("HW", "/message/android/notification/badge/add_num", 1);
//        ups.addOption("HW", "/message/android/notification/importance", "HIGH");
//        ups.addOption("VV","classification",1);

        //设置options 方式一
//        ups.addOption("HW","badgeAddNum",3);
//        ups.addOption("HW","badgeClass","com.getui.demo.GetuiSdkDemoActivity");
//        ups.addOption("OP","app_message_id",11);
//        ups.addOption("VV","message_sort",1);
//        ups.addOptionAll("channel","default");

        // PushMessage在线走个推通道才会起作用的消息体
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        Map<String, Object> mapTC = new HashMap<>();
        mapTC.put("title", title);
        mapTC.put("content", content);
        mapTC.put("payload", map);
        String jsonTC = JSONUtils.toJSONString(mapTC);
        pushMessage.setTransmission(jsonTC);
        log.info("pushDTO:{}", pushDTO);
        return pushDTO;
    }

    /**
     * 离线通知消息参数模板
     *
     */
    private PushDTO<Audience> offlinePushDTO(String title, String content, String linkUrl, String type) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        //requestid需要每次变化唯一
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setGroupName("wxb-group");
        // PushMessage在线走个推通道才会起作用的消息体

        Map<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("content", content);
        map.put("linkUrl", linkUrl);
        map.put("type", type);

        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        Map<String, Object> mapTC = new HashMap<>();
        mapTC.put("title", title);
        mapTC.put("content", content);
        mapTC.put("payload", map);
        String jsonTC = JSONUtils.toJSONString(mapTC);
        System.err.println(jsonTC);
        pushMessage.setTransmission(jsonTC);
        log.info("pushDTO:{}", pushDTO);

        return pushDTO;
    }
}

这里演示单推送:步骤

  1. 获取 cId
    在 vue.app 中
<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
			// 获取 cId 可以理解为设备id 
			uni.getPushClientId({
				success: (res) => {
					console.log(res.cid);
				},
				fail: (res) => {
					console.log(res);
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>

  1. 调用工具类推送消息
    @Test
    public void pushMsg() {
        String cid = "9d02a626d057cac8b7a5a5ff84bde125";
        String title = "测试 - 后端";
        String content = "测试 - 后端 - 内容";
        String type = PushMsgUtils.MESSAGE_PUSH;
        ApiResult<Map<String, Map<String, String>>> mapApiResult = pushMsgUtils.pushToSingleByCid(cid, title, content, type);
        System.out.println("mapApiResult = " + mapApiResult);
    }
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
移动智能办公系统是基于uni-app框架和springboot后端技术开发的一款集合办公管理、知识分享、团队协作等功能的移动应用程序。 该系统的设计与实现包括以下几个方面: 1. 用户管理:系统管理员可以对用户进行管理,包括注册、登录、权限设置等功能。用户可以根据自身角色访问对应的功能模块。 2. 办公管理:系统提供日程安排、任务管理、公告发布等功能,用户可以通过系统进行办公事务的管理和协作。可以设置提醒和共享功能,方便团队成员协同工作。 3. 知识分享:用户可以发布、查看和评论知识分享内容,支持多种格式的文档上传和在线预览,方便团队之间的知识共享和技术交流。 4. 团队协作:系统提供在线聊天、文件共享、任务分配等功能,方便团队成员之间的沟通和协作。可以设定项目组和权限,实现不同团队之间的隔离和数据安全。 5. 报表统计:系统可以对用户的办公行为进行数据监控和分析,生成相应的报表和图表,帮助管理层进行决策和评估。 该系统的实现基于uni-app框架和springboot后端技术,uni-app可以实现一次开发多端部署,用户可以在不同的移动终端上使用该应用。而springboot提供了快速开发、安全稳定的后台支持,能够保证系统的高效运行和数据的安全性。 综上所述,基于uni-appspringboot的移动智能办公系统设计与实现可以提供全方位的办公管理、知识分享和团队协作等功能,为用户提供便捷、高效的办公体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值