极光官方java sdk文档
http://www.jianshu.com/p/e1f6335d40f0
1.注册
极光官网上注册一个app,会生成appKey和MasterSecret,代码中声明JPushClient类需用到。
2.maven工程下导入依赖
直接在pom文件中添加<code>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.15</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></code>
log包可以不dependency,根据自身项目需要。
3.项目编译
4.扣代码
/**
* Created by yang on 2017/2/27.
*/
public class JpushUtils {
private static final String APP_KEY = "a8e8f82096aba5758f32776b";
private static final String MASTER_SECRET = "d311c59747521472501e25b7";
public static PushResult sendJpushMsg(String msg, List<String> aliasList, String type, String platform, String orderId) {
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
Map<String, String> extras = new HashMap<String, String>();
// 添加信息:msg 消息内容 type:1,订单推送 2,系统推送
extras.put("type", type);
extras.put("orderId", orderId + "");
if (platform.equals("android")) {
PushPayload payload = buildPushObject_android_alias_alert(msg, extras, aliasList);
try {
PushResult result = jpushClient.sendPush(payload);
return result;
} catch (Exception e) {
e.printStackTrace();
}
} else if (platform.equals("ios")) {
PushPayload payload = buildPushObject_ios_alias_alert(msg, extras, aliasList);
try {
PushResult result = jpushClient.sendPush(payload);
return result;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 透传android,由android自行设计弹出框格式
*/
public static PushPayload buildPushObject_android_alias_alert(String msg, Map<String, String> extras, List<String> aliasList) {
return PushPayload.newBuilder()
.setPlatform(Platform.android())//必填 Platform类可设置推送的平台设备,android、ios、winphone或all;
.setMessage(Message.newBuilder().setMsgContent(msg).addExtras(extras).build())////设置推送方式为透传
.setAudience(Audience.alias(aliasList))//设置推送平台目标别名
.build();
}
/**
* 推送ios
*/
public static PushPayload buildPushObject_ios_alias_alert(String msg, Map<String, String> extras, List<String> aliasList) {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())//必填 Platform类可设置推送的平台设备,android、ios、winphone;
.setNotification(Notification.newBuilder()
.setAlert(msg)
.addPlatformNotification(IosNotification.newBuilder().addExtras(extras).build())
.build())//设置推送方式为推送
.setAudience(Audience.alias(aliasList))//设置推送平台目标别名
//.setOptions(Options.newBuilder().setApnsProduction(false).build())
//通过setApnsProduction来制定ios推送环境True 表示推送生产环境,False 表示要推送开发环境; 如 果不指定则为推送生产环境
.build();
}
}```
###5.测试
```java
/**
* Created by yang on 2017/2/28.
*/
public class JpushTest {
public static void main(String[] args){
List<String> aliasList = new ArrayList<>();
aliasList.add("12");
PushResult rs = JpushUtils.sendJpushMsg("你好",aliasList,"1","ios",null);
}
}
根据alias设置别名,此处的别名需要与app端init jpush注册的别名一致。
或者通过极光web界面网页推送
GG...