一:依赖
<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>pushy</artifactId>
<version>0.14.1</version>
</dependency>
二:code
package com.supcon.mare.MessagePush;
import com.supcon.mare.android.AndroidNotification;
import com.supcon.mare.android.BadgeNotification;
import com.supcon.mare.android.ClickAction;
import com.supcon.mare.common.util.exception.InterfaceException;
import com.supcon.mare.constant.ErrorCode;
import com.supcon.mare.exception.HuaweiMesssagingException;
import com.supcon.mare.message.*;
import com.supcon.mare.model.SendResponse;
import com.supcon.mare.model.Urgency;
import com.supcon.mare.model.vo.AppMsgPushVO;
import com.supcon.mare.util.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author 刘旺
* @version V1.0
* @Package com.supcon.mare.MessagePush
* @date 2020/8/20 15:14
* @Copyright © 2020 中控(西安)
*/
@Slf4j
@Component
public class HuaWeiPush {
@Value("${company.domainName}")
private String companyName;
@Value("${huawei.appId}")
private String appId;
@Value("${huawei.appSecret}")
private String appSecret;
private HuaweiMessageClient huaweiMessageClient = new HuaweiMessageClientImpl();
/**
* 通过华为消息服务器发送消息
*/
public List<String> forwardMsgToHuaWei(AppMsgPushVO appMsgPushVO, List<String> pushTokens) {
Integer badge = appMsgPushVO.getBadge();
String action = appMsgPushVO.getAction();
String description = appMsgPushVO.getDescription();
Boolean transmission = appMsgPushVO.getTransmission();
Notification notification = Notification.builder().setTitle(appMsgPushVO.getTitle())
.setBody(appMsgPushVO.getBody())
.build();
AndroidNotification androidNotification;
AndroidNotification.Builder androidNotificationBuild = AndroidNotification.builder();
androidNotificationBuild.setDefaultSound(true);
androidNotificationBuild.setForegroundShow(true);
if (null != description) {
androidNotificationBuild.setNotifySummary(description);
}
if (0 != badge) {
androidNotificationBuild.setBadge(BadgeNotification.builder().setAddNum(badge).setBadgeClass("Classic").build());
}
if (null != action) {
if (action.startsWith("https")) {
androidNotificationBuild.setClickAction(ClickAction.builder().setType(2).setUrl(action).build());
} else if (action.endsWith("end")) {
androidNotificationBuild.setClickAction(ClickAction.builder().setType(1).setIntent(action).build());
} else {
androidNotificationBuild.setClickAction(ClickAction.builder().setType(1).setAction(action).build());
}
}
androidNotificationBuild.setStyle(0);
androidNotification = androidNotificationBuild.build();
AndroidConfig androidConfig = AndroidConfig.builder()
.setUrgency(Urgency.HIGH.getValue())
.setNotification(androidNotification)
.build();
pushTokens.forEach(s -> {
log.info("pushToken: " + s);
});
Message message = Message.builder().setNotification(notification)
.setAndroidConfig(androidConfig)
.addAllToken(pushTokens)
.build();
List<String> list = new ArrayList<>();
String response = null;
try {
log.info("start to send message by HuaWei server");
SendResponse send = huaweiMessageClient.send(message, false, appId, appSecret);
String msg = send.getMsg();
String code = send.getCode();
response = "msg:" + msg + " code: " + code;
log.info(response);
if (!code.equals("80000000")) {
list.add(msg);
}
} catch (HuaweiMesssagingException e) {
log.error(response);
log.error(Utils.getStackTraceV2(e));
throw new InterfaceException(ErrorCode.SEND_MSG_ERROR, "errorCode " + e.getErrorCode() + " msg: " + e.getMessage());
} catch (IOException e) {
log.error(Utils.getStackTraceV2(e));
throw new InterfaceException(ErrorCode.READ_MESSAGE_ERROR, "read message io exception");
}
return list;
}
}
/*
* Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.supcon.mare.android;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.supcon.mare.message.Notification;
import com.supcon.mare.model.Importance;
import com.supcon.mare.model.Visibility;
import com.supcon.mare.util.CollectionUtils;
import com.supcon.mare.util.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
public class AndroidNotification {
private static final String COLOR_PATTERN = "^#[0-9a-fA-F]{6}$";
private static final String URL_PATTERN = "^https.*";
private static final String VIBRATE_PATTERN = "[0-9]+|[0-9]+[sS]|[0-9]+[.][0-9]{1,9}|[0-9]+[.][0-9]{1,9}[sS]";
@JSONField(name = "title")
private String title;
@JSONField(name = "body")
private String body;
@JSONField(name = "icon")
private String icon;
@JSONField(name = "color")
private String color;
@JSONField(name = "sound")
private String sound;
@JSONField(name = "default_sound")
private boolean defaultSound;
@JSONField(name = "tag")
private String tag;
@JSONField(name = "click_action")
private ClickAction clickAction;
@JSONField(name = "body_loc_key")
private String bodyLocKey;
@JSONField(name = "body_loc_args")
private List<String> bodyLocArgs = new ArrayList<>();
@JSONField(name = "title_loc_key")
private String titleLocKey;
@JSONField(name = "title_loc_args")
private List<String> titleLocArgs = new ArrayList<>();
@JSONField(name = "multi_lang_key")
private JSONObject multiLangKey;
@JSONField(name = "channel_id")
private String channelId;
@JSONField(name = "notify_summary")
private String notifySummary;
@JSONField(name = "image")
private String image;
@JSONField(name = "style")
private Integer style;
@JSONField(name = "big_title")
private String bigTitle;
@JSONField(name = "big_body")
private String bigBody;
@JSONField(name = "auto_clear")
private Integer autoClear;
@JSONField(name = "notify_id")
private Integer notifyId;
@JSONField(name = "group")
private String group;
@JSONField(name = "badge")
private Color badge;
@JSONField(name = "ticker")
private String ticker;
@JSONField(name = "auto_cancel")
private boolean autoCancel;
@JSONField(name = "when")
private String when;
@JSONField(name = "local_only")
private Boolean localOnly;
@JSONField(name = "importance")
private String importance;
@JSONField(name = "use_default_vibrate")
private boolean useDefaultVibrate;
@JSONField(name = "use_default_light")
private boolean useDefaultLight;
@JSONField(name = "vibrate_config")
private List<String> vibrateConfig = new ArrayList<>();
@JSONField(name = "visibility")
private String visibility;
@JSONField(name = "foreground_show")
private boolean foregroundShow;
@JSONField(name = "inbox_content")
private List<String> inboxContent;
private AndroidNotification(Builder builder) {
this.title = builder.title;
this.body = builder.body;
this.icon = builder.icon;
this.color = builder.color;
this.sound = builder.sound;
this.defaultSound = builder.defaultSound;
this.tag = builder.tag;
this.clickAction = builder.clickAction;
this.bodyLocKey = builder.bodyLocKey;
if (!CollectionUtils.isEmpty(builder.bodyLocArgs)) {
this.bodyLocArgs.addAll(builder.bodyLocArgs);
} else {
this.bodyLocArgs = null;
}
this.titleLocKey = builder.titleLocKey;
if (!CollectionUtils.isEmpty(builder.titleLocArgs)) {
this.titleLocArgs.addAll(builder.titleLocArgs);
} else {
this.titleLocArgs = null;
}
if (builder.multiLangkey != null) {
this.multiLangKey = builder.multiLangkey;
} else {
this.multiLangKey = null;
}
this.channelId = builder.channelId;
this.notifySummary = builder.notifySummary;
this.image = builder.image;
this.style = builder.style;
this.bigTitle = builder.bigTitle;
this.bigBody = builder.bigBody;
this.autoClear = builder.autoClear;
this.notifyId = builder.notifyId;
this.group = builder.group;
this.ticker = builder.ticker;
this.autoCancel = builder.autoCancel;
this.when = builder.when;
this.importance = builder.importance;
this.useDefaultVibrate = builder.useDefaultVibrate;
this.useDefaultLight = builder.useDefaultLight;
if (!CollectionUtils.isEmpty(builder.vibrateConfig)) {
this.vibrateConfig = builder.vibrateConfig;
} else {
this.vibrateConfig = null;
}
this.visibility = builder.visibility;
this.foregroundShow = builder.foregroundShow;
if (!CollectionUtils.isEmpty(builder.inboxContent)) {
this.inboxContent = builder.inboxContent;
} else {
this.inboxContent = null;
}
}
/**
* check androidNotification's parameters
*
* @param notification which is in message
*/
public void check(Notification notification) {
if (null != notification) {
ValidatorUtils.checkArgument(StringUtils.isNotEmpty(notification.getTitle()) || StringUtils.isNotEmpty(this.title), "title should be set");
ValidatorUtils.checkArgument(StringUtils.isNotEmpty(notification.getBody()) || StringUtils.isNotEmpty(this.body), "body should be set");
}
if (StringUtils.isNotEmpty(this.color)) {
ValidatorUtils.checkArgument(this.color.matches(AndroidNotification.COLOR_PATTERN), "Wrong color format, color must be in the form #RRGGBB");
}
if (this.clickAction != null) {
this.clickAction.check();
}
if (!CollectionUtils.isEmpty(this.bodyLocArgs)) {
ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.bodyLocKey), "bodyLocKey is required when specifying bodyLocArgs");
}
if (!CollectionUtils.isEmpty(this.titleLocArgs)) {
ValidatorUtils.checkArgument(StringUtils.isNotEmpty(this.titleLocKey), "titleLocKey is required when specifying titleLocArgs");
}
if (StringUtils.isNotEmpty(this.image)) {
ValidatorUtils.checkArgument(this.image.matches(URL_PATTERN), "notifyIcon must start with https");
}
//Style 0,1,2
if (this.style != null) {
boolean isTrue = this.style == 0 ||
this.style == 1 ||
this.style == 2 ||