package jpush;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
public class JPushServer {
//在极光注册上传应用的 appKey 和 masterSecret
private static final String appKey ="7df35835f1b0243b4c8c7a31";必填,例如466f7032ac604e02fb7bda89
private static final String masterSecret = "23eace451b8666811ab9903f";//必填,每个应用都对应一个masterSecret
private static final String ALERT = "测试极光推送消息";
public static void main(String[] args) {
JPushServer js = new JPushServer();
//js.alertAll("所有平台的所有推送消息qqqqqqqq");
//js.alertAllAlias("user_125","别名为user_125的测试消息");
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushPayload payload =JPushServer.buildPushObject_android_and_ios("aaa","aaa","aaa","aaa");
try {
PushResult pushResult = jpushClient.sendPush(payload);
jpushClient.close();
} catch (APIConnectionException e) {
jpushClient.close();
e.printStackTrace();
} catch (APIRequestException e) {
jpushClient.close();
e.printStackTrace();
}
}
/*
* 所有平台所有设备
*/
public PushResult alertAll(String message){
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushPayload payload = PushPayload.alertAll(message);
try {
PushResult pushResult = jpushClient.sendPush(payload);
jpushClient.close();
return pushResult;
} catch (APIConnectionException e) {
jpushClient.close();
e.printStackTrace();
} catch (APIRequestException e) {
jpushClient.close();
e.printStackTrace();
}
return null;
}
/*
* 所有平台,推送目标是别名为 alias,通知内容为 message
*/
public PushResult alertAllAlias(String alias,String message) {
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
// PushPayload payload = PushPayload.newBuilder()
// .setPlatform(Platform.all())
// .setAudience(Audience.alias(alias))
// .setNotification(Notification.alert(message))
// .build();
PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(ALERT)
.setSound("default")
.addExtra("from" ,"JPush")
.build())
.build())
.setMessage(Message.content(message))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
// try {
// PushResult pushResult = jpushClient.sendPush(payload);
// jpushClient.close();
// return pushResult;
// } catch (APIConnectionException e) {
// jpushClient.close();
// e.printStackTrace();
// } catch (APIRequestException e) {
// jpushClient.close();
// e.printStackTrace();
// }
return null;
}
/*
* 所有平台,推送目标是tag,通知内容为 message
*/
public PushResult alertAllTag(String tag,String message) {
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.tag(tag))
.setNotification(Notification.alert(message))
.build();
try {
PushResult pushResult = jpushClient.sendPush(payload);
jpushClient.close();
return pushResult;
} catch (APIConnectionException e) {
jpushClient.close();
e.printStackTrace();
} catch (APIRequestException e) {
jpushClient.close();
e.printStackTrace();
}
return null;
}
public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias("user125"))
.setNotification(Notification.newBuilder()
.setAlert(notification_title)
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
//.addExtra("androidNotification extras key",extrasparam)
.build()
)
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
//直接传alert
//此项是指定此推送的badge自动加1
.incrBadge(1)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
//.addExtra("iosNotification extras key",extrasparam)
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// .setContentAvailable(true)
.build()
)
.build()
)
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
//.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build()
)
.build();
}
}
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
public class JPushServer {
//在极光注册上传应用的 appKey 和 masterSecret
private static final String appKey ="7df35835f1b0243b4c8c7a31";必填,例如466f7032ac604e02fb7bda89
private static final String masterSecret = "23eace451b8666811ab9903f";//必填,每个应用都对应一个masterSecret
private static final String ALERT = "测试极光推送消息";
public static void main(String[] args) {
JPushServer js = new JPushServer();
//js.alertAll("所有平台的所有推送消息qqqqqqqq");
//js.alertAllAlias("user_125","别名为user_125的测试消息");
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushPayload payload =JPushServer.buildPushObject_android_and_ios("aaa","aaa","aaa","aaa");
try {
PushResult pushResult = jpushClient.sendPush(payload);
jpushClient.close();
} catch (APIConnectionException e) {
jpushClient.close();
e.printStackTrace();
} catch (APIRequestException e) {
jpushClient.close();
e.printStackTrace();
}
}
/*
* 所有平台所有设备
*/
public PushResult alertAll(String message){
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushPayload payload = PushPayload.alertAll(message);
try {
PushResult pushResult = jpushClient.sendPush(payload);
jpushClient.close();
return pushResult;
} catch (APIConnectionException e) {
jpushClient.close();
e.printStackTrace();
} catch (APIRequestException e) {
jpushClient.close();
e.printStackTrace();
}
return null;
}
/*
* 所有平台,推送目标是别名为 alias,通知内容为 message
*/
public PushResult alertAllAlias(String alias,String message) {
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
// PushPayload payload = PushPayload.newBuilder()
// .setPlatform(Platform.all())
// .setAudience(Audience.alias(alias))
// .setNotification(Notification.alert(message))
// .build();
PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(ALERT)
.setSound("default")
.addExtra("from" ,"JPush")
.build())
.build())
.setMessage(Message.content(message))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
// try {
// PushResult pushResult = jpushClient.sendPush(payload);
// jpushClient.close();
// return pushResult;
// } catch (APIConnectionException e) {
// jpushClient.close();
// e.printStackTrace();
// } catch (APIRequestException e) {
// jpushClient.close();
// e.printStackTrace();
// }
return null;
}
/*
* 所有平台,推送目标是tag,通知内容为 message
*/
public PushResult alertAllTag(String tag,String message) {
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.tag(tag))
.setNotification(Notification.alert(message))
.build();
try {
PushResult pushResult = jpushClient.sendPush(payload);
jpushClient.close();
return pushResult;
} catch (APIConnectionException e) {
jpushClient.close();
e.printStackTrace();
} catch (APIRequestException e) {
jpushClient.close();
e.printStackTrace();
}
return null;
}
public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias("user125"))
.setNotification(Notification.newBuilder()
.setAlert(notification_title)
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notification_title)
.setTitle(notification_title)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
//.addExtra("androidNotification extras key",extrasparam)
.build()
)
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(notification_title)
//直接传alert
//此项是指定此推送的badge自动加1
.incrBadge(1)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
//.addExtra("iosNotification extras key",extrasparam)
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
// .setContentAvailable(true)
.build()
)
.build()
)
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(msg_title)
//.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build()
)
.build();
}
}