package com.ruoyi.common.utils; import cn.hutool.json.JSONObject; 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.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.Notification; import com.google.gson.JsonObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Slf4j @Component public class JPushUtils { @Value("${jPush.appKey}") private String appKey; @Value("${jPush.masterSecret}") private String masterSecret; /** * 推送消息 * @param alias 别名 * @param alert 内容 * @param title 标题 */ public void jPush(String[] alias, String alert, String title, String bigText, Map extras, JsonObject intent){ JPushClient jPushClient = new JPushClient("13abca75f15bca1cded3151b", "2bde1fa85c62c396a2858ae6"); // 厂商 JsonObject oppo = new JsonObject(); oppo.addProperty("channel_id", "199"); JsonObject xiaomi = new JsonObject(); xiaomi.addProperty("channel_id", "127152"); JsonObject huawei = new JsonObject(); huawei.addProperty("importance", "NORMAL"); huawei.addProperty("target_user_type", 1); huawei.addProperty("category", "EXPRESS"); Map thirdPartyChannel = new HashMap<>(); thirdPartyChannel.put("oppo", oppo); thirdPartyChannel.put("xiaomi", xiaomi); thirdPartyChannel.put("huawei", huawei); PushPayload pushPayload = PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.alias(alias)) .setOptions(Options.newBuilder() .setClassification(1) .setThirdPartyChannelV2(thirdPartyChannel) .build()) .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() .setAlert(alert) .setTitle(title) .setBigText(bigText) .addExtras(extras) .setIntent(intent) .build()) .build()) .build(); try { PushResult pushResult = jPushClient.sendPush(pushPayload); log.info("[极光推送]PushResult result is " + pushResult); } catch (APIConnectionException | APIRequestException e) { throw new RuntimeException(e); } } public static void main(String[] args) { JPushUtils jPushUtils = new JPushUtils(); String[] alias = new String[]{"111"}; // jPushUtils.jPush(alias, "请及时查看", "千冻网"); } }