【Java工具】之极光推送(九)
2019-06-13 本文已影响15人
3d0829501918
为了以后方便,特把极光推送方法记录下来。
- 1、maven依赖
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.17</version>
</dependency>
-
2、注册极光,获取密钥
极光地址:https://www.jiguang.cn/
极光Java API:https://docs.jiguang.cn/jpush/server/sdk/java_sdk/
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);
private static final String appKey ="你的appKey";
private static final String masterSecret = "你的masterSecret";
private static final ClientConfig clientConfig = ClientConfig.getInstance();
private static final JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
-
3.推送全部用户
推送平台是全部,角标数字为 5,通知声音为默认 "default",并且附加字段 from = "JPush"、content和message。消息内容是 message;ApnsProduction为true代表是生成环境,false是测试环境。
/**
* 推送全部用户
* @param notification
* @param message
*/
public static boolean pushAll(String notification, String message) {
boolean b = false;
PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder().setAlert(notification).setSound("default")
.setBadge(5)
.setContentAvailable(true).addExtra("from", "JPush").addExtra("content", message)
.build())
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification).build())
.build())
.setMessage(Message.content(message)).setOptions(Options.newBuilder().setApnsProduction(false).build())
.build();
try {
PushResult result = jpushClient.sendPush(payload);
b = true;
} catch (APIConnectionException e) {
} catch (APIRequestException e) {
}
return b;
}
- 4、根据别名进行推送
/**
* 根据别名进行推送
* @param notification
* @param message .setContentAvailable(true)
*/
public static boolean pushSelected(String alias[], String notification, String message) {
boolean b = false;
PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder().setAlert(notification).setSound("default")
.setBadge(5)
.addExtra("1", "http://tms.tikinmedia.com/upload/deviceVersion")
.build())
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification).build())
.build())
//.setMessage(Message.content(message))
.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println(result);
b = true;
} catch (APIConnectionException e) {
} catch (APIRequestException e) {
}
return b;
}
- 5、添加自定义附加字段
/**
* 安卓与ios平台,通过别名推送
* @param alias 别名
* @param content 推送内容
* @param key 拓展字段key值
* @param value 拓展字段value
* @return
*/
public static boolean buildPushObject_all_alias_alert(String[] alias, String content,String key,String value) {
boolean b = false;
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.setAlert(content)
.addPlatformNotification(AndroidNotification.newBuilder().addExtra(key,value)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.setBadge(5)
.setSound("default").addExtra(key,value)
.build())
.build())
.build();
try {
PushResult pushResult = jpushClient.sendPush(payload);
//解析数据,0表示成功
//{"msg_id":3828060948763255,"sendno":180326298,"statusCode":0}
System.out.println(pushResult);
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return b;
}
- 6、结语
迈开脚步,再长的路也不在话下;停滞不前,再短的路也难以到达。