极光推送和文件读取


//文件读取

package com.daqi.core.utils;



import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * 读取Properties类,绑定到classpath下的source/Source.properties文件。
 * @author ssq
 */
public class PropertiesUtil {
    //配置文件的路径
    @SuppressWarnings("unused")
private String configPath=null;
    
    /**
     * 配置文件对象
     */
    private static Properties props=null;
    
    /**
     * 导出考勤时表格使用的xlsx模板存放地址
     */
    public static final String EXPORT_TEMPLATE;
    
    /**
     * 导出考勤时本地存放根文件夹
     */
    public static final String EXPORT_REALPATH;
    
    /**
     * 导出考勤时,存放的网络路径,(目前)不包括域名
     */
    public static final String EXPORT_DOWNLOAD_PATH;
    
    /**
     * 默认构造函数,自动找到classpath下的source/Source.properties。
     */
   static{
        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("source/Source.properties");
        String template="",realpath="",downloadurl="";
        props = new Properties();
        try {
props.load(in);

template = props.getProperty("export.template");
realpath = props.getProperty("export.realpath");
downloadurl = props.getProperty("export.download.path");

} catch (IOException e) {
e.printStackTrace();
}finally {
try {
   //关闭资源
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
        
        EXPORT_TEMPLATE = template;
EXPORT_REALPATH = realpath;
EXPORT_DOWNLOAD_PATH = downloadurl;
    }
   
   
    
    /**
     * 根据key值读取配置的值
     * @author ssq
     * @param key key值
     * @return key 键对应的值  字符串
     * @throws IOException 
     */
    public static String readValue(String key) throws IOException {
        return  props.getProperty(key);
    }
    
    /**
     * 读取properties的全部信息放入map
     * 
     * @author ssq
     * @throws FileNotFoundException 配置文件没有找到
     * @throws IOException 关闭资源文件,或者加载配置文件错误
     * 
     */
    public static Map<String,String> readAllProperties() throws FileNotFoundException,IOException  {
        //保存所有的键值
        Map<String,String> map=new HashMap<String,String>();
        @SuppressWarnings("rawtypes")
Enumeration en = props.propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String Property = props.getProperty(key);
            map.put(key, Property);
        }
        return map;
    }


 
   

}













//极光推送


package com.daqi.core.utils;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;
import com.daqi.mos.jpush.vo.Registration_idBean;
import com.daqi.mos.message.dto.JpushDto;
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.audience.AudienceTarget;  
import cn.jpush.api.push.model.notification.AndroidNotification;  
import cn.jpush.api.push.model.notification.IosNotification;  
import cn.jpush.api.push.model.notification.Notification;  


/**
 * @author ssq
 * @date 2018-1-5
 * 极光简单推送
 * 
 * */
public class JpushUtil {
  

    protected static final Logger LOG = LoggerFactory.getLogger(JpushUtil.class);  
  
    private static final String appKeyIOS ;  
    private static final String appKeyAndroid ;  
private static final String masterSecret ;  
    public static  String TITLE = "";  
    public static  String ALERT = "";  
    public static  String MSG_CONTENT = "";  
    public  static JPushClient jpushClientIOS=null;  
    public  static JPushClient jpushClientAndroid=null;  
    public  static JPushClient jpushClient=null;  
    static{
    String keyIOS="";
    String keyAndroid="";
    String secret="";
    try {
    keyIOS = PropertiesUtil.readValue("appKeyIOS");
    keyAndroid= PropertiesUtil.readValue("appKeyAndroid");
    secret = PropertiesUtil.readValue("masterSecret");
   
   
    } catch (IOException e) {
    LOG.warn("账号密码异常",e);
    e.printStackTrace();
    }
    appKeyIOS=keyIOS;
    appKeyAndroid=keyAndroid;
    masterSecret=secret;
    }
      
    
    /**
     * @author ssq
     * @date 2018-1-5
     * ios平台的全部推送
     * */
    @SuppressWarnings("deprecation")
public static void ios_AllPush( String message ) {  
    JpushDto jpush = FastJsonUtils.toBean(message,JpushDto.class);
    HashMap<String, String> map = new HashMap<String,String>(); 
          if (jpush!=null) {
        TITLE=jpush.getTitle();  
ALERT=jpush.getContent();
MSG_CONTENT=message;
LOG.info("字符长度:"+message.length());
map.put("extras", message);
}
         jpushClient = new JPushClient(masterSecret, appKeyIOS, 3);  
        //推送,所有你需要做的是建立pushpayload对象。 
         //生成推送的内容,这里我们先测试全部推送  
         //PushPayload payload = buildPushObject_all_all_alert();  
         
         //ios推送
        PushPayload payload = buildPushObject_ios_all_alert(map);
        try {  
            PushResult result = jpushClient.sendPush(payload);  
            LOG.info("封装对象:"+payload.toString());
            LOG.info("返回结果: " + result);  
        } 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());  
        }  
    } 
      
    /**
     * @author ssq
     * @date 2018-1-5
     * 安卓平台的全部推送
     * */
    @SuppressWarnings("deprecation")
    public static void android_AllPush(String message ) {  
    JpushDto jpush = FastJsonUtils.toBean(message,JpushDto.class);
    HashMap<String, String> map = new HashMap<String,String>(); 
          if (jpush!=null) {
        TITLE=jpush.getTitle();  
ALERT=jpush.getContent();
MSG_CONTENT=message;
LOG.info("字符长度:"+message.length());
map.put("extras", message);
}
    jpushClient = new JPushClient(masterSecret, appKeyAndroid, 3);  
    //推送,所有你需要做的是建立pushpayload对象。 
    //安卓推送
    PushPayload payload = buildPushObject_android_all_alert(map);
    try {  
      PushResult result = jpushClient.sendPush(payload);  
          LOG.info("封装对象:"+payload.toString());
          LOG.info("返回结果: " + result);  
    } 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());  
    }  
    } 
    
    /**
     * @author ssq
     * @date 2018-1-5
     *全部平台的全部推送
     * */
    @SuppressWarnings("deprecation")
    public static void all_AllPush(String message ) {  
    JpushDto jpush = FastJsonUtils.toBean(message,JpushDto.class);
    HashMap<String, String> map = new HashMap<String,String>(); 
          if (jpush!=null) {
        TITLE=jpush.getTitle();  
ALERT=jpush.getContent();
MSG_CONTENT=message;
LOG.info("字符长度:"+message.length());
map.put("extras", message);
}
    jpushClient = new JPushClient(masterSecret, appKeyAndroid, 3);  
    //全部推送
    PushPayload payload = buildPushObject_all_alias_alert(map);
    try {  
      PushResult result = jpushClient.sendPush(payload);  
          LOG.info("封装对象:"+payload.toString());
          LOG.info("返回结果: " + result);  
    } 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());  
    }  
    } 
    
    
    
    


    /**
     * @author ssq
     * @date 2018-1-5
     *全部平台根据RegistrationId进行推送
     *@param alert 消息内容
     *@param registrationIdList registrationId集合
     *TODO
     * */
    @SuppressWarnings({ "deprecation" })
    public static void all_ByRegistrationIdPush(String message) {  
    JpushDto jpush = FastJsonUtils.toBean(message,JpushDto.class);
   
    List<String>  registrationIdListIos  = new ArrayList<String>();
    List<String>  registrationIdListAndroid  = new ArrayList<String>();
   
      Object registrationIdList = jpush.getRegistrationIdList();
      String jsonNoFeatures = FastJsonUtils.toJSONNoFeatures(registrationIdList);
      List<Registration_idBean> registration_idBeanList = FastJsonUtils.toList(jsonNoFeatures, Registration_idBean.class);
    for ( Registration_idBean registration_idBean : registration_idBeanList) {
    //判断非null  再区别设备  0--IOS   1--安卓
    if (registration_idBean.getDevicetype()!=null) {
    if (registration_idBean.getDevicetype()==0) {
    if (registration_idBean.getRegistrationid()!=null&&registration_idBean.getRegistrationid()!="") {
    registrationIdListIos.add(registration_idBean.getRegistrationid());
}
    }else {
    if (registration_idBean.getRegistrationid()!=null&&registration_idBean.getRegistrationid()!="") {
    registrationIdListAndroid.add(registration_idBean.getRegistrationid());
}
    }
}
}
    HashMap<String, String> map = new HashMap<String,String>(); 
    HashMap<String, Object> hashMap = new HashMap<String,Object>();
          if (jpush!=null) {
        TITLE=jpush.getTitle();  
Integer enumstateid = jpush.getEnumstateid();
String content = jpush.getContent();
String alert=content;
if (enumstateid==70) {
//拼接弹窗
if (content.contains(",")&&content.contains("__")) {
String[] split = content.split("__");
String[] split2 = split[2].split(",");
alert=split[0]+split[1]+","+split2[1];
}else {
alert="您有新消息待查看";
}
hashMap.put("name", jpush.getName());
hashMap.put("idcard", jpush.getIdcard());
hashMap.put("projectid", jpush.getProjectid());
hashMap.put("projectname", jpush.getProjectname());
}
ALERT=alert;
MSG_CONTENT=message;
LOG.info("标题"+TITLE);
LOG.info("传过来消息总和"+message);
LOG.info("字符长度:"+message.length());
hashMap.put("msgid", jpush.getMsgid());
hashMap.put("content", content);
hashMap.put("title", jpush.getTitle());
hashMap.put("msgtype", jpush.getMsgtype());


hashMap.put("enumstateid", enumstateid);
hashMap.put("deviceid", jpush.getDeviceid());

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String sendtime = simpleDateFormat.format(date);
hashMap.put("sendtime", sendtime);
LOG.info("推送时间:"+sendtime);
String string = FastJsonUtils.toJSONNoFeatures(hashMap);
map.put("extras", string);
}
    jpushClientIOS = new JPushClient(masterSecret, appKeyIOS, 3);  
    jpushClientAndroid = new JPushClient(masterSecret, appKeyAndroid, 3);  
    //根据rid集合推送
    PushPayload payloadIos=null;
    PushPayload payloadAndroid =null;
    if (registrationIdListIos.size()>0) {
    String readValue ="";
    try {
readValue = PropertiesUtil.readValue("environmental");
} catch (IOException e) {
e.printStackTrace();
}
    if ("1".equals(readValue)) {
    payloadIos = buildPushObject_all_registration_id_alert_ios_test(registrationIdListIos,map);
}else {
payloadIos = buildPushObject_all_registration_id_alert_ios_formal(registrationIdListIos,map);
}
}
    if (registrationIdListAndroid.size()>0) {
    payloadAndroid = buildPushObject_all_registration_id_alert_android(registrationIdListAndroid,map);

}
    try { 
    //IOS推送
    if (payloadIos!=null) {
    PushResult resultIos = jpushClientIOS.sendPush(payloadIos);  
    LOG.info("封装对象ios:"+payloadIos.toString());
    LOG.info("返回结果ios: " + resultIos);  
    }
    //安卓推送
    if (payloadAndroid!=null) {
    PushResult resultAndroid = jpushClientAndroid.sendPush(payloadAndroid);  
    LOG.info("返回结果Android: " + resultAndroid);  
    LOG.info("封装对象Android:"+payloadAndroid.toString());
}
    } 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());  
    }  
    } 
    
    
    
    
    
    
    
    
    
    
    
    
    
    public static PushPayload buildPushObject_all_all_alert() {  
        return PushPayload.alertAll(ALERT);  
    }  
  
    
    
    
    
    //构建推送对象:所有平台,推送目标的别名是all  内容为ALERT
    public static PushPayload buildPushObject_all_alias_alert(Map<String, String> map) {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.all())//设置接受的平台  
                .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到  别名
                .setNotification(Notification.ios(ALERT, map))  //通知内容
                .build();  
    }  
    
     
    
    
//     ios平台  构建推送对象:ios平台,推送目标的别名是all  内容为ALERT
    public static PushPayload buildPushObject_ios_all_alert(Map<String, String> map) {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.ios())//设置接受的平台  
                .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到  别名
                .setNotification(Notification.ios(ALERT, map))  //IOS通知内容
                .build();  
    }  
    
    
    
    
    
    
//     安卓平台  构建推送对象:android平台,推送目标的别名是all  内容为ALERT
    public static PushPayload buildPushObject_android_all_alert(Map<String, String> map) {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.android())//设置接受的平台  
                .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到  别名
                .setNotification(Notification.ios(ALERT, map))  //IOS通知内容
                .build();  
    }  
    
    
    
    
    
    //构建推送对象:所有平台,推送目标根据registration_id           android内容推送
    public static PushPayload buildPushObject_all_registration_id_alert_android(List<String> registrationId,Map<String, String> map) {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.all())//置接受的平台  
                .setAudience(Audience.registrationId(registrationId))//根据registrationId推送指定对象
                .setNotification(Notification.android(ALERT, TITLE, map))  //通知内容
                .setMessage(Message.content(MSG_CONTENT))  //消息内容
                .build();  
    }  
    
    //构建推送对象:所有平台,推送目标根据registration_id    IOS 内容推送
    public static PushPayload buildPushObject_all_registration_id_alert_ios_test(List<String> registrationId,Map<String, String> map) {  
        return PushPayload.newBuilder()  
        .setOptions(Options.newBuilder().setApnsProduction(false).build())//推送测试开发环境  false是开发环境    true 正式环境  不写默认false
                .setPlatform(Platform.all())//置接受的平台  
                .setAudience(Audience.registrationId(registrationId))//根据registrationId推送指定对象
                .setNotification(Notification.ios(ALERT, map))  //通知内容
                .setMessage(Message.content(MSG_CONTENT))  //消息内容
                .build();  
    }  
    
    
    
    //构建推送对象:所有平台,推送目标根据registration_id    IOS 内容推送
    public static PushPayload buildPushObject_all_registration_id_alert_ios_formal(List<String> registrationId,Map<String, String> map) {  
        return PushPayload.newBuilder()  
        .setOptions(Options.newBuilder().setApnsProduction(true).build())//推送测试开发环境  false是开发环境    true 正式环境  不写默认false
                .setPlatform(Platform.all())//置接受的平台  
                .setAudience(Audience.registrationId(registrationId))//根据registrationId推送指定对象
                .setNotification(Notification.ios(ALERT, map))  //通知内容
                .setMessage(Message.content(MSG_CONTENT))  //消息内容
                .build();  
    }  
    
    
    
    
    
    
    
    
    
    
    
    
    
      //构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE。
    public static PushPayload buildPushObject_android_tag_alertWithTitle() {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.android())  
                .setAudience(Audience.tag("tag1"))  
                .setNotification(Notification.android(ALERT, TITLE, null))  
                .build();  
    }  
      
    public static PushPayload buildPushObject_android_and_ios() {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.android_ios())  
                .setAudience(Audience.tag("tag1"))  
                .setNotification(Notification.newBuilder()  
                        .setAlert("alert content")  
                        .addPlatformNotification(AndroidNotification.newBuilder()  
                                .setTitle("Android Title").build())  
                        .addPlatformNotification(IosNotification.newBuilder()  
                                .incrBadge(1)  
                                .addExtra("extra_key", "extra_value").build())  
                        .build())  
                .build();  
    }  
      
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.ios())  
                .setAudience(Audience.tag_and("tag1", "tag_all"))  
                .setNotification(Notification.newBuilder()  
                        .addPlatformNotification(IosNotification.newBuilder()  
                                .setAlert(ALERT)  
                                .setBadge(5)  
                                .setSound("happy")  
                                .addExtra("from", "JPush")  
                                .build())  
                        .build())  
                 .setMessage(Message.content(MSG_CONTENT))  
                 .setOptions(Options.newBuilder()  
                         .setApnsProduction(true)  
                         .build())  
                 .build();  
    }  
      
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {  
        return PushPayload.newBuilder()  
                .setPlatform(Platform.android_ios())  
                .setAudience(Audience.newBuilder()  
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))  
                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))  
                        .build())  
                .setMessage(Message.newBuilder()  
                        .setMsgContent(MSG_CONTENT)  
                        .addExtra("from", "JPush")  
                        .build())  
                .build();  
    } 
    
    


    
//     ios平台  完整推送
 public static PushPayload buildPushObject_ios_messageWithExtras() {  
    PushPayload payload = PushPayload.newBuilder()  
                                    .setPlatform(Platform.ios())  
                                    .setAudience(Audience.all())  
                                    .setNotification(Notification.newBuilder()  
                                            .addPlatformNotification(IosNotification.newBuilder()  
                                                    .setAlert("测试推送-ios")  
                                                    .setBadge(5)  
                                                    .setSound("happy")  
                                                    .addExtra("from", "JPush")  
                                                    .build())  
                                            .build())  
                                     .setMessage(Message.content("测试推送-content-ios"))  
                                     .setOptions(Options.newBuilder()  
                                             .setApnsProduction(true)  
                                             .build())  
                                     .build();
return payload; 
    
    }
 
 
// 18071adc0307c356e75
 public static void main(String[] args) {
JpushDto jpushDto = new JpushDto();
ArrayList<Registration_idBean> registrationIdList = new ArrayList<Registration_idBean>();
 
Registration_idBean registration_idBean = new Registration_idBean();
registration_idBean.setDevicetype(0);
registration_idBean.setRegistrationid("161a3797c80e9a09b58");
registrationIdList.add(registration_idBean);
 
Registration_idBean registration_idBeanAndroid = new Registration_idBean();
registration_idBeanAndroid.setDevicetype(1);
registration_idBeanAndroid.setRegistrationid("160a3797c80ef3a9859");
registrationIdList.add(registration_idBeanAndroid);
jpushDto.setRegistrationIdList(registrationIdList);
 
 
jpushDto.setEnumstateid(70);
jpushDto.setContent("hello");
jpushDto.setMsgid("125656");
jpushDto.setMsgtype(0);
String jsonNoFeatures = FastJsonUtils.toJSONNoFeatures(jpushDto);
all_ByRegistrationIdPush(jsonNoFeatures);

}
 
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值