package com.huawei.push.util.push;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.gson.Gson;
|
import com.meizu.push.sdk.constant.PushType;
|
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 org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.yeshi.utils.push.entity.PushAppInfo;
|
import org.yeshi.utils.push.entity.PushMessage;
|
import org.yeshi.utils.push.exception.MeiZuPushException;
|
|
import java.io.IOException;
|
import java.util.*;
|
|
/**
|
* 魅族推送
|
* 推送文档:http://open.res.flyme.cn/fileserver/upload/file/201803/e174a5709f134f64aae3fb168aec8ea3.pdf
|
*/
|
public class MeiZuPushUtil {
|
|
static Logger logger = LoggerFactory.getLogger(MeiZuPushUtil.class);
|
|
private static VarnishedMessage createMessage(PushAppInfo appInfo, PushMessage message) {
|
VarnishedMessage.Builder builder = new VarnishedMessage.Builder().appId(Long.parseLong(appInfo.getAppId()))
|
.title(message.getTitle())
|
.content(message.getContent())
|
//点击动作 (0,"打开应用"),(1,"打开应用页面"),(2,"打 开URI页面"),(3, "应用客户端自定义")【非必填, 默认值为0】
|
.clickType(1);
|
if (message.getActivity() != null) {
|
builder = builder.activity(message.getActivity());
|
}
|
if (message.getActivityParams() != null && message.getActivityParams().size() > 0) {
|
JSONObject params = new JSONObject();
|
for (Iterator<String> its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
params.put(key, message.getActivityParams().get(key));
|
}
|
builder = builder.parameters(params);
|
}
|
return builder.build();
|
}
|
|
/**
|
* 推送通知
|
*
|
* @param appInfo
|
* @param message
|
* @param pushIds 不能超过1000个
|
* @return
|
* @throws IOException
|
* @throws MeiZuPushException
|
*/
|
public static String pushNotificationByPushId(PushAppInfo appInfo, PushMessage message, List<String> pushIds) throws IOException, MeiZuPushException {
|
//推送标题限制字数1-32,推送内容限制字数1-100
|
IFlymePush push = new IFlymePush(appInfo.getAppSecret());
|
VarnishedMessage msg = createMessage(appInfo, message);
|
ResultPack<PushResult> result = push.pushMessage(msg, pushIds, 2);
|
logger.info("MEIZU推送响应结果:{}", result.code() + "-" + result.comment());
|
if (result.isSucceed()) {
|
return result.value().getMsgId();
|
} else {
|
throw new MeiZuPushException(result.code(), result.comment());
|
}
|
}
|
|
/**
|
* 推送所有
|
*
|
* @param appInfo
|
* @param message
|
* @return
|
* @throws IOException
|
* @throws MeiZuPushException
|
*/
|
public static String pushNotificationAll(PushAppInfo appInfo, PushMessage message) throws IOException, MeiZuPushException {
|
IFlymePush push = new IFlymePush(appInfo.getAppSecret());
|
VarnishedMessage msg = createMessage(appInfo, message);
|
ResultPack<Long> result = push.pushToApp(PushType.STATUSBAR, msg);
|
if (result.isSucceed()) {
|
return result.value() + "";
|
} else {
|
throw new MeiZuPushException(result.code(), result.comment());
|
}
|
}
|
|
|
public static void main(String[] args) {
|
PushAppInfo appInfo = new PushAppInfo("139085", "dc40ea74e19948a683cb1876d5a8813e", "6497adb8bde044c6a32c0a2766332ca3");
|
Map<String, String> activityParams = new HashMap<>();
|
activityParams.put("activity", "test");
|
JSONObject jumpParams = new JSONObject();
|
jumpParams.put("id", 123123);
|
activityParams.put("params", jumpParams.toJSONString());
|
|
PushMessage message = new PushMessage("你好啊", "下午好,hello world", "com.weikou.beibeivideo.ui.push.OpenClickActivity", null, null, activityParams);
|
|
String[] ids = new String[]{
|
"UCI4e0f4070047c4949057e76446d6474484500447b05"
|
};
|
|
try {
|
MeiZuPushUtil.pushNotificationByPushId(appInfo, message, Arrays.asList(ids));
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (MeiZuPushException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
}
|