极光推送的java实现
第二导入
@PropertySource("classpath:config/properties/jpush.properties")
@Component
public class JpushConfig {
@Value(value="${appKey}")
public String appKey;
@Value(value="${appSecret}")
public String appSecret;
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
}
第三就是实现
@Component
public class JpushService {
private static Logger logger = LoggerFactory.getLogger(JpushService.class);
@Autowired
private JpushConfig jpushConfig;
public void sendMsg(long userId,String message) {
ClientConfig clientConfig = ClientConfig.getInstance();
String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
final NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(jpushConfig.getAppKey(), jpushConfig.getAppSecret()), null, clientConfig);
try {
URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(String.valueOf(userId))).setNotification(Notification.alert(message)).build();
client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
@Override
public void onSucceed(ResponseWrapper responseWrapper) {
logger.debug("userid: {},sendmsg: {},Jpush result: {}" ,userId, message,responseWrapper.responseContent);
}
});
} catch (URISyntaxException e) {
logger.info("极光推送失败:", e);
}finally {
client.close();
}
}
public void sendNewMsgNotice(long userId) {
String message = "您有一条新信息!";
this.sendMsg(userId, message );
}
}
第四调用