pom.xml增加如下依赖:
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.6.6</version>
</dependency>
JPushUtil工具类如下:
@Slf4j
public class JPushUtil {
protected static final Logger LOG = LoggerFactory.getLogger(JPushUtil.class);
/** 设置好账号的app_key和masterSecret是必须的
用自己创建以用后的APP_KEY和MASTER_SECRET
替换即可,后期可写到yml配置文件中*/
private static String APP_KEY = "xxxxxxxxxxxxxxxx";
private static String MASTER_SECRET = "xxxxxxxxxxxxxxxxxxx";
//根据别名推送消息,把房间号也推过去
public static void pushMessageByAlias(List<String> aliases, CallRoomJPush callRoomJPush){
ClientConfig clientConfig = ClientConfig.getInstance();
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
// String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
// Call setHttpClient to set httpClient,
// If you don't invoke this method, default httpClient will use NativeHttpClient.
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
// jpushClient.getPushClient().setHttpClient(httpClient);
// For push, all you need do is to build PushPayload object.
String pushContentJson=new Gson().toJson(callRoomJPush);
final PushPayload payload = buildPushMessage_android_and_ios(aliases,pushContentJson);
// PushPayload payload = buildPushObject_all_alias_alert();
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
System.out.println(result);
// 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
// If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
// jpushClient.close();
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
LOG.error("Sendno: " + payload.getSendno());
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
LOG.error("Sendno: " + payload.getSendno());
}
}
/**
* Could modify the contents for pushing
* The comments are showing how to use it
* @return
*/
public static PushPayload buildPushMessage_android_and_ios(List<String> aliases,String pushContentJson) {
Map<String, String> extras = new HashMap<String, String>();
extras.put("test", "https://community.jiguang.cn/push");
// you can set anything you want in this builder, read the document to avoid collision.
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(aliases))
.setMessage(Message.newBuilder()
.setMsgContent(pushContentJson)
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setTimeToLive(43200)
.build())
.build();
}
/**
* Could modify the contents for pushing
* The comments are showing how to use it
* @return
*/
public static PushPayload buildPushObject_android_and_ios() {
Map<String, String> extras = new HashMap<String, String>();
extras.put("test", "https://community.jiguang.cn/push");
// you can set anything you want in this builder, read the document to avoid collision.
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.all())
// .setMessage(Message.newBuilder()
// .setMsgContent("Hi, JPush")
// .build())
.setNotification(Notification.newBuilder()
.setAlert("testing alert content")
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle("Android Title")
.addExtras(extras).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra("extra_key", "extra_value").build())
.build())
// .setSMS(SMS.newBuilder()
// .setDelayTime(1000)
// .setTempID(2000)
// .addPara("Test", 1)
// .setActiveFilter(true)
// .build())
// .setNotification3rd(Notification3rd.newBuilder()
// .setContent("Hi, JPush")
// .setTitle("msg testing")
// .setChannelId("channel1001")
// .setUriActivity("cn.jpush.android.ui.OpenClickActivity")
// .setUriAction("cn.jpush.android.intent.CONNECTION")
// .setBadgeAddNum(1)
// .setBadgeClass("com.test.badge.MainActivity")
// .setSound("sound")
// .addExtra("news_id", 124)
// .addExtra("my_key", "a value")
// .build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setTimeToLive(43200)
.build())
.build();
}
List类型的aliases是别名的集合,我们的项目里面把userId设置为了别名,所以aliases这个集合里面,存储的是要推送给哪些人的userId集合,而callRoomJPush是一个实体类,要往客户端传递的消息,这个根据自己的业务需要,去定义字段,最终客户端收到的消息里面的message字段,就是推送传过去的数据。