激光推送工具类 package org.common.serveradmin.utils;/** * @Author:LiuHao * @Date: Create in 16:00 2017/10/16 **/import cn.jiguang.common.resp.APIConnectionException;import cn.jiguang.common.resp.APIRequestException;import cn.jpush.ap
package org.common.serveradmin.utils;
/**
* @Author:LiuHao
* @Date: Create in 16:00 2017/10/16
**/
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;
import org.common.serveradmin.emuns.ServerAdminCodeEmun;
import org.common.serveradmin.exception.ServerAdminException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.woke.serveradmin.dao.mapper.MessageReserveMapper;
import org.woke.serveradmin.dao.models.MessageReserve;
import org.woke.serveradmin.dao.models.Push;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JpushUtil {
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtil.class);
private static final String appKey = "*****";
private static final String masterSecret = "****";
public int sendToAll(String notificationTitle, String notificationContent, String msgTitle, String msgContent, Map
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);
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;
}
//tel是别名 notificationTitle通知标题 notificationContent通知内容 msgTitle自定义消息标题 msgContent自定义消息内容
public int sendToTel(String tel, String notificationTitle, String notificationContent, String msgTitle, String msgContent, Map
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); 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
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); 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 PushPayload buildPushObject_android_and_ios(String notificationTitle, String notificationContent, String msgTitle, String msgContent, Map
extras) { 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
extras) { 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
extras) { 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
a=new HashMap
(); a.put("2", "2"); new JpushUtil().sendToTel("123456","ds", "dg", "dg", "测试", a); } }
pom.xml
cn.jpush.api jpush-clientRELEASE
