首先需要在极光推送官网:推送控制台创建AppKey和Master Secret(包名随便写一个会自动生成测试包),并下载对应的APP安装包测试Java代码能否正确推送
集成maven依赖
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.9</version>
</dependency>
由于官方也没有将Java对应API整理好文档,故为方便以后使用特此整理如下
功能 | 方法 | ||
Push API | 推送 | sendPush(PushPayload pushPayload) | |
推送校验 | sendPushValidate(PushPayload payload) | ||
推送唯一标识符 | getCidList(int count, String type) | ||
Report API | 送达统计 | getReportReceiveds(String msgIds) | |
送达状态查询 | getMessageStatus(CheckMessagePayload payload) | ||
消息统计(vip可用) | getReportMessages(String msgIds) | ||
用户统计(vip可用) | getReportUsers(TimeUnit timeUnit, String start, int duration) | ||
Device API | 查询设备的别名与标签 | getDeviceTagAlias(String registrationId) | |
设置设备的别名与标签 | updateDeviceTagAlias(String registrationId, String alias,Set<String> tagsToAdd, Set<String> tagsToRemove) | ||
清楚所有别名和标签 | updateDeviceTagAlias(String registrationId, boolean clearAlias, boolean clearTag) | ||
查询别名 | getAliasDeviceList(String alias,String platform) | ||
删除别名 | deleteAlias(String alias, String platform) | ||
查询标签列表 | getTagList() | ||
更新标签 | addRemoveDevicesFromTag(String theTag,Set<String> toAddUsers, Set<String> toRemoveUsers) | ||
删除标签 | deleteTag(String theTag, String platform) | ||
判断设备与标签绑定关系 | isDeviceInTag(String theTag, String registrationID) | ||
获取用户在线状态(vip可用) | getUserOnlineStatus(String... registrationIds) |
demo代码
package com.jpush;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.connection.HttpProxy;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
public class Jpush {
protected static final Logger LOG = LoggerFactory.getLogger(Jpush.class);
protected static final String APP_KEY = "3c3aac234abd0d66cba05ed7";
protected static final String MASTER_SECRET = "1a0a7a22ac56b272d86b4452";
protected ClientConfig clientConfig = ClientConfig.getInstance();
protected HttpProxy proxy = null;
protected JPushClient jPushClient = null;
public static void main(String[] args) {
Jpush jpush = new Jpush();
jpush.push("消息标题", "哈哈哈", "", Audience.all(), new HashMap<String, String>());
}
public JPushClient getJPushClient(){
//代理设置
proxy = new HttpProxy("192.168.100.36", 8088);
clientConfig = ClientConfig.getInstance();
jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, proxy, clientConfig);
return jPushClient;
}
public PushResult push(String title, String message, String extras,
Audience audience, Map<String, String> map) {
PushResult result = null;
try {
result = getJPushClient().sendPushValidate(buildPushObject_android_and_ios(
title, message, extras, audience, map));
System.out.println(result);
return result;
} catch (APIConnectionException e) {
LOG.error(e.getMessage(), e);
} catch (APIRequestException e) {
LOG.error(e.getMessage(), e);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return result;
}
/**
*
* @Description:
* @param title 标题
* @param message 消息内容
* @param extras 额外字段
* @param audience 推送设备指定
* @param map 自定义消息map集合
* @return
*/
public PushPayload buildPushObject_android_and_ios(String title,
String message, String extras, Audience audience,
Map<String, String> map) {
return PushPayload
.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(audience)
.setNotification(
Notification
.newBuilder()
.addPlatformNotification(
AndroidNotification.newBuilder()
.setTitle(title)
.setAlert(message)
.addExtra("extras", extras)
.build())
.addPlatformNotification(
IosNotification.newBuilder()
.setAlert(message)
.setMutableContent(true)
.addExtra("extras", extras)
.build()).build())
.setMessage(
Message.newBuilder().setMsgContent("自定义消息内容")
.setTitle("自定义消息标题").addExtras(map)
.build())
.setOptions(Options.newBuilder().setApnsProduction(false)// 仅仅对于IOS生效.false开发环境
.build())
.build();
}
}