我是靠谱客的博主 单身糖豆,最近开发中收集的这篇文章主要介绍Unipush移动端消息推送,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

移动端消息推送(只适用于安卓手机,ios待研究)

    • 1.安装app到手机
    • 2.开通unipush功能
    • 3.后端java代码实现消息推送
    • 4.自有证书生成签名
    • 5.自有证书查看签名

1.安装app到手机

打开HBuilderX,双击项目中的“manifest.json”文件,勾选app模块配置中的Push(消息推送)-uniPush,将app进行云打包,包名自己定义,格式io.dcloud.HBuilder,并使用自有证书(自有证书获取地址:https://ask.dcloud.net.cn/article/35777),最后将打包之后的apk文件在安卓手机上安装。

2.开通unipush功能

开通unipush功能(开通指南:https://ask.dcloud.net.cn/article/35716),在后台配置“厂商推送设置”,在各手机开放平台创建推送应用(创建流程:https://www.dcloud.io/docs/a/unipush/manufacturer.pdf),并获取配置参数,在unipush厂商推送配置中配置参数,最后进行在线消息与厂商离线消息推送测试。(UniPush使用指南:https://ask.dcloud.net.cn/article/35622)

3.后端java代码实现消息推送

后端java代码用Java-sdk封装的Rest-V2实现,完整服务端厂商推送教程参考:https://docs.getui.com/getui/server/rest_v2/common_args/。
所用依赖:

<dependency>
<groupId>com.getui.push</groupId>
<artifactId>restful-sdk</artifactId>
<version>1.0.0.1</version>
</dependency>

实现方法:PushUtils.java(如下)。

package com.fn.common.push.utils;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
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.PushBatchDTO;
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.GTNotification;
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.res.TaskIdDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
/**
* @ClassName: PushUtils
* @Author: lxh
* @Description: 移动端消息推送
* @Date: 2021/7/9 10:22
*/
public class PushUtils {
private static final Logger logger = LoggerFactory.getLogger(PushUtils.class);
/**
* 应用配置
*/
private static PushApi apiConfig() {
GtApiConfiguration apiConfiguration = new GtApiConfiguration();
apiConfiguration.setAppId("DEOB0OBf3LAjRDxDtgjEi7");
apiConfiguration.setAppKey("7qPScWS7vS9u1fBjfUy4jA");
apiConfiguration.setMasterSecret("LNUV32RYyq61tHmQCMYVg");
// 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
apiConfiguration.setDomain("https://restapi.getui.com/v2/");
// 实例化ApiHelper对象,用于创建接口对象
ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
// 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi
return apiHelper.creatApi(PushApi.class);
}
/**
* 配置推送条件
*/
private static Settings getSettings() {
//配置推送条件
// 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道;
// 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线;
// 3: 表示该消息只通过个推通道下发,不考虑用户是否在线;
// 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
Strategy strategy = new Strategy();
strategy.setDef(1);
Settings settings = new Settings();
settings.setStrategy(strategy);
//消息有效期,走厂商消息需要设置该值
settings.setTtl(3600000);
return settings;
}
/**
* 推送消息体
*/
private static void sendMessage(PushDTO<?> pushDTO, String title, String content, String payload, String intent, String url) {
// 设置推送参数 requestid需要每次变化唯一
pushDTO.setRequestId(System.currentTimeMillis() + "");
pushDTO.setSettings(getSettings());
PushChannel pushChannel = new PushChannel();
//安卓离线厂商通道推送消息体
AndroidDTO androidDTO = new AndroidDTO();
Ups ups = new Ups();
ThirdNotification thirdNotification = new ThirdNotification();
logger.info("推送的消息体:" + title + content);
thirdNotification.setTitle("离线标题:" + title);
thirdNotification.setBody("离线内容:" + content);
//ClickType:点击通知后续动作,
//目前支持5种后续动作,
//intent:打开应用内特定页面,
//url:打开网页地址,
//payload:启动应用加自定义消息内容,
//startapp:打开应用首页,
//none:纯通知,无后续动作
if (StringUtils.isEmpty(payload) && StringUtils.isEmpty(intent) && StringUtils.isEmpty(url)) {
thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
} else if (StringUtils.isEmpty(payload) && StringUtils.isEmpty(url)) {
thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_INTENT.type);
thirdNotification.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;" +
"action=android.intent.action.oppopush;component=io.dcloud.fire/" + intent + ";S.UP-OL-SU=true;" +
"S.title=" + title + ";S.content=" + content + ";S.payload=payload;end");
} else if (StringUtils.isEmpty(payload) && StringUtils.isEmpty(intent)) {
thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_URL.type);
thirdNotification.setUrl(url);
} else if (StringUtils.isEmpty(intent) && StringUtils.isEmpty(url)) {
thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_PAYLOAD.type);
thirdNotification.setPayload(payload);
} else {
thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
}
ups.setNotification(thirdNotification);
//各厂商自有功能单项设置
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);
androidDTO.setUps(ups);
pushChannel.setAndroid(androidDTO);
pushDTO.setPushChannel(pushChannel);
// PushMessage在线走个推通道才会起作用的消息体
PushMessage pushMessage = new PushMessage();
pushDTO.setPushMessage(pushMessage);
GTNotification notification = new GTNotification();
notification.setTitle(title);
notification.setBody(content);
if (StringUtils.isEmpty(payload) && StringUtils.isEmpty(intent) && StringUtils.isEmpty(url)) {
notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
} else if (StringUtils.isEmpty(payload) && StringUtils.isEmpty(intent)) {
notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_URL.type);
notification.setUrl(url);
} else if (StringUtils.isEmpty(intent) && StringUtils.isEmpty(url)) {
notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_PAYLOAD.type);
notification.setPayload(payload);
} else {
notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
}
pushMessage.setNotification(notification);
}
/**
* 对指定用户进行单推
*/
public static void pushToSingle(String cid, String title, String content, String payload, String intent, String url) {
PushDTO<Audience> pushDTO = new PushDTO<>();
sendMessage(pushDTO, title, content, payload, intent, url);
//设置接收人信息u
Audience audience = new Audience();
audience.addCid(cid);
pushDTO.setAudience(audience);
//进行cid单推
ApiResult<Map<String, Map<String, String>>> apiResult = apiConfig().pushToSingleByCid(pushDTO);
if (apiResult.isSuccess()) {
// success
logger.info("推送成功" + apiResult.getData());
} else {
// failed
logger.info("推送失败" + "code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
}
}
/**
* 根据cid进行批量推送
*/
public static void pushBatch(List<String> CIDs, String title, String content, String payload, String intent, String url) {
PushDTO<Audience> pushDTO = new PushDTO<>();
sendMessage(pushDTO, title, content, payload, intent, url);
//设置接收人信息u
Audience audience = new Audience();
PushBatchDTO batchDTO = new PushBatchDTO();
batchDTO.setAsync(true);
for (String cid : CIDs) {
audience.addCid(cid);
pushDTO.setAudience(audience);
batchDTO.addPushDTO(pushDTO);
}
//进行cid批量推
ApiResult<Map<String, Map<String, String>>> apiResult = apiConfig().pushBatchByCid(batchDTO);
if (apiResult.isSuccess()) {
// success
logger.info("推送成功" + apiResult.getData());
} else {
// failed
logger.info("推送失败" + "code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
}
}
/**
* 对所有app用户群推通知
*/
public static void pushAllApp(String title, String content, String payload, String intent, String url) {
PushDTO<String> pushDTO = new PushDTO<String>();
sendMessage(pushDTO, title, content, payload, intent, url);
//群推
ApiResult<TaskIdDTO> apiResult = apiConfig().pushAll(pushDTO);
if (apiResult.isSuccess()) {
// success
logger.info("推送成功" + apiResult.getData());
} else {
// failed
logger.info("推送失败" + "code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
}
}
}

4.自有证书生成签名

keytool -genkey -alias testalias -keyalg RSA -keysize 2048 -validity 36500 -keystore test.keystore

5.自有证书查看签名

keytool -list -v -keystore test.keystore
Enter keystore password: //输入密码,回车

最后

以上就是单身糖豆为你收集整理的Unipush移动端消息推送的全部内容,希望文章能够帮你解决Unipush移动端消息推送所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部