diff --git a/.gitignore b/.gitignore index c507849..b6dcbc7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target .idea +push.iml diff --git a/config/fcm.properties b/config/fcm.properties new file mode 100644 index 0000000..33cc2c0 --- /dev/null +++ b/config/fcm.properties @@ -0,0 +1 @@ +fcm.credentialsPath=fcm/wildfirechat-4753c-firebase-adminsdk-1s9fh-bc026cc369.json \ No newline at end of file diff --git a/config/oppo.properties b/config/oppo.properties new file mode 100644 index 0000000..fffdcd1 --- /dev/null +++ b/config/oppo.properties @@ -0,0 +1,2 @@ +oppo.AppKey=16c6afe503b24259928e082ef01a6bf2 +oppo.AppSecret=2114e4067de4424fbfc0638e311ce88c \ No newline at end of file diff --git a/config/vivo.properties b/config/vivo.properties new file mode 100644 index 0000000..1cd166c --- /dev/null +++ b/config/vivo.properties @@ -0,0 +1,3 @@ +vivo.appSecret=d0f24e5b-e92b-4b95-8d45-927bec3ba512 +vivo.appId=12918 +vivo.appKey=c42feb05-de6c-427d-af55-4f902d9e0a75 \ No newline at end of file diff --git a/fcm/wildfirechat-4753c-firebase-adminsdk-1s9fh-bc026cc369.json b/fcm/wildfirechat-4753c-firebase-adminsdk-1s9fh-bc026cc369.json new file mode 100644 index 0000000..e6c9fa2 --- /dev/null +++ b/fcm/wildfirechat-4753c-firebase-adminsdk-1s9fh-bc026cc369.json @@ -0,0 +1,12 @@ +{ + "type": "service_account", + "project_id": "wildfirechat-4753c", + "private_key_id": "bc026cc369dbceee7ea84cfafa25e9d617c37a55", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADxyBgkqzziG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8FleZOk5Yd8Z7\nd+LJ0vfSTZWXLHxXZcp53vnX3yLlO20X/mAvgELvZJEHHCDTYhb67+O+EJUCrGWv\nXrdqTJf31sO/IxB1pdFZVomsT+04WUZaEUTFD7kdRKHVn89HIAZRqxh3tv9gdfuc\nRfh4JE8XC3krCFKz+OSzbq2tO8kEdwOOljT/3tU9BUbQ9uKd5stamTafzBuQ5lQ8\n/0jGcjpcOigpuT3nnpDmIyXKOGhnA2euVFxaRzyoEFoloatG/qhPqGlDUFy8jJLJ\n5TERbFHpJmGxFLu+apYQ8MaM8zIMNxQtq4f+e/M7szpwmtrxJYrodNamImODWr5t\nsrhrXM2NAgMBAAECggEAA71Hh5CCW69a0MpeqWX99hrKqNiXg5fAbfFK+jRza4rz\n28s0YjuZ2RLn0+X4UcRuTmsz+WvF+b7MP2hgKerNzf2MIxAtyjSMd66B7hYE7tex\nwVCLIb8rqrboblMdUyFJXRjacBEWma8uzeufwmF+3NImL2JyCEKDytIeNpv7EGaS\nEPEtOfkyvgSK6FfG7K+Tahgu4BVCLkk41Jz8ZBpdqMuUvDBdF1MLb506XtNSsGrd\nBQstyB3emGxXsWfKEmKlZ5bqs7RC0UCF74YCNZ1iUegghJzF/5sSbQBtEsEaE3LP\n4BJ+nKWdRY0ddNvxrRg72bBqvm6L8KaviXHK4L9jxwKBgQD/OdjRdh8tKYI0tF7i\nqy5bHlGiWcvrmartq0Y4+gPoBaLdCKLoDDoNRp3KYyGJAvnSMb0Run74BOLwGxhP\nEGFQrUYUqDEaqyfMBfB6MA3LLmoWi8DFL0yPLeavQEesSnf6k5ZyeBWMw+MfBtIR\nqYssEtt70XWQKD8OAsRjCtSlzwKBgQC8qF6yapbEwJjL1rWjJDBvHxKRnMNehbVp\nl6oKjXa9j9Eu3BqiuCnkBNxHn9KVLE3DZLwGBypgzqenw1EVqkwhBY2j6miEsXpq\n6eixwjK8RGPF611AkVSITssHVqJX279MYac6Qqs6LGBt+5RlZU+LgS5XjZdY7A/7\ny8jXUU6J4wKBgFvJYPlLYsDqKaJsr6cS6vPn/nHFm3maZQdeTj3VZCxAQqb9ISEU\nrJfzqnGLnxbYFCIMszM5/8mQcFDrXZ2phWXvOsHZDvfJT+GmzSoUOBG3Gn4LOoNv\nlDf1yP2Dv4LtsSJTi4qVNyLNm9Ofr1Kcx6lNCtQHqyDx6GNQiXIZBgqvAoGAQOJ3\nj01xhUHoS1MRLYbo0Y8SR9pupze9aQ+sf+yfhhrMix1QW96TKcUyLcAeIQgXxrgC\nouAcByeMKfrXDGiBvmm2iUW5pMmVnOfODDUKVg3/cra8X9ScCjVvRprZQvZlGNQN\nO9BqK5vPnoQESctKTGSll58vCnpGFQfFj6h7JpkCgYEA1xX7JZuaQCo4121wr+3T\nwfa0M8875ApO+c/Rii6y40YIfdwvbHadAIJHmFgM3V+R7ps3K+aBwBFUdT8rSsek\nMctsBYGvZyU7Or2zkTY3+GO2ZCx1Sg0IpUmWULKkbqaRQgHuW4SGyxJYNUCNKo8U\n69+k0zzdIxX0TUiqk4enbOA=\n-----END PRIVATE KEY-----\n", + "client_email": "firebase-adminsdk-1s9fh@wildfirechat-4753c.iam.gserviceaccount.com", + "client_id": "114736707742305810886", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-1s9fh%40wildfirechat-4753c.iam.gserviceaccount.com" +} diff --git a/pom.xml b/pom.xml index e5a7aef..cd134f0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,144 +1,212 @@ - - 4.0.0 + + 4.0.0 - cn.wildfirechat - push - 0.0.2 - jar + cn.wildfirechat + push + 0.0.1-SNAPSHOT + jar - push - Demo project for Spring Boot + push + Demo project for Spring Boot - - org.springframework.boot - spring-boot-starter-parent - 2.2.10.RELEASE - - + + org.springframework.boot + spring-boot-starter-parent + 2.0.6.RELEASE + + - - UTF-8 - UTF-8 - 1.8 - + + UTF-8 + UTF-8 + 1.8 + - - - org.springframework.boot - spring-boot-starter - + + + org.springframework.boot + spring-boot-starter + - - org.springframework.boot - spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-test - test - + + org.springframework.boot + spring-boot-starter-test + test + - - com.xiaomi.push - mipush-sdk-server - 2.2.18 - system - ${project.basedir}/src/main/libs/MiPush_SDK_Server_2_2_19.jar - - + + com.xiaomi.push + mipush-sdk-server + 2.2.18 + system + ${project.basedir}/src/main/libs/MiPush_SDK_Server_2_2_19.jar + + - - com.meizu.flyme - push-server-sdk - 1.2.7.20180307_release - + + com.oppo.push + opush-server + 1.0.4 + system + ${project.basedir}/src/main/libs/opush-server-sdk-1.0.4.jar + + - - - com.google.code.gson - gson - 2.8.2 - + + com.meizu.flyme + push-server-sdk + 1.2.7.20180307_release + - - commons-io - commons-io - 2.5 - - - - com.googlecode.json-simple - json-simple - 1.1.1 - - - - org.slf4j - slf4j-api - 1.7.5 - - - - org.slf4j - slf4j-log4j12 - 1.7.5 - + + + com.vivo.push.sdk + vPush-server-sdk + 1.0 + system + ${project.basedir}/src/main/libs/vPush-server-sdk-1.0.jar + + - - commons-httpclient - commons-httpclient - 3.1 - - - uk.org.lidalia - slf4j-test - 1.0.0-jdk6 - test - + + + + + + + + - - com.google.code.findbugs - annotations - 2.0.3 - provided - + + + + + + + + - - org.mockito - mockito-all - 1.9.5 - jar - test - + + + + + + + + + + + org.apache.http + httpclient + 4.5 + system + ${project.basedir}/src/main/libs/httpclient-4.5.jar + + + + + org.apache.http + httpcore + 4.4.1 + system + ${project.basedir}/src/main/libs/httpcore-4.4.1.jar + + + + + + + + com.google.code.gson + gson + 2.8.2 + + + + commons-io + commons-io + 2.5 + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + + org.slf4j + slf4j-api + 1.7.5 + + + + org.slf4j + slf4j-log4j12 + 1.7.5 + - - com.fasterxml.jackson.core - jackson-core - [2.9.10.3,) - - - com.fasterxml.jackson.core - jackson-databind - [2.9.10.3,) - - - com.fasterxml.jackson.core - jackson-annotations - [2.9.10.3,) - + + commons-httpclient + commons-httpclient + 3.1 + + + + uk.org.lidalia + slf4j-test + 1.0.0-jdk6 + test + + + + com.google.code.findbugs + annotations + 2.0.3 + provided + + + + org.mockito + mockito-all + 1.9.5 + jar + test + - - com.turo - pushy - 0.13.10 - + + com.fasterxml.jackson.core + jackson-core + 2.9.8 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.8 + + + com.fasterxml.jackson.core + jackson-annotations + 2.9.8 + + + + + com.turo + pushy + 0.13.10 + com.turo @@ -146,7 +214,20 @@ 0.13.10 - + + com.google.firebase + firebase-admin + 7.1.0 + + + + com.google.guava + guava + 30.0-jre + + + + @@ -154,17 +235,23 @@ - - - - org.springframework.boot - spring-boot-maven-plugin - - true - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + src/main/libs + BOOT-INF/lib/ + + **/*.jar + + + + diff --git a/push.iml b/push.iml deleted file mode 100644 index dbf32f9..0000000 --- a/push.iml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/java/cn/wildfirechat/push/android/AndroidPushServiceImpl.java b/src/main/java/cn/wildfirechat/push/android/AndroidPushServiceImpl.java index 2ebd7f2..b5974d4 100644 --- a/src/main/java/cn/wildfirechat/push/android/AndroidPushServiceImpl.java +++ b/src/main/java/cn/wildfirechat/push/android/AndroidPushServiceImpl.java @@ -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; diff --git a/src/main/java/cn/wildfirechat/push/android/AndroidPushType.java b/src/main/java/cn/wildfirechat/push/android/AndroidPushType.java index f52ec04..febff3f 100644 --- a/src/main/java/cn/wildfirechat/push/android/AndroidPushType.java +++ b/src/main/java/cn/wildfirechat/push/android/AndroidPushType.java @@ -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; } diff --git a/src/main/java/cn/wildfirechat/push/android/fcm/FCMConfig.java b/src/main/java/cn/wildfirechat/push/android/fcm/FCMConfig.java new file mode 100644 index 0000000..058f75d --- /dev/null +++ b/src/main/java/cn/wildfirechat/push/android/fcm/FCMConfig.java @@ -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; + } +} diff --git a/src/main/java/cn/wildfirechat/push/android/fcm/FCMPush.java b/src/main/java/cn/wildfirechat/push/android/fcm/FCMPush.java new file mode 100644 index 0000000..901fd17 --- /dev/null +++ b/src/main/java/cn/wildfirechat/push/android/fcm/FCMPush.java @@ -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://.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(); + } + } +} diff --git a/src/main/java/cn/wildfirechat/push/android/oppo/OppoConfig.java b/src/main/java/cn/wildfirechat/push/android/oppo/OppoConfig.java new file mode 100644 index 0000000..0a68473 --- /dev/null +++ b/src/main/java/cn/wildfirechat/push/android/oppo/OppoConfig.java @@ -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; + } +} diff --git a/src/main/java/cn/wildfirechat/push/android/oppo/OppoPush.java b/src/main/java/cn/wildfirechat/push/android/oppo/OppoPush.java new file mode 100644 index 0000000..04ea7f3 --- /dev/null +++ b/src/main/java/cn/wildfirechat/push/android/oppo/OppoPush.java @@ -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开发者自定义消息Id,OPPO推送平台根据此ID做去重处理,对于广播推送相同appMessageId只会保存一次,对于单推相同appMessageId只会推送一次 + //notification.setAppMessageId(UUID.randomUUID().toString()); + + // 应用接收消息到达回执的回调URL,字数限制200以内,中英文均以一个计算 + //notification.setCallBackUrl("http://www.test.com"); + + // App开发者自定义回执参数,字数限制50以内,中英文均以一个计算 + //notification.setCallBackParameter(""); + + // 点击动作类型0,启动应用;1,打开应用内页(activity的intent action);2,打开网页;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; + } +} diff --git a/src/main/java/cn/wildfirechat/push/android/vivo/VivoConfig.java b/src/main/java/cn/wildfirechat/push/android/vivo/VivoConfig.java new file mode 100644 index 0000000..d0e1eb1 --- /dev/null +++ b/src/main/java/cn/wildfirechat/push/android/vivo/VivoConfig.java @@ -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; + } +} diff --git a/src/main/java/cn/wildfirechat/push/android/vivo/VivoPush.java b/src/main/java/cn/wildfirechat/push/android/vivo/VivoPush.java new file mode 100644 index 0000000..4eb8061 --- /dev/null +++ b/src/main/java/cn/wildfirechat/push/android/vivo/VivoPush.java @@ -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()); + } + } +} diff --git a/src/main/libs/httpclient-4.5.jar b/src/main/libs/httpclient-4.5.jar new file mode 100644 index 0000000..970c989 Binary files /dev/null and b/src/main/libs/httpclient-4.5.jar differ diff --git a/src/main/libs/httpcore-4.4.1.jar b/src/main/libs/httpcore-4.4.1.jar new file mode 100644 index 0000000..99715b6 Binary files /dev/null and b/src/main/libs/httpcore-4.4.1.jar differ diff --git a/src/main/libs/opush-server-sdk-1.0.4.jar b/src/main/libs/opush-server-sdk-1.0.4.jar new file mode 100644 index 0000000..f502f2e Binary files /dev/null and b/src/main/libs/opush-server-sdk-1.0.4.jar differ diff --git a/src/main/libs/vPush-server-sdk-1.0.jar b/src/main/libs/vPush-server-sdk-1.0.jar new file mode 100755 index 0000000..904175d Binary files /dev/null and b/src/main/libs/vPush-server-sdk-1.0.jar differ