add oppo vivo fcm push

This commit is contained in:
imhao183
2021-03-12 12:10:54 +08:00
parent 4e64c81ea6
commit 27b6efcaee
19 changed files with 639 additions and 243 deletions

View File

@@ -1,8 +1,11 @@
package cn.wildfirechat.push.android;
import cn.wildfirechat.push.PushMessage;
import cn.wildfirechat.push.android.fcm.FCMPush;
import cn.wildfirechat.push.android.hms.HMSPush;
import cn.wildfirechat.push.android.meizu.MeiZuPush;
import cn.wildfirechat.push.android.oppo.OppoPush;
import cn.wildfirechat.push.android.vivo.VivoPush;
import cn.wildfirechat.push.android.xiaomi.XiaomiPush;
import com.google.gson.Gson;
import org.slf4j.Logger;
@@ -22,6 +25,14 @@ public class AndroidPushServiceImpl implements AndroidPushService {
@Autowired
private XiaomiPush xiaomiPush;
@Autowired
private VivoPush vivoPush;
@Autowired
private OppoPush oppoPush;
@Autowired
private FCMPush fcmPush;
@Override
public Object push(PushMessage pushMessage) {
@@ -36,6 +47,15 @@ public class AndroidPushServiceImpl implements AndroidPushService {
case AndroidPushType.ANDROID_PUSH_TYPE_MEIZU:
meiZuPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_VIVO:
vivoPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_OPPO:
oppoPush.push(pushMessage);
break;
case AndroidPushType.ANDROID_PUSH_TYPE_FCM:
fcmPush.push(pushMessage);
break;
default:
LOG.info("unknown push type");
break;

View File

@@ -4,4 +4,7 @@ public interface AndroidPushType {
int ANDROID_PUSH_TYPE_XIAOMI = 1;
int ANDROID_PUSH_TYPE_HUAWEI = 2;
int ANDROID_PUSH_TYPE_MEIZU = 3;
int ANDROID_PUSH_TYPE_VIVO = 4;
int ANDROID_PUSH_TYPE_OPPO = 5;
int ANDROID_PUSH_TYPE_FCM = 6;
}

View File

@@ -0,0 +1,20 @@
package cn.wildfirechat.push.android.fcm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix="fcm")
@PropertySource(value = "file:config/fcm.properties")
public class FCMConfig {
private String credentialsPath;
public String getCredentialsPath() {
return credentialsPath;
}
public void setCredentialsPath(String credentialsPath) {
this.credentialsPath = credentialsPath;
}
}

View File

@@ -0,0 +1,53 @@
package cn.wildfirechat.push.android.fcm;
import cn.wildfirechat.push.PushMessage;
import cn.wildfirechat.push.PushMessageType;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.FileInputStream;
@Component
public class FCMPush {
private static final Logger LOG = LoggerFactory.getLogger(FCMPush.class);
@Autowired
private FCMConfig mConfig;
@PostConstruct
private void init() throws Exception {
FileInputStream refreshToken = new FileInputStream(mConfig.getCredentialsPath());
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
}
public void push(PushMessage pushMessage) {
Notification.Builder builder = Notification.builder().setTitle(pushMessage.senderName).setBody(pushMessage.pushContent);
Message message = Message.builder()
.setNotification(builder.build())
.setToken(pushMessage.deviceToken)
.build();
try {
// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
} catch (FirebaseMessagingException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,29 @@
package cn.wildfirechat.push.android.oppo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "oppo")
@PropertySource(value = "file:config/oppo.properties")
public class OppoConfig {
private String appSecret;
private String appKey;
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
}

View File

@@ -0,0 +1,147 @@
package cn.wildfirechat.push.android.oppo;
import cn.wildfirechat.push.PushMessage;
import cn.wildfirechat.push.PushMessageType;
import com.oppo.push.server.Notification;
import com.oppo.push.server.Result;
import com.oppo.push.server.Sender;
import com.oppo.push.server.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
@Component
public class OppoPush {
private static final Logger LOG = LoggerFactory.getLogger(OppoPush.class);
@Autowired
OppoConfig mConfig;
private Sender mSender;
@PostConstruct
private void init() {
try {
mSender = new Sender(mConfig.getAppKey(), mConfig.getAppSecret());
} catch (Exception e) {
e.printStackTrace();
}
}
public void push(PushMessage pushMessage) {
if (mSender == null) {
LOG.error("Oppo push message can't sent, because not initial correctly");
}
Result result = null;
try {
Notification notification = getNotification(pushMessage); //创建通知栏消息体
Target target = Target.build(pushMessage.deviceToken); //创建发送对象
result = mSender.unicastNotification(notification, target); //发送单推消息
result.getStatusCode(); // 获取http请求状态码
result.getReturnCode(); // 获取平台返回码
result.getMessageId(); // 获取平台返回的messageId
} catch (Exception e) {
e.printStackTrace();
LOG.error("sendSingle error " + e.getMessage());
}
if (result != null) {
LOG.info("Server response: MessageId: " + result.getMessageId()
+ " ErrorCode: " + result.getReturnCode()
+ " Reason: " + result.getReason());
}
}
private Notification getNotification(PushMessage pushMessage) {
if (pushMessage.isHiddenDetail) {
pushMessage.pushContent = "您收到一条新消息";
}
Notification notification = new Notification();
/**
* 以下参数必填项
*/
String title;
if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_FRIEND_REQUEST) {
if (StringUtils.isEmpty(pushMessage.senderName)) {
title = "好友请求";
} else {
title = pushMessage.senderName + " 请求加您为好友";
}
} else {
if (StringUtils.isEmpty(pushMessage.senderName)) {
title = "消息";
} else {
title = pushMessage.senderName;
}
}
notification.setTitle(title);
notification.setContent(pushMessage.pushContent);
/**
* 以下参数非必填项, 如果需要使用可以参考OPPO push服务端api文档进行设置
*/
//通知栏样式 1. 标准样式 2. 长文本样式 3. 大图样式 【非必填默认1-标准样式】
notification.setStyle(1);
// App开发者自定义消息IdOPPO推送平台根据此ID做去重处理对于广播推送相同appMessageId只会保存一次对于单推相同appMessageId只会推送一次
//notification.setAppMessageId(UUID.randomUUID().toString());
// 应用接收消息到达回执的回调URL字数限制200以内中英文均以一个计算
//notification.setCallBackUrl("http://www.test.com");
// App开发者自定义回执参数字数限制50以内中英文均以一个计算
//notification.setCallBackParameter("");
// 点击动作类型0启动应用1打开应用内页activity的intent action2打开网页4打开应用内页activity【非必填默认值为0】;5,Intent scheme URL
//notification.setClickActionType(4);
// 应用内页地址【click_action_type为1或4时必填长度500】
//notification.setClickActionActivity("com.coloros.push.demo.component.InternalActivity");
// 网页地址【click_action_type为2必填长度500】
//notification.setClickActionUrl("http://www.test.com");
// 动作参数打开应用内页或网页时传递给应用或网页【JSON格式非必填】字符数不能超过4K示例{"key1":"value1","key2":"value2"}
//notification.setActionParameters("{\"key1\":\"value1\",\"key2\":\"value2\"}");
// 展示类型 (0, “即时”),(1, “定时”)
notification.setShowTimeType(0);
// 定时展示开始时间根据time_zone转换成当地时间时间的毫秒数
//notification.setShowStartTime(System.currentTimeMillis() + 1000 * 60 * 3);
// 定时展示结束时间根据time_zone转换成当地时间时间的毫秒数
//notification.setShowEndTime(System.currentTimeMillis() + 1000 * 60 * 5);
// 是否进离线消息,【非必填默认为True】
//notification.setOffLine(true);
// 离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时必填最长3天】
if (pushMessage.pushMessageType != PushMessageType.PUSH_MESSAGE_TYPE_NORMAL) {
notification.setOffLineTtl(60); // 单位秒
} else {
notification.setOffLineTtl(10 * 60);
}
// 时区默认值GMT+08:00北京香港新加坡
//notification.setTimeZone("GMT+08:00");
// 0不限联网方式, 1仅wifi推送
notification.setNetworkType(0);
return notification;
}
}

View File

@@ -0,0 +1,38 @@
package cn.wildfirechat.push.android.vivo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ConfigurationProperties(prefix = "vivo")
@PropertySource(value = "file:config/vivo.properties")
public class VivoConfig {
private String appSecret;
private int appId;
private String appKey;
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public int getAppId() {
return appId;
}
public void setAppId(int appId) {
this.appId = appId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
}

View File

@@ -0,0 +1,91 @@
package cn.wildfirechat.push.android.vivo;
import cn.wildfirechat.push.PushMessage;
import cn.wildfirechat.push.PushMessageType;
import com.vivo.push.sdk.notofication.Message;
import com.vivo.push.sdk.notofication.Result;
import com.vivo.push.sdk.server.Sender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.IOException;
@Component
public class VivoPush {
private static final Logger LOG = LoggerFactory.getLogger(VivoPush.class);
private long tokenExpiredTime;
@Autowired
VivoConfig mConfig;
private String authToken;
private void refreshToken() {
Sender sender = null;//注册登录开发平台网站获取到的appSecret
try {
sender = new Sender(mConfig.getAppSecret());
Result result = sender.getToken(mConfig.getAppId(), mConfig.getAppKey());//注册登录开发平台网站获取到的appId和appKey
authToken = result.getAuthToken();
tokenExpiredTime = System.currentTimeMillis() + 12 * 60 * 60 * 1000;
} catch (Exception e) {
e.printStackTrace();
LOG.error("getToken error" + e.getMessage());
}
}
public void push(PushMessage pushMessage) {
if (tokenExpiredTime <= System.currentTimeMillis()) {
refreshToken();
}
Result resultMessage = null;
try {
if (pushMessage.isHiddenDetail) {
pushMessage.pushContent = "您收到一条新消息";
}
String title;
if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_FRIEND_REQUEST) {
if (StringUtils.isEmpty(pushMessage.senderName)) {
title = "好友请求";
} else {
title = pushMessage.senderName + " 请求加您为好友";
}
} else {
if (StringUtils.isEmpty(pushMessage.senderName)) {
title = "消息";
} else {
title = pushMessage.senderName;
}
}
Sender senderMessage = new Sender(mConfig.getAppSecret(), authToken);
Message.Builder builder = new Message.Builder()
.regId(pushMessage.getDeviceToken())//该测试手机设备订阅推送后生成的regId
.notifyType(3)
.title(title)
.content(pushMessage.pushContent)
.timeToLive(1000)
.skipType(1)
.networkType(-1)
.requestId(System.currentTimeMillis() + "");
if (pushMessage.pushMessageType != PushMessageType.PUSH_MESSAGE_TYPE_NORMAL) {
builder.timeToLive(60); // 单位秒
} else {
builder.timeToLive(10 * 60);
}
resultMessage = senderMessage.sendSingle(builder.build());
} catch (Exception e) {
e.printStackTrace();
LOG.error("sendSingle error " + e.getMessage());
}
if (resultMessage != null) {
LOG.info("Server response: MessageId: " + resultMessage.getTaskId()
+ " ErrorCode: " + resultMessage.getResult()
+ " Reason: " + resultMessage.getDesc());
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.