pom.xml引入极光的SDK:
看自己需要引入那个版本的SDK。
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.4</version> </dependency>
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.6.Final</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency>
核心代码:
public class PushService { private static final Logger LOG= LoggerFactory.getLogger(PushService.class); private static final String masterSecret="自己去申请一个"; private static final String appKey="自己去申请一个"; /** * 所有用户 * @param alert * @return */ public static PushPayload buildPushObject_all_all_alert(String alert) { return PushPayload.alertAll(alert); } /** * 所有平台,推送目标为registrationID * @param alert 内容 * @param extras 附加选项 * @param registrationID * @return */ private static PushPayload buildPushObject_all_alias_alert(String alert, Map<String,String> extras, List<String> registrationID) { return PushPayload.newBuilder() .setPlatform(Platform.android_ios())//构建平台:所有平台:安卓、ios、WindowsPhone .setAudience(Audience.registrationId(registrationID))//构建推送目标 .setNotification( Notification .newBuilder() .setAlert(alert) .addPlatformNotification( AndroidNotification.newBuilder().addExtras(extras).build()) .addPlatformNotification( IosNotification.newBuilder().addExtras(extras).build()) .build() ) .setOptions(Options.newBuilder().setApnsProduction(true).build())//是否是正式环境,默认开发环境 .build(); } /** * * @param content 消息內容 * @param extras 附加选项 * @param registrationID */ public static void SendWithRegistrationID(String content,Map<String,String> extras, List<String> registrationID) { // ClientConfig clientConfig = ClientConfig.getInstance(); // JPushClient jpushClient = new JPushClient(masterSecret,appKey, null, clientConfig); JPushClient jpushClient = new JPushClient(masterSecret,appKey); try { PushPayload payload=buildPushObject_all_alias_alert(content,extras,registrationID); payload.resetOptionsTimeToLive(86400*2L);//离线状态下保存时间 PushResult result = jpushClient.sendPush(payload); //SDK版本如果使用3.2.15以上的话需要在请求结束后close进程:jpushClient.close(); } catch (APIConnectionException e) { e.printStackTrace(); LOG.error("Connection error. Should retry later. {}", e); } catch (APIRequestException e) { e.printStackTrace(); } } //模拟调用 public static void main(String[] args) { List<String> id=new ArrayList<>();//极光的唯一RegistrationID id.add("11111111"); id.add("22222222"); SendWithRegistrationID("测试发送",new HashMap<>(),id); }
需要注意:
如果使用 NettyHttpClient(v3.2.15 版本新增),需要在响应返回后手动调用一下 NettyHttpClient 中的 close 方法,否则进程不会退出。代码示例:
...
try {
PushResult result = jpushClient.sendPush(payload);
// 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。
jpushClient.close();
} catch(InterruptedException e) {
e.printStackTrace();
}