public class JpushServiceImpl implements IJpushService {
private final static String appKey = "ad4acd0fd9004140c22d00d8";
private final static String masterSecret = "xxxxxxxxxx";
private final static int maxtrytimes = 3;
JPushClient jpushClient = new JPushClient(masterSecret, appKey, maxtrytimes);
public PushPayload buildPushObject(String title, String content,
String from, Audience audience ,Map extra,Boolean sendNotify) {
//推送对象构造
AndroidNotification android = AndroidNotification.newBuilder()
.setTitle(title).addExtra("from", from)
.addExtra("sendtime", System.currentTimeMillis()).build();
IosNotification ios = IosNotification.newBuilder().incrBadge(1)
.addExtra("from", from).setSound("default")
.addExtra("sendtime", System.currentTimeMillis()).build();
Notification notification = Notification.newBuilder().setAlert(content)
.addPlatformNotification(android).addPlatformNotification(ios)
.build();//通知构造
Message msg;
//消息构造
if(extra==null){
msg = Message.newBuilder().setTitle(title)
.setMsgContent(content).build();
}else{
msg = Message.newBuilder().setTitle(title)
.setMsgContent(content).addExtras(extra).build();
//发送额外信息可用addExtras传map,也可直接用addExtra传键值对
}
if(sendNotify){
return PushPayload.newBuilder().setPlatform(Platform.android_ios())
.setAudience(audience).setNotification(notification)
.setMessage(msg).build();
}else{
return PushPayload.newBuilder().setPlatform(Platform.android_ios())
.setAudience(audience)
.setMessage(msg).build();
}
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
JpushServiceImpl s = new JpushServiceImpl();
Map extra = new HashMap();
extra.put("keyyy", "value");
PushPayload payload = s.buildPushObject("测试信息", "消息测试",
"发送者", Audience.all(),extra,false);
PushResult result = s.jpushClient.sendPush(payload);
}
}