|
IOS安卓,设置通知声音消息等, 采用的是别名。
package jpush;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.connection.HttpProxy;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
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 cn.jpush.api.schedule.ScheduleResult;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.action.ActionContext;
public class Push {
private static String appKey = "";
private static String masterSecret = "";
private static Boolean apnsProduction = false;
private static JPushClient jpushClient;
private static Boolean useProxy = false;
static{
InputStream configFile = Push.class.getResourceAsStream("jpush.config.xml");
try{
SAXReader reader = new SAXReader();
Document doc = reader.read(configFile);
Element config = doc.getRootElement();
appKey = config.elementTextTrim("appKey");
masterSecret = config.elementTextTrim("masterSecret");
apnsProduction = (config.elementTextTrim("apnsProduction").equals("true"))?true:false;
}catch(Exception e){
e.printStackTrace();
}
}
public static JSONObject push(JSONObject params, ActionContext context){
String registrationId = params.getString("registrationId");
try {
sendPushMessage(registrationId, appKey, masterSecret);
} catch (APIConnectionException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (APIRequestException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return null;
}
public static JSONObject pushAlias(JSONObject params, ActionContext context){
String alias = params.getString("alias");
String mess = params.getString("sendMessage");
try {
sendPushMessageAlias(alias,mess, appKey, masterSecret);
} catch (APIConnectionException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (APIRequestException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return null;
}
public static ScheduleResult sendPushMessage(String registrationId, String key, String secret) throws APIConnectionException, APIRequestException{
ClientConfig config = ClientConfig.getInstance();
HttpProxy proxy = null;
if (useProxy) {
proxy = new HttpProxy("http-proxy.system", 3128);
}
jpushClient = new JPushClient(secret, key, 3, proxy, config);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar nowTime = Calendar.getInstance();
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.registrationId(registrationId))
.setNotification(Notification.alert("Android测试.." + nowTime.getTime()))
.build();
payload.resetOptionsTimeToLive(86400);
payload.resetOptionsApnsProduction(apnsProduction);
nowTime.add(Calendar.SECOND, 10);
String scheduleTime =sdf.format(nowTime.getTime());
ScheduleResult result = jpushClient.createSingleSchedule(UUID.randomUUID().toString().replaceAll("-", ""), scheduleTime, payload);
return result;
}
public static ScheduleResult sendPushMessageAlias(String alias, String mess, String key, String secret) throws APIConnectionException, APIRequestException{
ClientConfig config = ClientConfig.getInstance();
HttpProxy proxy = null;
if (useProxy) {
proxy = new HttpProxy("http-proxy.system", 3128);
}
Set<String> tags = new HashSet<String>();
if (alias != null && alias.length() > 0) {
String[] arrys = alias.split(";");
for (String str : arrys) {
tags.add(str);
}
}
jpushClient = new JPushClient(secret, key, 3, proxy, config);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar nowTime = Calendar.getInstance();
String extrasparam = "{a:1}";
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(tags))
.setNotification(Notification.newBuilder()
// .setAlert("1111")
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(mess)
.setTitle("订单通知")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("androidNotification extras key",extrasparam)
.build()
)
.addPlatformNotification(IosNotification.newBuilder()
//传一个IosAlert对象,指定apns title、title、subtitle等
.setAlert(mess)
//直接传alert
//此项是指定此推送的badge自动加1
.incrBadge(1)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("default")
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtra("iosNotification extras key",extrasparam)
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_ ... remote-notification
// .setContentAvailable(true)
.build()
)
.build()
)
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
.setMessage(Message.newBuilder()
.setMsgContent("1111")
.setTitle("1111")
.addExtra("message extras key",extrasparam)
.build())
.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(false)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build()
)
.build();
payload.resetOptionsTimeToLive(86400);
payload.resetOptionsApnsProduction(apnsProduction);
nowTime.add(Calendar.SECOND, 10);
String scheduleTime =sdf.format(nowTime.getTime());
ScheduleResult result = jpushClient.createSingleSchedule(UUID.randomUUID().toString().replaceAll("-", ""), scheduleTime, payload);
return result;
}
}
|
|