jpush推送通知ios、android

 

引入jpush sdk

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.3.13</version>
</dependency>
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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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()初始化相應的對象

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章