|
@@ -0,0 +1,376 @@
|
|
|
+package com.ozs.jpush;
|
|
|
+
|
|
|
+import cn.jiguang.common.resp.APIConnectionException;
|
|
|
+import cn.jiguang.common.resp.APIRequestException;
|
|
|
+import cn.jpush.api.JPushClient;
|
|
|
+import cn.jpush.api.push.PushResult;
|
|
|
+import cn.jpush.api.push.model.Message;
|
|
|
+import cn.jpush.api.push.model.Options;
|
|
|
+import cn.jpush.api.push.model.Platform;
|
|
|
+import cn.jpush.api.push.model.PushPayload;
|
|
|
+import cn.jpush.api.push.model.audience.Audience;
|
|
|
+import cn.jpush.api.push.model.notification.AndroidNotification;
|
|
|
+import cn.jpush.api.push.model.notification.IosNotification;
|
|
|
+import cn.jpush.api.push.model.notification.Notification;
|
|
|
+import cn.jpush.api.schedule.ScheduleResult;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+ * @Author : sunhh
|
|
|
+ * @create 2023/3/13 11:19
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class JPushUtil {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private final static String APPKER = "1712ced750e33d75d18cb577";
|
|
|
+ private final static String MASTERSECRET = "e4b8ba2eda2acd0f75177b88";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 推送给设备标识参数的用户
|
|
|
+ */
|
|
|
+ public static int sendToRegistrationId(List<String> alias, String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKER);
|
|
|
+ int result = 0;
|
|
|
+ try {
|
|
|
+ PushPayload pushPayload = JPushUtil.buildPushObjectAllAliasAlertWithTitle(alias, notification_title, msg_title, msg_content, extrasparam);
|
|
|
+ PushResult pushResult = jPushClient.sendPush(pushPayload);
|
|
|
+ if (pushResult.getResponseCode() == 200) {
|
|
|
+ result = 1;
|
|
|
+ }
|
|
|
+ log.info("[极光推送]PushResult result is " + pushResult);
|
|
|
+ } catch (APIConnectionException e) {
|
|
|
+ log.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
+ } catch (APIRequestException e) {
|
|
|
+ log.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
+ log.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
+ log.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
+ log.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 推送给设备标识参数的用户(定时)
|
|
|
+ */
|
|
|
+ public static int sendToRegistrationId2(List<String> alias, String notification_title, String msg_title, String msg_content, String extrasparam, String time) {
|
|
|
+ JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKER);
|
|
|
+ int result = 0;
|
|
|
+ try {
|
|
|
+ PushPayload pushPayload = JPushUtil.buildPushObjectAllAliasAlertWithTitle(alias, notification_title, msg_title, msg_content, extrasparam);
|
|
|
+ ScheduleResult scheduleResult = jPushClient.createSingleSchedule("测试", time, pushPayload);
|
|
|
+ if (scheduleResult.getResponseCode() == 200) {
|
|
|
+ result = 1;
|
|
|
+ }
|
|
|
+ log.info("[极光推送]ScheduleResult result is " + scheduleResult);
|
|
|
+ } catch (APIConnectionException e) {
|
|
|
+ log.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
+ } catch (APIRequestException e) {
|
|
|
+ log.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
+ log.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
+ log.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
+ log.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送给所有安卓用户
|
|
|
+ */
|
|
|
+ public static int sendToAllAndroid(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKER);
|
|
|
+ int result = 0;
|
|
|
+ try {
|
|
|
+ PushPayload pushPayload = JPushUtil.buildPushObjectAndroidAllAlertWithTitle(notification_title, msg_title, msg_content, extrasparam);
|
|
|
+ PushResult pushResult = jPushClient.sendPush(pushPayload);
|
|
|
+ if (pushResult.getResponseCode() == 200) {
|
|
|
+ result = 1;
|
|
|
+ }
|
|
|
+ log.info("[极光推送]PushResult result is " + pushResult);
|
|
|
+ } catch (APIConnectionException e) {
|
|
|
+ log.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
+ } catch (APIRequestException e) {
|
|
|
+ log.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
+ log.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
+ log.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
+ log.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送给所有IOS用户
|
|
|
+ */
|
|
|
+ public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKER);
|
|
|
+ int result = 0;
|
|
|
+ try {
|
|
|
+ PushPayload pushPayload = JPushUtil.buildPushObjectIosAllAlertWithTitle(notification_title, msg_title, msg_content, extrasparam);
|
|
|
+ PushResult pushResult = jPushClient.sendPush(pushPayload);
|
|
|
+ if (pushResult.getResponseCode() == 200) {
|
|
|
+ result = 1;
|
|
|
+ }
|
|
|
+ log.info("[极光推送]PushResult result is " + pushResult);
|
|
|
+ } catch (APIConnectionException e) {
|
|
|
+ log.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
+ } catch (APIRequestException e) {
|
|
|
+ log.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
+ log.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
+ log.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
+ log.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送给所有用户
|
|
|
+ */
|
|
|
+ public static int sendToAll(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKER);
|
|
|
+ int result = 0;
|
|
|
+ try {
|
|
|
+ PushPayload pushPayload = JPushUtil.buildPushObjectAndroidAndIos(notification_title, msg_title, msg_content, extrasparam);
|
|
|
+ PushResult pushResult = jPushClient.sendPush(pushPayload);
|
|
|
+ if (pushResult.getResponseCode() == 200) {
|
|
|
+ result = 1;
|
|
|
+ }
|
|
|
+ log.info("[极光推送]PushResult result is " + pushResult);
|
|
|
+ } catch (APIConnectionException e) {
|
|
|
+ log.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
+ } catch (APIRequestException e) {
|
|
|
+ log.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
+ log.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
+ log.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
+ log.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 发送给所有用户(定时推送)
|
|
|
+ */
|
|
|
+ public static int sendToAll2(String notification_title, String msg_title, String msg_content, String extrasparam, String time) {
|
|
|
+ JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKER);
|
|
|
+ int result = 0;
|
|
|
+ try {
|
|
|
+ PushPayload pushPayload = JPushUtil.buildPushObjectAndroidAndIos(notification_title, msg_title, msg_content, extrasparam);
|
|
|
+ ScheduleResult scheduleResult = jPushClient.createSingleSchedule("测试", time, pushPayload);
|
|
|
+ if (scheduleResult.getResponseCode() == 200) {
|
|
|
+ result = 1;
|
|
|
+ }
|
|
|
+ log.info("[极光推送]scheduleResult result is " + scheduleResult);
|
|
|
+ } catch (APIConnectionException e) {
|
|
|
+ log.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
+ } catch (APIRequestException e) {
|
|
|
+ log.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
+ log.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
+ log.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
+ log.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static PushPayload buildPushObjectAndroidAndIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ return PushPayload.newBuilder()
|
|
|
+ .setPlatform(Platform.android_ios())
|
|
|
+ .setAudience(Audience.all())
|
|
|
+ .setNotification(Notification.newBuilder()
|
|
|
+ .setAlert(msg_content)
|
|
|
+ .addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
+ .setAlert(msg_content)
|
|
|
+ .setTitle(notification_title)
|
|
|
+
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ .addPlatformNotification(IosNotification.newBuilder()
|
|
|
+
|
|
|
+ .setAlert(msg_content)
|
|
|
+
|
|
|
+
|
|
|
+ .incrBadge(1)
|
|
|
+
|
|
|
+
|
|
|
+ .setSound("sound.caf")
|
|
|
+
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+
|
|
|
+
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .setMessage(Message.newBuilder()
|
|
|
+ .setMsgContent(msg_content)
|
|
|
+ .setTitle(msg_title)
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build())
|
|
|
+ .setOptions(Options.newBuilder()
|
|
|
+
|
|
|
+ .setApnsProduction(true)
|
|
|
+
|
|
|
+ .setSendno(1)
|
|
|
+
|
|
|
+ .setTimeToLive(86400)
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static PushPayload buildPushObjectAllAliasAlertWithTitle(List<String> alias, String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+
|
|
|
+
|
|
|
+ return PushPayload.newBuilder()
|
|
|
+
|
|
|
+ .setPlatform(Platform.all())
|
|
|
+
|
|
|
+ .setAudience(Audience.registrationId(alias))
|
|
|
+
|
|
|
+
|
|
|
+ .setNotification(Notification.newBuilder()
|
|
|
+
|
|
|
+ .addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
+ .setAlert(msg_content)
|
|
|
+ .setTitle(notification_title)
|
|
|
+
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build())
|
|
|
+
|
|
|
+ .addPlatformNotification(IosNotification.newBuilder()
|
|
|
+
|
|
|
+ .setAlert(msg_content)
|
|
|
+
|
|
|
+
|
|
|
+ .incrBadge(1)
|
|
|
+
|
|
|
+
|
|
|
+ .setSound("sound.caf")
|
|
|
+
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .build())
|
|
|
+ .build())
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .setMessage(Message.newBuilder()
|
|
|
+ .setMsgContent(msg_content)
|
|
|
+ .setTitle(msg_title)
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build())
|
|
|
+ .setOptions(Options.newBuilder()
|
|
|
+
|
|
|
+ .setApnsProduction(true)
|
|
|
+
|
|
|
+ .setSendno(1)
|
|
|
+
|
|
|
+ .setTimeToLive(86400)
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static PushPayload buildPushObjectAndroidAllAlertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ return PushPayload.newBuilder()
|
|
|
+
|
|
|
+ .setPlatform(Platform.android())
|
|
|
+
|
|
|
+ .setAudience(Audience.all())
|
|
|
+
|
|
|
+ .setNotification(Notification.newBuilder()
|
|
|
+
|
|
|
+ .addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
+ .setAlert(msg_content)
|
|
|
+ .setTitle(notification_title)
|
|
|
+
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build())
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .setMessage(Message.newBuilder()
|
|
|
+ .setMsgContent(msg_content)
|
|
|
+ .setTitle(msg_title)
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build())
|
|
|
+ .setOptions(Options.newBuilder()
|
|
|
+
|
|
|
+ .setApnsProduction(true)
|
|
|
+
|
|
|
+ .setSendno(1)
|
|
|
+
|
|
|
+ .setTimeToLive(86400)
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static PushPayload buildPushObjectIosAllAlertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
+ return PushPayload.newBuilder()
|
|
|
+
|
|
|
+ .setPlatform(Platform.ios())
|
|
|
+
|
|
|
+ .setAudience(Audience.all())
|
|
|
+
|
|
|
+ .setNotification(Notification.newBuilder()
|
|
|
+
|
|
|
+ .addPlatformNotification(IosNotification.newBuilder()
|
|
|
+
|
|
|
+ .setAlert(msg_content)
|
|
|
+
|
|
|
+
|
|
|
+ .incrBadge(1)
|
|
|
+
|
|
|
+
|
|
|
+ .setSound("sound.caf")
|
|
|
+
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+
|
|
|
+
|
|
|
+ .build())
|
|
|
+ .build()
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ .setMessage(Message.newBuilder()
|
|
|
+ .setMsgContent(msg_content)
|
|
|
+ .setTitle(msg_title)
|
|
|
+ .addExtra("url", extrasparam)
|
|
|
+ .build())
|
|
|
+ .setOptions(Options.newBuilder()
|
|
|
+
|
|
|
+ .setApnsProduction(true)
|
|
|
+
|
|
|
+ .setSendno(1)
|
|
|
+
|
|
|
+ .setTimeToLive(86400)
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|