使用极光推送能够实现app的消息推送,需要将 JPush Android SDK 集成到app里,JPush Android SDK 创建到 JPush Cloud 的长连接,为 App 提供长期在线的能力。 当开发者想要及时地推送消息到达 App 时,只需要调用 JPush API 推送,或者使用其他方便的智能推送工具,即可轻松与用户交流。
JPush Android SDK 集成:
极光推送消息通道分为 极光通道 和 厂商通道 , 推送时候,如果极光通道在线会默认优先选择极光推动进行推送,如果极光通道不在线会采用厂商通道进行推送,关于厂商通道的办理方式,可以参考官方提供的指南,本文介绍极光推送服务端api的简单操作步骤:
一、引入极光依赖
<!--JPush-->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.2.2</version>
</dependency>
二、添加极光相关配置(AppKey和AppSrcit)
使用前需要到极光官网申请应用,并获取到密钥
#极光推送应用配置
JPush:
APP_KEY:
MASTER_SECRET:
三、初始化极光客户端
@Slf4j
@Configuration
public class JPushConfig {
@Value("${JPush.APP_KEY}")
private String APP_KEY;
@Value("${JPush.MASTER_SECRET}")
private String MASTER_SECRET;
@Bean(name = "jpushClient")
public JPushClient jpushClient() {
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
log.info("jpushClient init accomplish");
return jpushClient;
}
}
四、推送工具类进行推送
@Slf4j
@Component
public class JPushUtil {
@Autowired
private JPushClient jpushClient;
/**
* 批量推送
*/
public PushResult batchPushWarning(List<String> rids, String title, String content) {
PushPayload pushPayload = buildNotificationPushObject(rids, title, content);
PushResult pushResult = null;
try {
pushResult = jpushClient.sendPush(pushPayload);
} catch (Exception e) {
log.error("批量推送 - 极光推送出错");
e.printStackTrace();
}
return pushResult;
}
/**
* 逐条推送
*/
public PushResult push(String rid, String title, String content) {
PushPayload pushPayload = buildNotificationPushObject(rid, title, content);
PushResult pushResult = null;
try {
pushResult = jpushClient.sendPush(pushPayload);
} catch (Exception e) {
log.error("单挑 - 极光推送出错");
e.printStackTrace();
}
return pushResult;
}
/**
* 批量推送
*/
public static PushPayload buildNotificationPushObject(List<String> rids, String title, String content) {
//设置推送通道下发策略
Map<String, JsonObject> thirdMap = new HashMap<>();
setThirdPartyChannel(thirdMap);
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.registrationId(rids))
.setOptions(Options.newBuilder()
//设置android通道下发策略
.setThirdPartyChannelV2(thirdMap)
.build())
//设置通知消息
.setNotification(Notification.newBuilder()
.setAlert(content)
//设置android通知消息
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title)
.build())
.build())
.build();
}
/**
* 逐条推送
*/
public static PushPayload buildNotificationPushObject(String rid, String title, String content) {
//设置推送通道下发策略
Map<String, JsonObject> thirdMap = new HashMap<>();
setThirdPartyChannel(thirdMap);
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.registrationId(rid))
.setOptions(Options.newBuilder()
//设置android通道下发策略
.setThirdPartyChannelV2(thirdMap)
.build())
//设置通知消息
.setNotification(Notification.newBuilder()
.setAlert(content)
//设置android通知消息
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title)
.build())
.build())
.build();
}
/**
* 设置第三方通道策略
* distribution 为字符串类型,取值不能为空字符串,只能是"ospush"、"jpush"、"secondary_push"。
* 值为 ospush 表示推送强制走厂商通道下发;
* 值为 jpush 表示推送强制走极光通道下发;
* 值为 secondary_push 表示推送优先走极光,极光不在线再走厂商,厂商作为辅助;【官方建议此种方式】
* options.third_party_channel 的 key 只支持 xiaomi、huawei、meizu、oppo、vivo、fcm;
*/
public static void setThirdPartyChannel(Map<String, JsonObject> thirdMap) {
//华为厂商参数配置
JsonObject hwParam = new JsonObject();
hwParam.addProperty("distribution", "secondary_push");
// hwParam.addProperty("importance","NORMAL");
//华为 category
// hwParam.addProperty("category","WORK");
//小米厂商参数配置
JsonObject xmParam = new JsonObject();
xmParam.addProperty("distribution", "secondary_push");
//小米channel_id
// xmParam.addProperty("channel_id", "1001");
//OPPO厂商参数配置
JsonObject oppoParam = new JsonObject();
oppoParam.addProperty("distribution", "secondary_push");
//oppo channel_id
// oppoParam.addProperty("channel_id", "1002");
//VIVO厂商参数配置
JsonObject vivoParam = new JsonObject();
vivoParam.addProperty("distribution", "secondary_push");
//1系统消息,0运营消息
// vivoParam.addProperty("classification", 1);
//vivo category
// vivoParam.addProperty("category", "SUBSCRIPTION");
//荣耀厂商参数配置
JsonObject honorParam = new JsonObject();
honorParam.addProperty("distribution", "secondary_push");
//LOW不重要消息,NORMAL重要消息
honorParam.addProperty("importance", "NORMAL");
//添加到third_party_channel结构体
thirdMap.put("huawei", hwParam);
thirdMap.put("xiaomi", xmParam);
thirdMap.put("oppo", oppoParam);
thirdMap.put("vivo", vivoParam);
thirdMap.put("honor", honorParam);
}
}
五、厂商策略
Map<String,JsonObject> third_party_channel = new HashMap<>();
//华为厂商参数配置
JsonObject hwParam = new JsonObject();
hwParam.addProperty("distribution","secondary_push");//推送策略
hwParam.addProperty("importance","NORMAL");
hwParam.addProperty("category","WORK");//华为 category
//小米厂商参数配置
JsonObject xmParam = new JsonObject();
xmParam.addProperty("distribution","secondary_push");
xmParam.addProperty("channel_id","1001");//小米channel_id
//OPPO厂商参数配置
JsonObject oppoParam = new JsonObject();
oppoParam.addProperty("distribution","secondary_push");
oppoParam.addProperty("channel_id","1002");//oppo channel_id
//VIVO厂商参数配置
JsonObject vivoParam = new JsonObject();
vivoParam.addProperty("distribution","secondary_push");
vivoParam.addProperty("classification",1);//1系统消息,0运营消息
vivoParam.addProperty("category","SUBSCRIPTION");//vivo category
//荣耀厂商参数配置
JsonObject honorParam = new JsonObject();
honorParam.addProperty("distribution","secondary_push");
honorParam.addProperty("importance","NORMAL");//LOW不重要消息,NORMAL重要消息
//添加到third_party_channel结构体
third_party_channel.put("huawei",hwParam);
third_party_channel.put("xiaomi",xmParam);
third_party_channel.put("oppo",oppoParam);
third_party_channel.put("vivo",vivoParam);
third_party_channel.put("honor",honorParam);
Options option = Options.newBuilder()
.addCustom("classification",1)//根据消息类型填入
.setApnsProduction(false)
.setTimeToLive(86400)
.setThirdPartyChannelV2(third_party_channel)
.build();
//然后在payload构建时,setOptions中传入option
//以上参数为举例,实际options需要的参数以业务需要为准
"third_party_channel":{
"xiaomi":{
"distribution":"secondary_push",
"channel_id":"1001" //填入小米申请的channel_id
},
"huawei":{
"distribution":"secondary_push",
"importance":"NORMAL", //NORMAL代表重要消息,LOW代表不重要消息
"category":"IM", //填入华为申请的category
"target_user_type":0 //0代表普通消息(默认),1代表测试消息,测试消息1天限量500条
},
"honor":{
"distribution":"secondary_push",
"importance":"NORMAL" //NORMAL代表重要消息,LOW代表不重要消息
},
"meizu":{
"distribution":"secondary_push"
},
"oppo":{
"distribution":"secondary_push",
"channel_id":"1002" //填入oppo申请的channel_id
},
"vivo":{
"distribution":"secondary_push",
"classification": 1, //0代表运营消息,1代表系统消息
"category":"TODO", //填入vivo申请的category
"push_mode":0 //0表示正式推送,1表示测试推送
}
}
厂商通道需要逐一去申请厂商参数,此过程较为繁琐,申请到厂商参数并配置到极光应用控制台后,还需要争对厂商去申请厂商的配额,并在消息中携带相关参数,完成离线推送,这块需要参考极光官方的指导手册进行。
参数配置参考:厂商推送分类限制及适配方案 · BDS技术支持组
QPS限制:厂商限额及 QPS 限制 - 极光文档
标签:addProperty,极光,JsonObject,channel,push,推送,服务端 From: https://blog.csdn.net/m0_51607909/article/details/137016816