**极光推送,用官方jar包或者maven集成到框架中,下面分别写了三个方法,给所有用户推送,给tag用户推送,给Tel用户推送
**“`
public class JpushUtil {
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtil.class);
private static final String appKey =”1f0233c8******08e26c382”;
private static final String masterSecret = “c531a*********ce854c3a”;
public int sendToAll(String notificationTitle,String notificationContent, String msgTitle, String msgContent, Map<String, String> extras) {
int result = 0;
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload =JpushUtil.buildPushObject_android_and_ios(notificationTitle,notificationContent, msgTitle, msgContent, extras);
try {
PushResult pushResult = jpushClient.sendPush(payload);
System.out.println(pushResult.msg_id);
System.out.println(pushResult.sendno);
if(pushResult.getResponseCode()==200){
result=1;
}
LOG.info("Got result - " + pushResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
return result;
}
public int sendToTel(String tel,String notificationTitle,String notificationContent, String msgTitle, String msgContent, Map<String, String> extras) {
int result=0;
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload = JpushUtil.buildPushObject_all_registrationTel_alertWithTitle(tel, notificationTitle,notificationContent, msgTitle, msgContent, extras);
try {
PushResult pushResult = jpushClient.sendPush(payload);
System.out.println(pushResult.msg_id);
System.out.println(pushResult.sendno);
if(pushResult.getResponseCode()==200){
result=1;
}
LOG.info("Got result - " + pushResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
return result;
}
public int sendToTag(String tag,String notificationTitle,String notificationContent, String msgTitle, String msgContent, Map<String, String> extras) {
int result=0;
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload = JpushUtil.buildPushObject_all_tag_alertWithTitle(tag, notificationTitle,notificationContent, msgTitle, msgContent, extras);
try {
PushResult pushResult = jpushClient.sendPush(payload);
System.out.println(pushResult.msg_id);
System.out.println(pushResult.sendno);
if(pushResult.getResponseCode()==200){
result=1;
}
LOG.info("Got result - " + pushResult);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
return result;
}
// public static void main(String[] args) {
// //new Jpush().sendToAll(“ss”, “df”);
// }
/**
*
*
* @param 通知标题
* @param 信息标题
* @param 信息内容
* @param 穿透字段,可用来跳转指定页面
* @return
*/
public static PushPayload buildPushObject_android_and_ios(String notificationTitle,String notificationContent, String msgTitle, String msgContent, Map<String, String> extras) {
System.out.println("----------ALL----------------");
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.all())
.setNotification(Notification.newBuilder()
.setAlert(notificationTitle)
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notificationContent)//通知内容
.setTitle(notificationTitle)//通知标题
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtras(extras)
.build()
)
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(notificationContent)
.incrBadge(1)
.setSound("sound.caf")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtras(extras)
//此项说明此推送是一个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(msgContent)
.setTitle(msgTitle)
.addExtras(extras)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
.build()
)
.build();
}
private static PushPayload buildPushObject_all_registrationTel_alertWithTitle(String tel,String notificationTitle,String notificationContent, String msgTitle, String msgContent, Map<String, String> extras) {
System.out.println("----------TEL----------------");
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(tel))
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notificationContent)
.setTitle(notificationTitle)
.addExtras(extras)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(notificationContent)
.incrBadge(1)
.setSound("sound.caf")
.addExtras(extras)
.build())
.build())
.setMessage(Message.newBuilder()
.setMsgContent(msgContent)
.setTitle(msgTitle)
.addExtras(extras)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
.setSendno(1)
.setTimeToLive(86400)
.build())
.build();
}
private static PushPayload buildPushObject_all_tag_alertWithTitle(String tag,String notificationTitle,String notificationContent, String msgTitle, String msgContent, Map<String, String> extras) {
System.out.println("----------TAG----------");
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.tag(tag))
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(notificationContent)
.setTitle(notificationTitle)
.addExtras(extras)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(notificationContent)
.incrBadge(1)
.setSound("sound.caf")
.addExtras(extras)
.build())
.build())
.setMessage(Message.newBuilder()
.setMsgContent(msgContent)
.setTitle(msgTitle)
.addExtras(extras)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
.setSendno(1)
.setTimeToLive(86400)
.build())
.build();
}
public static void main(String[] args) {
Map<String,String> a=new HashMap<String, String>();
a.put("2", "2");
new JpushUtil().sendToAll("ds", "dg", "dg", "dsf", a);
}
}
“`