jpush推送通知ios、android

 

引入jpush sdk


    cn.jpush.api
    jpush-client
    3.3.13
public class JpushClientUtils {

    private final static String masterSecret = "xxxxx";
    private final static String appKey = "xxxxx";
    private final static Integer SUCCESS = 200;

    private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);


    //定时发送[-android
    public static String sendScheduleAndroid(Map parm){
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtras(parm)
                                .setTitle(parm.get("title"))  //标题
                                .setAlert(parm.get("content")) //内容
                                .setAlertType(1) //默认提示音
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())//false-开发环境,true-生产环境,android不区分环境
                .build();
        try {
            try {
                ScheduleResult singleSchedule = jPushClient.createSingleSchedule("schedule",parm.get("date"), payload);//时间
                System.out.println(singleSchedule.isResultOK());
                if( singleSchedule.getResponseCode() == SUCCESS){
                    return singleSchedule.getSchedule_id();
                }
            } catch (APIConnectionException e) {
                e.printStackTrace();
            }
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String sendScheduleIOS(Map parm){
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(parm.get("title"),"",parm.get("content")).build())
                                .setBadge(+1)
                                .setSound("happy")
                                .addExtras(parm)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();
        try {
            try {
                ScheduleResult singleSchedule = jPushClient.createSingleSchedule("schedule",parm.get("date"), payload);
                System.out.println(singleSchedule.isResultOK());
                System.out.println(singleSchedule.getSchedule_id());
                if( singleSchedule.getResponseCode() == SUCCESS){
                    return singleSchedule.getSchedule_id();
                }

            } catch (APIConnectionException e) {
                e.printStackTrace();
            }
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String sendScheduleAll(Map parm){
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(parm.get("content"))
                                .setBadge(+1)
                                .setSound("happy")
                                .addExtras(parm)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtras(parm)
                                .setTitle(parm.get("title"))
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(parm.get("title"),"",parm.get("content")).build())
                                .setAlertType(1)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();
        try {
            try {
                ScheduleResult singleSchedule = jPushClient.createSingleSchedule("schedule",parm.get("date"), payload);
                System.out.println(singleSchedule.isResultOK());
                System.out.println(singleSchedule.getSchedule_id());
                if( singleSchedule.getResponseCode() == SUCCESS){
                    return singleSchedule.getSchedule_id();
                }

            } catch (APIConnectionException e) {
                e.printStackTrace();
            }
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    //极光推送>>Android
    public static Long jpushAndroid(Map parm) {

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtras(parm)
                                .setTitle(parm.get("title"))
                                .setAlert(parm.get("content"))
                                .setAlertType(1)
                                .build())
                        .build())
                .setMessage(Message.content(parm.get("content")))
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();

        try {
            PushResult pu = jPushClient.sendPush(payload);
            if( pu.getResponseCode() == SUCCESS){
                return pu.msg_id;
            }

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    //极光推送>>ios
    public static  Long jpushIOS(Map parm) {

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(parm.get("title"),"",parm.get("content")).build())
                                .setBadge(+1)
                                .setSound("happy")
                                .addExtras(parm)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();

        try {
            PushResult pu = jPushClient.sendPush(payload);
            if( pu.getResponseCode() == SUCCESS){
                return pu.msg_id;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    //极光推送>>All所有平台
    public static Long jpushAll(Map parm) {

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(parm.get("title"),"",parm.get("content")).build())
                                .setBadge(+1)
                                .setSound("happy")
                                .addExtras(parm)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtras(parm)
                                .setTitle(parm.get("title"))
                                .setAlert(parm.get("content"))
                                .setAlertType(1)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();
        try {
            PushResult pu = jPushClient.sendPush(payload);
            if( pu.getResponseCode() == SUCCESS){
                return pu.msg_id;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  Long jpushIOStest(Map parm) {

        JsonObject object = new JsonObject();
        object.addProperty("title","ceshi title");
        object.addProperty("body","ceshi body");
        JSONObject jsonObject = new JSONObject();
        IosAlert.Builder builder = new IosAlert.Builder();
        builder.setTitleAndBody("title111","subtitle","body111");
        //创建JPushClient
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(object)
//                                .setAlert(IosAlert.newBuilder()
//                                        .setTitleAndBody("title","subtitle","neirong").build())
                                .setBadge(+1)
                                .setSound("happy")
//                                .addExtras(parm)
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();

        try {
            PushResult pu = jPushClient.sendPush(payload);
            if( pu.getResponseCode() == SUCCESS){
                return pu.msg_id;
            }
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return null;
    }

   
}

集成ios推送标题+内容需要注意的地方:

IosNotification.newBuilder() .setAlert(object)   Alert可以是string、object,需要注意一下JsonObject是gson而不是JSONObject;

别忘了build()初始化相应的对象

 

 

你可能感兴趣的:(jpush)