因为暂时开发自有对单一用户不同设备进行信息推送。
因此就简单实现了两个接口,可能后期项目完善会增加一些其他接口也不会太难。
极光推送已经把接口编写的简单至极,一个简单的例子就可以实现。但是会有很多参数有他不同的含义,可以参照:极光推送接口文档
接口代码:
public interface PushPortal {
/** * 推送所有平台所有用户 * @return */
public boolean pushAllAllAlert(String alert , Map<String, String> extras);
/** * 推送所有平台所有单一用户 * @return */
public boolean pushAllAliasAlert(String alert,List<String> aliasList,Map<String, String> extras);
}
实现代码:
@Service("pushPortal")
public class PushPortalImpl implements PushPortal{
private final String MASTER_SECRET="e6a135cb55a7292******";
//JPush服务器端下发的master_key
private final String APP_KEY="284403bc5d3b2*******";
//JPush服务器端下发的app_key
private final boolean iosflat =false;
//ios转发平台环境 True 表示推送生产环境,False 表示要推送开发环境 默认是开发
long timeToLive = 86400L; //信息保留时间
JPushClient jpushClient = new JPushClient(MASTER_SECRET,APP_KEY, null, ClientConfig.getInstance());
@Override
public boolean pushAllAllAlert(String alert , Map<String, String> extras) {
//组件传输参数
PushPayload payload = PushPayload
.newBuilder()
.setPlatform(Platform.all()) //指定平台
.setAudience(Audience.all()) //用户指定
.setNotification(
Notification.newBuilder()
.setAlert(alert)
.addPlatformNotification(
AndroidNotification.newBuilder().addExtras(extras).build())
.addPlatformNotification(
IosNotification.newBuilder().addExtras(extras).build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(iosflat).setTimeToLive(timeToLive).build()).build();
//上面设置ios平台环境 True 表示推送生产环境,False 表示要推送开发环境 默认是开发
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println("Got result - " + result);
return true;
} catch (APIConnectionException e) {
// Connection error, should retry later
System.out.print("Connection error, should retry later "+e);
return false;
} catch (APIRequestException e) {
// Should review the error, and fix the request
System.out.println("根据返回的错误信息核查请求是否正确"+e);
System.out.println("HTTP 状态信息码: " + e.getStatus());
System.out.println("JPush返回的错误码: " + e.getErrorCode());
System.out.println("JPush返回的错误信息: " + e.getErrorMessage());
return false;
}
}
@Override
public boolean pushAllAliasAlert(String alert,List<String> aliasList,Map<String, String> extras) {
//
PushPayload payload = PushPayload
.newBuilder()
.setPlatform(Platform.all()) //指定平台
.setAudience(Audience.alias(aliasList)) //用户指定
.setNotification(
Notification.newBuilder()
.setAlert(alert)
.addPlatformNotification(
AndroidNotification.newBuilder().addExtras(extras).build())
.addPlatformNotification(
IosNotification.newBuilder().addExtras(extras).build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(iosflat).setTimeToLive(timeToLive).build()).build();
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println("Got result - " + result);
return true;
} catch (APIConnectionException e) {
// Connection error, should retry later
System.out.print("Connection error, should retry later "+e);
return false;
} catch (APIRequestException e) {
// Should review the error, and fix the request
System.out.println("根据返回的错误信息核查请求是否正确"+e);
System.out.println("HTTP 状态信息码: " + e.getStatus());
System.out.println("JPush返回的错误码: " + e.getErrorCode());
System.out.println("JPush返回的错误信息: " + e.getErrorMessage());
return false;
}
}
}
添加注入方便调用。
博客主要是提供参考,具体参数还是要看光放文档。