网上找的都说的不是太清楚,自己半熟半生的做,做完事后现在总结一下,下面的代码就是向单个指定的手机推送信息,唯一标识就是那个 registrationId,这个是前端提供的,不用纠结,还是就是master_secret, app_key,这两个参数是官网注册应用了,极光提供的,.setAudience(Audience.registrationId("registrationId前端给"))这行参数很关键,可以去官网api看,不同的参数是不同的用户,.setPlatform(Platform.all())这个参数也很关键,不同的参数是不同的平台,msg就是推送显示的主题信息,我觉得已经很清楚了,有什么不懂的欢迎留言,还有就是如果发送请求的时候报空指针,就是参数传的不对
public class JGTS {
public static void main(String[] args) {
//设置推送参数
//这里同学们就可以自定义推送参数了
Map<String, String> parm = new HashMap<String, String>();
//设置提示信息,内容是文章标题
parm.put("msg","测试测试,收到请联系发送人");
jpushAll(parm);
}
//极光推送>>All所有平台
public static void jpushAll(Map<String, String> parm) {
String master_secret = "";
String app_key = "";
//创建JPushClient
JPushClient jpushClient = new JPushClient(master_secret, app_key);
//创建option
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all()) //所有平台的用户
.setAudience(Audience.registrationId("registrationId前端给"))//registrationId指定用户
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder() //发送ios
.setAlert(parm.get("msg")) //消息体
.setBadge(+1)
.setSound("happy") //ios提示音
.addExtras(parm) //附加参数
.build())
// .addPlatformNotification(AndroidNotification.newBuilder() //发送android
// .addExtras(parm) //附加参数
// .setAlert(parm.get("msg")) //消息体
// .build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
System.out.println(pu.toString());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
}