package com.huaxin.cloud.tianjian.util;
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* Created by Chen Meng on 2017/9/29.
*/
public class JPushHelper {
private static Properties P= null;
// static {
// try {
// P=getP();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//单例
private static JPushHelper jPushHelper=null;
private static JPushClient jPushClient=null;
private JPushHelper(){
jPushClient = new JPushClient("6cd1109d137ce735ae99883d","de17ee2a8c586fc764f18027"); //matersecret和appkey
}
public static synchronized JPushHelper getInstance(){
if(jPushHelper==null){
jPushHelper=new JPushHelper();
}
return jPushHelper;
}
//向单个用户发送通知
public static void sendPush(String alert,List<String> aliasList) throws APIConnectionException, APIRequestException, IOException {
//JSONObject json = new JSONObject();
//json.put("huaxin",map1);
List<String> newList = new ArrayList<>();
for(String item:aliasList) {
newList.add("tj_"+item);
}
// Boolean APNSPRODUCTION=Boolean.parseBoolean(P.getProperty("ApnsProduction"));
Map<String,String> map = new HashMap<String,String>();
//安卓推送
PushPayload AZpushPayload= PushPayload.newBuilder()
. setPlatform(Platform.android()) //推送的平台 Platform.ios()--ios平台 Platform.android()--安卓平台
.setAudience(Audience.alias(newList)) //推送的对象
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(alert) //alert为推送内容
//* .setBadge(1) //角标显示的数字*//*
//* .setSound("happy") //设置推送的声音*//*
.addExtras(map) //附加字段
.build())
.build())
.setMessage(Message.content(alert)) //推送内容是 - 内容为 MSG_CONTENT 的消息
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
//ios推送
PushPayload IospushPayload= PushPayload.newBuilder()
. setPlatform(Platform.ios()) //推送的平台 Platform.ios()--ios平台 Platform.android()--安卓平台
.setAudience(Audience.alias(newList)) //推送的对象
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(alert) //alert为推送内容
.setBadge(1) //角标显示的数字
.setSound("happy") //设置推送的声音
.addExtras(map) //附加字段
.build())
.build())
.setMessage(Message.content(alert)) //推送内容是 - 内容为 MSG_CONTENT 的消息
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
//同时推 安卓和ios 消息为自定义消息 安卓和IOS需改动 暂不启用
/* PushPayload pushPayload=PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(newList))
.setMessage(Message.newBuilder()
.setMsgContent(alert)
.addExtra("from", "JPush")
.build())
.build();*/
try{
PushResult AZresult = jPushClient.sendPush(AZpushPayload);
}catch (Exception e){
e.printStackTrace();
}
try{
PushResult Iosresult = jPushClient.sendPush(IospushPayload);
}catch (Exception e){
e.printStackTrace();
}
}
//读取配置文件,来设置setApnsProduction(APNSPRODUCTION)的值
// public static Properties getP() throws IOException {
// ClassLoader classLoader = JPushHelper.class.getClassLoader();// 读取属性文件 xxxxx.properties
// InputStream in = classLoader.getResourceAsStream("resources.properties");
// Properties p = new Properties();
// p.load(in);
// in.close();
// return p;
// }
}
极光推送-工具类
最新推荐文章于 2023-07-22 17:13:09 发布