2、创建JPushManager类,用于创建推送主体
@Component
public class JPushManager {
@Autowired
private JPushParam param;
/**
* 发送给所有Android用户
*/
public PushResult sendToAndroid(String content, String title, Map extras) {
JPushClient pushClient = new JPushClient(param.getMaster_secret(), param.getApp_key());
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.all())
.setNotification(Notification.android(content, title, extras))
.build();
try {
return pushClient.sendPush(payload);
} catch (APIConnectionException | APIRequestException e) {
e.printStackTrace();
}
return null;
}
/**
* 发送给所有Ios用户
*/
public PushResult sendToIos(Object alert, Map extras) {
JPushClient pushClient = new JPushClient(param.getMaster_secret(), param.getApp_key());
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.all())
.setNotification(Notification.ios(alert, extras))
.build();
try {
return pushClient.sendPush(payload);
} catch (APIConnectionException | APIRequestException e) {
e.printStackTrace();
}
return null;
}
/**
* 发送给所有用户
*/
public PushResult sendToAll(Object alert) {
JPushClient pushClient = new JPushClient(param.getMaster_secret(), param.getApp_key());
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.all())
.setNotification(Notification.alert(alert))
.build();
try {
return pushClient.sendPush(payload);
} catch (APIConnectionException | APIRequestException e) {
e.printStackTrace();
}
return null;
}
}