public class JPushUtils {
//Portal上注册应用时生成的 masterSecret
private static final String MASTER_SECTET = "";
//Portal上注册应用时生成的 appKey
private static final String APP_KEY = "";
//从消息推送时起,保存离线的时长。秒为单位。最多支持10天(864000秒)。
private static PushPayload pushNotifiton(String receiver_type ,String receiver_value,String content ,Map<String, String> extra,String title){
extra.put("title", title);
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(receiver_value))
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title)
.setAlert(content)
.addExtras(extra).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.setBadge(0)
.setSound("")
.setAlert(content)
.addExtras(extra).build())
.build()).build();
}
/**
* 向手机端推送通知
* @param userids 用户id多个逗号隔开
* @param content 推送内容
* @param businessId 业务id,android客户端根据业务id判断是那个业务
* @return
*/
public static boolean pushNotifiton(String userids,String content ,Map<String,String> extra,String title){
boolean flag=false;
JPushClient jpushClient = new JPushClient(MASTER_SECTET, APP_KEY, 3);
PushPayload payload=pushNotifiton("3",userids,content,extra,title);
try {
jpushClient.sendPush(payload);
flag=true;
} catch (Exception e) {
flag=false;
}
return flag;
}
/**
* 向手机端推送消息
* @param userids 用户id多个逗号隔开
* @param content 推送内容
* @param businessId 业务id,android客户端根据业务id判断是那个业务
* @return
*/
public static boolean pushMsg(String userids,String content,Map<String,Object> extra){
boolean flag=false;
JPushClient jpushClient = new JPushClient(MASTER_SECTET, APP_KEY, 3);
PushPayload payload = pushMsg("3",userids,content,extra);
try {
PushResult result = jpushClient.sendPush(payload);
flag=true;
} catch (Exception e) {
// TODO Auto-generated catch block
flag=false;
}
return flag;
}
/**
* 向客户端发送消息方法
* @param receiver_type 发送类型
* @param receiver_value 接收人
* @param content 消息内容
* @param businessId 业务id
*/
private static PushPayload pushMsg(String receiver_type ,String receiver_value,String content,Map<String,Object> extra){
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.alias(receiver_value))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(content)
.addExtra("noticeId", extra.get("noticeId").toString())
.addExtra("type", extra.get("type").toString())
.build())
.build();
}
public static void main(String[] args) {
Map<String,String> extra = new LinkedHashMap<String,String>();
//公告id
extra.put("noticeId", "45678");
extra.put("type", "s");
extra.put("sendman", "我");
Format format = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
extra.put("sendtime", format.format(new Date()));
extra.put("content", "我看对");
System.out.println(pushNotifiton("admi1n","cs1",extra,"cs2"));
}
}