极光推送
极光推送:主要用于APP实时获取最新消息。本文主要描述如何使用极光提供的SDK进行推送。
极光推送中主要需要配置的参数如下:
推送平台:JPush 当前支持 Android, iOS, Windows Phone 三个平台的推送。其关键字分别为:"android", "ios", "winphone"。
推送目标:广播(所有用户)、alias(别名:用别名来标识一个用户)、tag(标签:用标签来进行大规模的设备属性、用户属性分群。 一次推送最多 20 个)等。
推送方式:通知(展示在通知栏)、自定义消息(此部分内容不会展示到通知栏上,JPush SDK 收到消息内容后透传给 App。需要 App 自行处理。应用举例:弹出页面)等。
首先需要在项目的pom文件中引入jar包:
1 <dependency> 2 <groupId>cn.jpush.api</groupId> 3 <artifactId>jpush-client</artifactId> 4 <version>3.3.3</version> 5 </dependency>
应用就比较简单了:
1 //声明推送目标 ,在极光平台创建应用,就会生成唯一的AppKey、Master Secret 2 3 JPushClient jpushClient = new JPushClient(GlobleConfig.getProperty("JPush_masterSecret"), GlobleConfig.getProperty("JPush_appKey")); 4 5 //创建推送对象,举例:根据别名推送通知,具体方法实现在下边的工具类中 6 7 PushPayload pushPayload = JpushUtil.buildPushObject_all_aliases_alertWithTitle(alias, "退出登录", "您的账号已在其他设备登录!",map); 8 9 //进行推送 10 11 try { 12 PushResult pushResult = jpushClient.sendPush(pushPayload); 13 System.out.println(pushResult+"......."); 14 } catch (Exception e) { 15 //如果推送错误返回的code为1011,就是没有找到推送目标,则需要App配合排查问题 16 System.out.println("推送失败"); 17 }
主要的工具类 JpushUtil.java:
(我设置的推送目标是别名,也可以根据需求改成使用签名等。)
1 JPushUtil.java 工具类 2 3 import java.util.Dictionary; 4 import java.util.HashMap; 5 import java.util.Hashtable; 6 import java.util.List; 7 import java.util.Map; 8 9 import com.erenju.util.GlobleConfig; 10 import com.google.gson.Gson; 11 import com.google.gson.GsonBuilder; 12 import com.xhgx.domain.RestInfo; 13 import com.xhgx.web.util.JSONObject; 14 15 import cn.jiguang.common.resp.APIConnectionException; 16 import cn.jiguang.common.resp.APIRequestException; 17 import cn.jpush.api.JPushClient; 18 import cn.jpush.api.push.PushResult; 19 import cn.jpush.api.push.model.Message; 20 import cn.jpush.api.push.model.Options; 21 import cn.jpush.api.push.model.Platform; 22 import cn.jpush.api.push.model.PushPayload; 23 import cn.jpush.api.push.model.audience.Audience; 24 import cn.jpush.api.push.model.notification.AndroidNotification; 25 import cn.jpush.api.push.model.notification.IosAlert; 26 import cn.jpush.api.push.model.notification.IosNotification; 27 import cn.jpush.api.push.model.notification.Notification; 28 29 public class JpushUtil{ 30 31 /** 32 * 推送通知:以广播形式推送给所有平台 33 * @return 34 */ 35 public static PushPayload buildPushObject_all_alias_alert(){ 36 return PushPayload.newBuilder() 37 .setPlatform(Platform.all())//推送平台 :all代表全部 ,也可写具体的平台android或ios 38 .setAudience(Audience.all())//推送目标:all表示以广播形式推送,所有用户都可接收到 39 .setNotification(Notification.alert("测试广播推送!"))//推送到通知栏的内容 40 .setOptions(Options.newBuilder() 41 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 42 .setSendno(1)//推送编号 43 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 44 .build()) 45 .build(); 46 } 47 48 /** 49 * 推送通知:根据alias推送给个人 50 * @param alias 51 * @param notification_title 52 * @param msg_title 53 * @param extrasparam 54 * @return 55 */ 56 public static PushPayload buildPushObject_all_alias_alertWithTitle(String alias,String notification_title, String msg_title, Map<String,String> extrasparam){ 57 String iosAlert = notification_title+":"+msg_title; 58 //需要最新sdk版本 59 // IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build(); 60 //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build() 61 return PushPayload.newBuilder() 62 //推送平台 63 .setPlatform(Platform.all()) 64 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 65 .setAudience(Audience.alias(alias)) 66 //通知 67 .setNotification(Notification.newBuilder() 68 .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容 69 .setTitle(notification_title)//标题 70 .setAlert(msg_title)//内容 71 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 72 //另一种方式: addExtra(key,value) 73 .addExtras(extrasparam) 74 .build()) 75 .addPlatformNotification(IosNotification.newBuilder() 76 .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等 77 .incrBadge(1)//此项是指定此推送的badge自动加1 78 //.setSound("sound.caf")//设置声音 79 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 80 //另一种方式: addExtra(key,value) 81 .addExtras(extrasparam) 82 .setContentAvailable(true)//是否可以在锁屏状态下接收 83 .build()) 84 .build()) 85 //可选参数 86 .setOptions(Options.newBuilder() 87 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 88 .setSendno(1)//推送编号 89 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 90 .build()) 91 .build(); 92 } 93 /** 94 * 推送通知:根据alias同时推送给多个用户 95 * @param aliases 96 * @param notification_title 97 * @param msg_title 98 * @param extrasparam 99 * @return 100 */ 101 public static PushPayload buildPushObject_all_aliases_alertWithTitle(List<String> aliases,String notification_title, String msg_title, Map<String,String> extrasparam){ 102 String iosAlert = notification_title+":"+msg_title; 103 //需要最新sdk版本 104 // IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build(); 105 //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build() 106 return PushPayload.newBuilder() 107 //推送平台 108 .setPlatform(Platform.all()) 109 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 110 .setAudience(Audience.alias(aliases)) 111 //通知 112 .setNotification(Notification.newBuilder() 113 .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容 114 .setTitle(notification_title)//标题 115 .setAlert(msg_title)//内容 116 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 117 //另一种方式: addExtra(key,value) 118 .addExtras(extrasparam) 119 .build()) 120 .addPlatformNotification(IosNotification.newBuilder() 121 .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等 122 .incrBadge(1)//此项是指定此推送的badge自动加1 123 //.setSound("sound.caf")//设置声音 124 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 125 //另一种方式: addExtra(key,value) 126 .addExtras(extrasparam) 127 .setContentAvailable(true)//是否可以在锁屏状态下接收 128 .build()) 129 .build()) 130 //可选参数 131 .setOptions(Options.newBuilder() 132 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 133 .setSendno(1)//推送编号 134 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 135 .build()) 136 .build(); 137 } 138 139 /** 140 * 推送通知和自定义消息:根据alias推给个人 141 * @param alias 142 * @param notification_title 143 * @param msg_title 144 * @param msg_content 145 * @param extrasparam 146 * @return 147 */ 148 public static PushPayload buildPushObject_all_alias_alertAndmessage(String alias,String notification_title,String msg_title,String msg_content,Map<String, String> extrasparam ){ 149 String iosAlert = notification_title+":"+msg_title; 150 //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build() 151 152 return PushPayload.newBuilder() 153 //推送平台 154 .setPlatform(Platform.all()) 155 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 156 .setAudience(Audience.alias(alias)) 157 //通知 158 .setNotification(Notification.newBuilder() 159 .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容 160 .setTitle(notification_title)//标题 161 .setAlert(msg_title)//内容 162 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 163 //另一种方式: addExtra(key,value) 164 .addExtras(extrasparam) 165 .build()) 166 .addPlatformNotification(IosNotification.newBuilder() 167 .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、body、subtitle等 168 .incrBadge(1)//此项是指定此推送的badge自动加1 169 //.setSound("sound.caf")//设置声音 170 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 171 //另一种方式: addExtra(key,value) 172 .addExtras(extrasparam) 173 //.setContentAvailable(true)//是否可以在锁屏状态下接收 174 .build()) 175 .build()) 176 //自定义消息 177 .setMessage(Message.newBuilder() 178 .setTitle(msg_title)//消息标题 179 .setMsgContent(msg_content)//消息内容本身 180 //.setContentType("json")//消息内容类型 181 .addExtras(extrasparam)//json格式的可选参数 182 .build()) 183 //可选参数 184 .setOptions(Options.newBuilder() 185 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 186 .setSendno(1)//推送编号 187 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 188 .build()) 189 .build(); 190 } 191 /** 192 * 推送自定义消息:根据alias推给个人 193 * @param alias 194 * @param msg_title 195 * @param msg_content 196 * @param extrasparam 197 * @return 198 */ 199 public static PushPayload buildPushObject_all_alias_message(String alias,String msg_title,String msg_content,Map<String, String> extrasparam ){ 200 201 return PushPayload.newBuilder() 202 //推送平台 203 .setPlatform(Platform.all()) 204 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 205 .setAudience(Audience.alias(alias)) 206 //自定义消息 207 .setMessage(Message.newBuilder() 208 .setTitle(msg_title)//消息标题 209 .setMsgContent(msg_content)//消息内容本身 210 //.setContentType("json")//消息内容类型 211 .addExtras(extrasparam)//json格式的可选参数 212 .build()) 213 //可选参数 214 .setOptions(Options.newBuilder() 215 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 216 .setSendno(1)//推送编号 217 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 218 .build()) 219 .build(); 220 } 221 /** 222 * 推送通知:根据tag推送给个人 223 * @param tag 224 * @param tag_add 225 * @param notification_title 226 * @param msg_title 227 * @param extrasparam 228 * @return 229 */ 230 @SuppressWarnings("static-access") 231 public static PushPayload buildPushObject_all_tag_alertWithTitle(String tag,String tag_and,String notification_title, String msg_title, Map<String,String> extrasparam){ 232 String iosAlert = notification_title+":"+msg_title; 233 //需要最新sdk版本 234 // IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build(); 235 //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build() 236 return PushPayload.newBuilder() 237 //推送平台 238 .setPlatform(Platform.all()) 239 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 240 .setAudience(Audience.tag(tag).tag_and(tag_and)) 241 //通知 242 .setNotification(Notification.newBuilder() 243 .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容 244 .setTitle(notification_title)//标题 245 .setAlert(msg_title)//内容 246 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 247 //另一种方式: addExtra(key,value) 248 .addExtras(extrasparam) 249 .build()) 250 .addPlatformNotification(IosNotification.newBuilder() 251 .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等 252 .incrBadge(1)//此项是指定此推送的badge自动加1 253 //.setSound("sound.caf")//设置声音 254 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 255 //另一种方式: addExtra(key,value) 256 .addExtras(extrasparam) 257 //.setContentAvailable(true)//是否可以在锁屏状态下接收 258 .build()) 259 .build()) 260 //可选参数 261 .setOptions(Options.newBuilder() 262 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 263 .setSendno(1)//推送编号 264 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 265 .build()) 266 .build(); 267 } 268 /** 269 * 推送通知和自定义消息:根据tag推给个人 270 * @param tag 271 * @param tag_add 272 * @param notification_title 273 * @param msg_title 274 * @param msg_content 275 * @param extrasparam 276 * @return 277 */ 278 @SuppressWarnings("static-access") 279 public static PushPayload buildPushObject_all_tag_alertAndmessage(String tag,String tag_and,String notification_title,String msg_title,String msg_content,Map<String, String> extrasparam ){ 280 String iosAlert = notification_title+":"+msg_title; 281 //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build() 282 283 return PushPayload.newBuilder() 284 //推送平台 285 .setPlatform(Platform.all()) 286 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 287 .setAudience(Audience.tag(tag).tag_and(tag_and)) 288 //通知 289 .setNotification(Notification.newBuilder() 290 .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容 291 .setTitle(notification_title)//标题 292 .setAlert(msg_title)//内容 293 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 294 //另一种方式: addExtra(key,value) 295 .addExtras(extrasparam) 296 .build()) 297 .addPlatformNotification(IosNotification.newBuilder() 298 .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、body、subtitle等 299 .incrBadge(1)//此项是指定此推送的badge自动加1 300 //.setSound("sound.caf")//设置声音 301 //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) 302 //另一种方式: addExtra(key,value) 303 .addExtras(extrasparam) 304 //.setContentAvailable(true)//是否可以在锁屏状态下接收 305 .build()) 306 .build()) 307 //自定义消息 308 .setMessage(Message.newBuilder() 309 .setTitle(msg_title)//消息标题 310 .setMsgContent(msg_content)//消息内容本身 311 //.setContentType("json")//消息内容类型 312 .addExtras(extrasparam)//json格式的可选参数 313 .build()) 314 //可选参数 315 .setOptions(Options.newBuilder() 316 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 317 .setSendno(1)//推送编号 318 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 319 .build()) 320 .build(); 321 } 322 /** 323 * 推送自定义消息:根据tag推给个人 324 * @param tag 325 * @param msg_title 326 * @param msg_content 327 * @param extrasparam 328 * @return 329 */ 330 public static PushPayload buildPushObject_all_tags_message(String[] tag,String msg_title,String msg_content,Map<String, String> extrasparam ){ 331 332 return PushPayload.newBuilder() 333 //推送平台 334 .setPlatform(Platform.all()) 335 //推送目标:all、tag、tag_and、tag_not、alias、registration_id等 336 .setAudience(Audience.tag(tag)) 337 //自定义消息 338 .setMessage(Message.newBuilder() 339 .setTitle(msg_title)//消息标题 340 .setMsgContent(msg_content)//消息内容本身 341 //.setContentType("json")//消息内容类型 342 .addExtras(extrasparam)//json格式的可选参数 343 .build()) 344 //可选参数 345 .setOptions(Options.newBuilder() 346 .setApnsProduction(true)//APNs是否为生产环境,false为开发环境 347 .setSendno(1)//推送编号 348 .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天 349 .build()) 350 .build(); 351 } 352 353 } 354
声明: 生产环境 : 打包后安装的应用 走的是生产环境。 开发环境 : 连接真机测试时 应用走的是开发环境。
(只有ios需要配置环境,安卓不需要)
可能有描述不清晰的地方,可以参考极光平台提供的API文档:https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7


浙公网安备 33010602011771号