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<String, String> extras = new HashMap<String, String>();
            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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值