作者:陈柏佩66057 | 来源:互联网 | 2023-07-06 11:15
对接魅族产商推送的小伙伴看过来,如果对接魅族产商推送疑问可以留言,先将对接过程给你大家分享。
魅族推送 分两种一种是魅族聚合推送;另外一种是flyme推送。
首先申请魅族开放平台账号,申请地址,建议能用企业申请就用企业申请
https://open.flyme.cn/
申请完成后,记得看连接 ,这个是对接的sdk
http://open-wiki.flyme.cn/index.php?title=Flyme%E6%8E%A8%E9%80%81SDK
魅族flyme推送,请使用如下 git
https://github.com/MEIZUPUSH
java版本请找到 https://github.com/MEIZUPUSH/JavaSdk 这个中的代码,里面有javatest代码,调用如下:
我写的代码如下:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.meizu.push.sdk.server.IFlymePush;
import com.meizu.push.sdk.server.constant.ResultPack;
import com.meizu.push.sdk.server.model.push.PushResult;
import com.meizu.push.sdk.server.model.push.VarnishedMessage;
import com.numberone.common.enums.PushChannelEnum;
import com.numberone.common.enums.PushTargetTypeEnum;
import com.numberone.xuanfeng.mapper.CustomerMapper;
import com.numberone.xuanfeng.vo.push.BasePushReqVo;
import lombok.extern.slf4j.Slf4j;
/***
* 魅族消息推送实现
* @author 38292
*
*/
@Slf4j
@Service("push_meizu_service")
public class MeiZuPushService extends BasePushService{
@Value("${push.meizu.appid}")
public String APP_ID;
@Value("${push.meizu.appkey}")
public String APP_KEY;
@Value("${push.meizu.appsecret}")
public String APP_SECRET;
@Autowired
private CustomerMapper customerMapper;
@Override
public void sendMsg(BasePushReqVo vo) {
JSONObject result = null;
try {
this.doMeizuPush(vo);
log.info("魅族通知服务器消息推送器响应消息: {}", result);
}catch (Exception e){
log.info("魅族通知服务器消息推送异常:{}", e);
}
}
/**
* 组装消息
*
* @param appId
* @param title
* 标题
* @param content
* 内容
* @return
*/
public static VarnishedMessage buildMessage(Long appId, String title, String content) {
return new VarnishedMessage.Builder().appId(appId).title(title).content(content).build();
}
public void doMeizuPush(BasePushReqVo vo) {
log.info("APP_ID:{},APP_KEY:{},APP_SECRET:{}",APP_ID,APP_KEY,APP_SECRET);
// 推送对象
final IFlymePush push = new IFlymePush(APP_SECRET);
// 目标用户
List pushIds = new ArrayList();
//推送所有人
if (vo.getPushTargetType().equals(PushTargetTypeEnum.ALL_CUS)) {
List customers = customerMapper.selectCustomerByPushType(PushChannelEnum.PUSH_CHANNEL_MEIZU.getCode(),vo.getCompanyId());
if (customers != null && !customers.isEmpty()) {
pushIds =customers;
}
} else {
//推送指定用户
pushIds.addAll(vo.getPushIdenti());
}
// 1 调用推送服务
ResultPack result;
try {
result = push.pushMessage(buildMessage(Long.valueOf(APP_ID),vo.getTitle(), vo.getContent()), pushIds, 3);
if (result.isSucceed()) {
// 2 调用推送服务成功 (其中map为设备的具体推送结果,一般业务针对超速的code类型做处理)
PushResult pushResult = result.value();
// 推送消息ID,用于推送流程明细排查
String msgId = pushResult.getMsgId();
// 推送结果,全部推送成功,则map为empty
Map> targetResultMap = pushResult.getRespTarget();
if (targetResultMap != null && !targetResultMap.isEmpty()) {
log.info("Meizu Push fail, token: " + targetResultMap);
}
log.info(String.format(
"Meizu Push Over -> regisId: {}, data: {}, code: {}, messageId:{}, message: {}.", vo.getPushIdenti(),
result.toString(), result.code(), msgId, result.comment()));
} else {
// 调用推送接口服务异常 eg: appId、appKey非法、推送消息非法.....
// result.code(); //服务异常码
// result.comment();//服务异常描述
log.info(String.format("meizu pushMessage error code:%scomment:%s", result.code(), result.comment()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
魅族聚合推送
这个是聚合推送,集成推送平台PushSDK设计文档:https://github.com/MEIZUPUSH/UpsDemo
java 代码如下:
/**
* 魅族推送测试类
*
*/
public class Meizu {
public static final int APP_ID = 123;
public static final String APP_KEY = "APP_KEY";
public static final String APP_SECRET = "APP_SECRET";
// 需要推送用户的注册di
public static final String REGIS_ID = "REGIS_ID";
public static void main(String[] args) {
doMeizuPush();
}
/**
* 组装消息
*
* @param appId
* @param title
* 标题
* @param content
* 内容
* @return
*/
public static VarnishedMessage buildMessage(Long appId, String title, String content) {
return new VarnishedMessage.Builder().appId(appId).title(title).content(content).build();
}
public static void doMeizuPush() {
// 推送对象
final IFlymePush push = new IFlymePush(APP_SECRET);
// 目标用户
List pushIds = new ArrayList();
pushIds.add(REGIS_ID);
// 1 调用推送服务
ResultPack result;
try {
result = push.pushMessage(buildMessage(Long.valueOf(APP_ID), "title", "content"), pushIds, 3);
if (result.isSucceed()) {
// 2 调用推送服务成功 (其中map为设备的具体推送结果,一般业务针对超速的code类型做处理)
PushResult pushResult = result.value();
// 推送消息ID,用于推送流程明细排查
String msgId = pushResult.getMsgId();
// 推送结果,全部推送成功,则map为empty
Map> targetResultMap = pushResult.getRespTarget();
if (targetResultMap != null && !targetResultMap.isEmpty()) {
System.out.println("Meizu Push fail, token: " + targetResultMap);
}
System.out.println(String.format(
"Meizu Push Over -> regisId: {}, data: {}, code: {}, messageId:{}, message: {}.", REGIS_ID,
result.toString(), result.code(), msgId, result.comment()));
} else {
// 调用推送接口服务异常 eg: appId、appKey非法、推送消息非法.....
// result.code(); //服务异常码
// result.comment();//服务异常描述
System.out.println(String.format("meizu pushMessage error code:%scomment:%s", result.code(), result.comment()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}