Spring Boot中调用极光推送做服务

@Service
public class JGPushServiceImpl implements JGPushService {

    private final static String strAppKey = "";
    private final static String strMasterSecret = "";

    public void JGImmediatelyPush(Integer pushType, String strTitle, String strMessage, Date datePushTime,
            Integer iIsAndroid, Integer iIsIos, Long alias, Integer iType, String strExtras) {
        PushResult result = JGPush(pushType, strTitle, strMessage, iIsAndroid, iIsIos, alias, strExtras);
    }

    private PushResult JGPush(Integer pushType, String strTitle, String strMessage, Integer iIsAndroid, Integer iIsIos,
            Long alias, String strExtras) {
        PushResult result = null;
        ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(strMasterSecret, strAppKey, null, clientConfig);

        PushPayload pushPayload = BuildPushPayload(pushType, strTitle, strMessage, iIsAndroid, iIsIos, alias,
                strExtras);

        try {
            result = jpushClient.sendPush(pushPayload);
            System.out.println(result);
        } catch (APIConnectionException e) {
            System.out.println("APIConnectionException");
        } catch (APIRequestException e) {
            System.out.println("APIRequestException");
        }

        return result;
    }

    private PushPayload BuildPushPayload(Integer pushType, String strTitle, String strMessage, Integer iIsAndroid,
            Integer iIsIos, Long alias, String strExtras) {
        PushPayload.Builder pushPayloadBuilder = PushPayload.newBuilder();

        if (alias == 0)
            pushPayloadBuilder.setAudience(Audience.all());
        else
            pushPayloadBuilder.setAudience(Audience.alias(alias.toString()));

        if (iIsAndroid == 1)
            pushPayloadBuilder.setPlatform(Platform.android());
        if (iIsIos == 1)
            pushPayloadBuilder.setPlatform(Platform.ios());

        switch (pushType) {
        case 1:
            pushPayloadBuilder.setNotification(BuildNotification(strTitle, strMessage, iIsAndroid, iIsIos, strExtras));
            break;
        case 2:
            pushPayloadBuilder.setMessage(BuildMessage(strTitle, strMessage, strExtras));
            break;
        default:
            pushPayloadBuilder.setNotification(BuildNotification(strTitle, strMessage, iIsAndroid, iIsIos, strExtras));
            pushPayloadBuilder.setMessage(BuildMessage(strTitle, strMessage, strExtras));
            break;
        }

        return pushPayloadBuilder.build();
    }

    private Notification BuildNotification(String strTitle, String strMessage, Integer iIsAndroid, Integer iIsIos,
            String strExtras) {
        Notification.Builder notificationBuilder = Notification.newBuilder();

        if (iIsAndroid == 1)
            notificationBuilder.addPlatformNotification(BuildAndroidNotification(strTitle, strMessage, strExtras));

        if (iIsIos == 1)
            notificationBuilder.addPlatformNotification(BuildIosNotification(strMessage, strExtras));

        return notificationBuilder.build();
    }

    private Message BuildMessage(final String strTitle, final String strMessage, final String strExtras) {
        Message.Builder messageBuilder = Message.newBuilder();
        messageBuilder.setTitle(strTitle);
        messageBuilder.setMsgContent(strMessage);
        if (strExtras != null) {
            Map extras = new HashMap();
            extras.put("extras", strExtras);
            messageBuilder.addExtras(extras);
        }

        return messageBuilder.build();
    }

    private AndroidNotification BuildAndroidNotification(String strTitle, String strMessage, String strExtras) {
        AndroidNotification.Builder androidNotificationBuilder = AndroidNotification.newBuilder();
        androidNotificationBuilder.setTitle(strTitle);
        androidNotificationBuilder.setAlert(strMessage);
        if (strExtras != null)
            androidNotificationBuilder.addExtra("extras", strExtras);

        return androidNotificationBuilder.build();
    }

    private IosNotification BuildIosNotification(String strMessage, String strExtras) {
        IosNotification.Builder iosNotificationBuilder = IosNotification.newBuilder();
        iosNotificationBuilder.incrBadge(1);
        iosNotificationBuilder.setAlert(strMessage);
        if (strExtras != null)
            iosNotificationBuilder.addExtra("extras", strExtras);

        return iosNotificationBuilder.build();
    }
}

你可能感兴趣的:(Spring Boot中调用极光推送做服务)