极光 服务端工具类 jiguang-sdk

引入maven

  <!-- jiguang-sdk -->
            <dependency>
                <groupId>io.github.jpush</groupId>
                <artifactId>jiguang-sdk</artifactId>
                <version>5.1.10</version>
            </dependency>

编写工具类

/**
 * @Author:那日花开
 * @Package:com.geovis.common.core.utiles
 * @Project:digit-trade-platform
 * @name:JPushUtil
 * @Date:2024/12/5 10:42
 * @Filename:JPushUtil
 */
public class JPushUtil {

    // 设置好账号的app_key和masterSecret是必须的
    private static String APP_KEY = "03b344b****c94346956";
    private static String MASTER_SECRET = "05c****00e0b6e62";


   
    public static void jpushAndroid(Map<String, String> parm, List<String> registrationIds) {

        //创建JPushClient(极光推送的实例)
        PushApi pushApi = new PushApi.Builder()
                .setAppKey(APP_KEY)
                .setMasterSecret(MASTER_SECRET)
                .build();
        PushSendParam pushSendParam= new PushSendParam();
        pushSendParam.setPlatform(Platform.android);
        if(registrationIds!=null&&registrationIds.size()>0){
            Audience audience =new Audience();
            audience.setRegistrationIdList(registrationIds);
            pushSendParam.setAudience(audience);
        }else{
            pushSendParam.setAudience("all");
        }
        NotificationMessage notificationMessage= new NotificationMessage();
        notificationMessage.setAlert(parm.get("title"));
        NotificationMessage.Android a=new NotificationMessage.Android();
        a.setAlert( parm.get("alert"));
        a.setTitle( parm.get("title"));

        notificationMessage.setAndroid(a);
        pushSendParam.setNotification(notificationMessage);
        Options options= new Options();
        options.setApnsProduction(true);
        pushSendParam.setOptions(options);
        pushApi.send(pushSendParam);

    }

    //极光推送>>ios
    //Map<String, String> parm是我自己传过来的参数,可以自定义参数
    public static  void jpushIOS(Map<String, Object> parm, List<String> registrationIds) {

        //创建JPushClient
        PushApi pushApi = new PushApi.Builder()
                .setAppKey(APP_KEY)
                .setMasterSecret(MASTER_SECRET)
                .build();
        PushSendParam pushSendParam= new PushSendParam();
        pushSendParam.setPlatform(Platform.ios);
        if(registrationIds!=null&&registrationIds.size()>0){
            Audience audience =new Audience();
            audience.setRegistrationIdList(registrationIds);
            pushSendParam.setAudience(audience);
        }else{
            pushSendParam.setAudience("all");
        }

        NotificationMessage notificationMessage= new NotificationMessage();
        notificationMessage.setAlert(parm.get("alert")+"");
        NotificationMessage.IOS a=new NotificationMessage.IOS();
        a.setAlert(parm.get("alert"));
        a.setExtras(parm);

        pushSendParam.setNotification(notificationMessage);
        Options options= new Options();
        options.setApnsProduction(true);
        pushSendParam.setOptions(options);
        pushApi.send(pushSendParam);

    }



    //极光推送>>All所有平台
    public static void jpushAll(Map<String, Object> parm, List<String> registrationIds) {
        //创建JPushClient
        PushApi pushApi = new PushApi.Builder()
                .setAppKey(APP_KEY)
                .setMasterSecret(MASTER_SECRET)
                .build();

        PushSendParam pushSendParam= new PushSendParam();
        pushSendParam.setPlatform(ApiConstants.Platform.ALL);
        if(registrationIds!=null&&registrationIds.size()>0){
            Audience audience =new Audience();
            audience.setRegistrationIdList(registrationIds);
            pushSendParam.setAudience(audience);
        }else{
            pushSendParam.setAudience(ApiConstants.Audience.ALL);
        }
        NotificationMessage notificationMessage= new NotificationMessage();
        notificationMessage.setAlert(parm.get("alert")+"");
        NotificationMessage.IOS a=new NotificationMessage.IOS();
        a.setAlert(parm.get("alert"));
        a.setExtras(parm);


        notificationMessage.setIos(a);
        NotificationMessage.Android b=new NotificationMessage.Android();
        b.setAlert(""+ parm.get("alert"));
        b.setTitle(String.valueOf(parm.get("title")));
        b.setCategory("0");
        b.setExtras(parm);
        notificationMessage.setAndroid(b);
        NotificationMessage.HMOS c=new NotificationMessage.HMOS();
        c.setAlert(""+parm.get("alert"));
        c.setCategory("0");
        c.setExtras(parm);
        c.setBadgeAddNumber(1);
        notificationMessage.setHmos(c);

        pushSendParam.setNotification(notificationMessage);
        Options options= new Options();
        options.setApnsProduction(true);
        pushSendParam.setOptions(options);

        pushApi.send(pushSendParam);
    }

    public static void main(String[] args) {
        Map<String, String> parm = new HashMap<>();
        parm.put("title","我是通知");
        parm.put("alert","tong zhi nei rong");

        List<String> registrationIds= new ArrayList<>();
        registrationIds.add("1104a89793af2cfc030");
        jpushAndroid(parm,registrationIds);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值