com.luues.push.JG.JGPushUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-mobile-push Show documentation
Show all versions of commons-mobile-push Show documentation
A simple package of IOS and Aurora push
package com.luues.push.JG;
import java.util.Collection;
import java.util.Map;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.notification.IosNotification;
import org.apache.log4j.Logger;
import cn.jpush.api.push.model.Message;
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.Notification;
/**
* 极光推送
*/
public class JGPushUtil {
private static Logger logger = Logger.getLogger(JGPushUtil.class.getName());
private static JGPushUtil jgPushUtil;
private static JPushClient jPushClient;
private JGPushUtil(){}
public static JGPushUtil getInstance(){
if(null == jgPushUtil){
jgPushUtil = new JGPushUtil();
jPushClient = new JPushClient("5cdb9c9efa0c7bb71f1709df", "563286250b193b1acc02236b", 3);
}
return jgPushUtil;
}
public void sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
jPushClient.sendPush(pushPayload);
}
//快捷地构建推送对象:所有平台,所有设备,内容为 ALERT 的通知
public PushPayload buildPushObject_all_all_alert(String alert) {
return PushPayload.alertAll(alert);
}
//构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT
public PushPayload buildPushObject_all_alias_alert(String user, String alert) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(user))
.setNotification(Notification.alert(alert))
.build();
}
//构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE
public PushPayload buildPushObject_android_tag_alertWithTitle(String tag, String title, String alert) {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.tag(tag))
.setNotification(Notification.android(alert, title, null))
.build();
}
//构建推送对象:平台是 Android,注册id 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE
public PushPayload buildPushObject_android_regisId_alertWithTitle(String title, String alert, Map map, Collection ids) {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.registrationId(ids))
.setMessage(Message.newBuilder()
.setTitle(title)
.setMsgContent(alert)
.addExtras(map)
.build())
.build();
}
/**
* 构建推送对象:平台是 iOS,推送目标是 "tag1", "tag_all" 的并集,
* 推送内容同时包括通知与消息 - 通知信息是 ALERT,角标数字为 5,通知声音为
* "happy",并且附加字段 from = "JPush";消息内容是 MSG_CONTENT
* 。通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。APNs
* 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)
*
* @return
*/
public PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String alert, String content, String... user) {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.tag_and(user))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(alert)
.setBadge(5)
.setSound("happy")
.addExtra("from", "qty")
.build())
.build())
.setMessage(Message.content(content))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
}
/**
* 构建推送对象:平台是 Andorid 与 iOS,推送目标是 ("tag1" 与 "tag2" 的交集)并("alias1" 与 "alias2" 的交集),推送内容是 - 内容为 MSG_CONTENT 的消息,并且附加字段 from = JPush。
*
* @return
*/
public PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String content) {
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(content)
.addExtra("from", "JPush")
.build())
.build();
}
}