最近App需要集成消息推送,公司最终采用了极光,做一下工具类,可以直接粘贴使用
极光官方文档:https://docs.jiguang.cn//jpush/server/server_sdk/
maven 包
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.6</version>
</dependency>
工具类:
1.函数接口
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.util.List;
/**
* @author zqh
*/
@FunctionalInterface
public interface IJiGuangPushPayloadFuction {
/**
* 初始化需要使用的函数
*
* @param jiGuangPushPayloadFuction
* @return
*/
PushPayload init(IJiGuangPushPayloadFuction jiGuangPushPayloadFuction);
/**
* 快捷地构建推送对象:所有平台,所有设备,内容为 ALERT 的通知。
*/
default PushPayload buildPushObject_all_all_alert(String alert) {
return PushPayload.alertAll(alert);
}
/**
* 构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT
*/
default PushPayload buildPushObject_all_alias_alert(String alias, String alert) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(alias))
.setNotification(Notification.alert(alert))
.build();
}
/**
* 构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT,单独设置android 推送消息等级
*
*/
default PushPayload buildPushObject_all_alias_priority_alert(String alias, String alert) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).setPriority(1).build()
)
.addPlatformNotification(IosNotification.newBuilder().setAlert(alert).build())
.build())
.build();
}
/**
* 构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE
*
* @return
*/
default PushPayload buildPushObject_android_tag_alertWithTitle(String tag, String alert, String title) {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.tag(tag))
.setNotification(Notification.android(alert, title, null))
.build();
}
/**
* 构建推送对象:平台是 iOS,推送目标是 "tag1", "tag_all" 的交集,推送内容同时包括通知与消息 - 通知信息是 ALERT,角标数字为 5,通知声音为 "happy",并且附加字段 from = "JPush";消息内容是 MSG_CONTENT。通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)
*
* @return
*/
default PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(List<String> tags, String alert, String msgContent) {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.tag_and(tags))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(alert)
.setBadge(5)
.setSound("happy")
.addExtra("from", "JPush")
.build())
.build())
.setMessage(Message.content(msgContent))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
}
/**
* 构建推送对象:平台是 Andorid 与 iOS,推送目标是 ("tag1" 与 "tag2" 的并集)交("alias1" 与 "alias2" 的并集),推送内容是 - 内容为 MSG_CONTENT 的消息,并且附加字段 from = JPush。
*
* @return
*/
default PushPayload buildPushObject_ios_audienceMore_messageWithExtras(List<String> tags, List<String> aliass, String msgContent) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.tag(tags))
.addAudienceTarget(AudienceTarget.alias(aliass))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(msgContent)
.addExtra("from", "JPush")
.build())
.build();
}
}
2.测试类
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author zqh
*/
public class JiGuangPushUtils {
private final static Logger logger = LoggerFactory.getLogger(JiGuangPushUtils.class);
/**
* 填写你申请的masterSecret和appKey
*/
static JPushClient jpushClient = new JPushClient("masterSecret", "appKey", null, ClientConfig.getInstance());
public static void pushMsg(IJiGuangPushPayloadFuction jiGuangPushPayloadFuction) {
try {
System.out.println(jiGuangPushPayloadFuction);
PushResult result = jpushClient.sendPush(jiGuangPushPayloadFuction.init(jiGuangPushPayloadFuction));
logger.info("Got result - " + result);
} catch (APIConnectionException e) {
logger.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
logger.error("Should review the error, and fix the request", e);
logger.info("HTTP Status: " + e.getStatus());
logger.info("Error Code: " + e.getErrorCode());
logger.info("Error Message: " + e.getErrorMessage());
}
}
/**
* 测试
*/
public static void main(String[] args) {
pushMsg((jiGuangPushPayloadFuction) -> jiGuangPushPayloadFuction.buildPushObject_all_all_alert("测试"));
pushMsg((jiGuangPushPayloadFuction) -> jiGuangPushPayloadFuction.buildPushObject_all_alias_alert("alias","测试"));
pushMsg((jiGuangPushPayloadFuction) -> jiGuangPushPayloadFuction.buildPushObject_android_tag_alertWithTitle("tag", "alert", "title"));
}
}