概述
引入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()初始化相应的对象
最后
以上就是善良豌豆为你收集整理的jpush推送通知ios、android的全部内容,希望文章能够帮你解决jpush推送通知ios、android所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复