package com.suka.common.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.PushPayload.Builder;
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 JiGuangPushUtil {
private static JPushClient carJPushClient=new JPushClient(Constants.CAR_MASTERSECRET,Constants.CAR_APPKEY);
private static JPushClient goodsJPushClient=new JPushClient(Constants.GOODS_MASTERSECRET,Constants.GOODS_APPKEY);
//日志
protected static final Logger log = LoggerFactory.getLogger(JiGuangPushUtil.class);
/**
* 通知推送
* 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
* @param type 推送方式:1、“tag”标签推送,2、“alias”别名推送 3 、"all" 全部推送
* @param value 推送的标签或别名值
* @param alert 推送的内容
* @param regType:端口类型 0:货主端 1:车主端
* @param Extra 参数 类型map
*/
@SuppressWarnings("unused")
private static void pushNotice(String type,String value,String alert,Integer regType,Map<String, String> extraMap ){
Builder builder= PushPayload.newBuilder();
builder.setPlatform(Platform.all());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
//设置如果用户不在线、离线消息保存的时间
Options options=Options.sendno();
options.setTimeToLive(86400l); //设置为86400为保存一天,如果不设置默认也是保存一天
options.setApnsProduction(true); //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
builder.setOptions(options);
//设置推送方式
if(type.equals("alias")){
builder.setAudience(Audience.alias(value));//根据别名推送
}else if(type.equals("tag")){
builder.setAudience(Audience.tag(value));//根据标签推送
}else{
builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
}
builder.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder().addExtras(extraMap).setAlert(alert).build())
.addPlatformNotification(IosNotification.newBuilder().addExtras(extraMap).setAlert(alert).build())
.build());
PushPayload pushPayload=builder.build();
try{
//进行推送,实际推送就在这一步
if(regType==0){
//货主端 推送
PushResult goodsPushResult=goodsJPushClient.sendPush(pushPayload);
}else if(regType==1) {
//车主端 推送
PushResult carPushResult=carJPushClient.sendPush(pushPayload);
}else {
log.info("推送异常");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 自定义消息推送
* 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
* @param type 推送方式:1、“tag”标签推送,2、“alias”别名推送 3 "all" 全部推送
* @param value 推送的标签或别名值
* @param alert 推送的内容
* @param regType:端口类型 0:货主端 1:车主端
* @param Extra 参数 类型map
*
*/
@SuppressWarnings("unused")
private static void pushMsg(String type, String value,String alert,Integer regType,Map<String, String> extraMap ){
Builder builder= PushPayload.newBuilder();
builder.setPlatform(Platform.all());//设置接受的平台
if(type.equals("alias")){
builder.setAudience(Audience.alias(value));//别名推送
}else if(type.equals("tag")){
builder.setAudience(Audience.tag(value));//标签推送
}else{
builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
}
Message.Builder newBuilder=Message.newBuilder();
newBuilder.setMsgContent(alert);//消息内容
newBuilder.addExtras(extraMap);
Message message=newBuilder.build();
builder.setMessage(message);
PushPayload pushPayload=builder.build();
try{
//进行推送,实际推送就在这一步
if(regType==0){
//货主端 推送
PushResult goodsPushResult=goodsJPushClient.sendPush(pushPayload);
}else if(regType==1) {
//车主端 推送
PushResult carPushResult=carJPushClient.sendPush(pushPayload);
}else {
log.info("推送异常");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 同时推送多个设备------通知推送
* 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
* @param type 推送方式:1、“tag”标签推送,2、“alias”别名推送
* @param value List<String> 类型 推送的标签或别名值
* @param alert 推送的内容
* @param regType:端口类型 0:货主端 1:车主端
* @param Extra 参数 类型map
*/
@SuppressWarnings("unused")
private static void pushNoticeMany(String type,List<String> value,String alert,Integer regType,Map<String, String> extraMap ){
Builder builder= PushPayload.newBuilder();
builder.setPlatform(Platform.all());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
//设置如果用户不在线、离线消息保存的时间
Options options=Options.sendno();
options.setTimeToLive(86400l); //设置为86400为保存一天,如果不设置默认也是保存一天
options.setApnsProduction(true); //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
builder.setOptions(options);
//设置推送方式
if(type.equals("alias")){
builder.setAudience(Audience.alias(value));//根据别名推送
}else if(type.equals("tag")){
builder.setAudience(Audience.tag(value));//根据标签推送
}
builder.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder().addExtras(extraMap).setAlert(alert).build())
.addPlatformNotification(IosNotification.newBuilder().addExtras(extraMap).setAlert(alert).build())
.build());
PushPayload pushPayload=builder.build();
try{
//进行推送,实际推送就在这一步
if(regType==0){
//货主端 推送
PushResult goodsPushResult=goodsJPushClient.sendPush(pushPayload);
}else if(regType==1) {
//车主端 推送
PushResult carPushResult=carJPushClient.sendPush(pushPayload);
}else {
log.info("推送异常");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 同时推送多个设备---自定义消息推送
* 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
* @param type 推送方式:1、“tag”标签推送,2、“alias”别名推送
* @param value List<String> 类型 推送的标签或别名值
* @param alert 推送的内容
* @param regType:端口类型 0:货主端 1:车主端
* @param Extra 参数 类型map
*
*/
@SuppressWarnings("unused")
private static void pushMsgMany(String type,List<String> value,String alert,Integer regType,Map<String, String> extraMap){
Builder builder= PushPayload.newBuilder();
builder.setPlatform(Platform.all());//设置接受的平台
//设置推送方式
if(type.equals("alias")){
builder.setAudience(Audience.alias(value));//根据别名推送
}else if(type.equals("tag")){
builder.setAudience(Audience.tag(value));//根据标签推送
}
Message.Builder newBuilder=Message.newBuilder();
newBuilder.setMsgContent(alert);//消息内容
newBuilder.addExtras(extraMap);
Message message=newBuilder.build();
builder.setMessage(message);
PushPayload pushPayload=builder.build();
try{
//进行推送,实际推送就在这一步
if(regType==0){
//货主端 推送
PushResult goodsPushResult=goodsJPushClient.sendPush(pushPayload);
}else if(regType==1) {
//车主端 推送
PushResult carPushResult=carJPushClient.sendPush(pushPayload);
}else {
log.info("推送异常");
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Map<String, String> map =new HashMap<>();
map.put("jumpType", "1");
map.put("id", "123456");
//给标签为kefu的用户进行消息推送
JiGuangPushUtil.pushNotice("alias","8","你有新的任务,请及时处理", 1,map);
}
}