package com.donghui.oa.util;
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 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* @Description //TODO? 極光推送工具類
* @Author wjq
* @time: 2020/9/16 17:22
* 官方示例 https://github.com/jpush/jpush-api-java-client
*/
public class JgPushClientUtil {
? ? private static Logger logger =LoggerFactory.getLogger(JgPushClientUtil.class);
private JPushClient jPushClient;
public JgPushClientUtil(String APP_KEY,String MASTER_SECRET) {
? ? ? ? this.jPushClient =new JPushClient(MASTER_SECRET, APP_KEY,null,ClientConfig.getInstance());
}
? ? public JPushClient getJPushClient(){
? ? ? ? return jPushClient;
}
? ? // 極光單詞最多發(fā)送一千個(gè)用戶
? ? // 將發(fā)用戶集合 分割成 1000個(gè)元素為一組的 二位數(shù)組
? ? private ArrayList<List> allotAliasList(ArrayList<List> arrayLists,List<String> alias,
int listSize,int startIndex,int constantLimitSize){
? ? ? ? if (listSize > constantLimitSize) {
? ? ? ? ? ? List<String> list =new ArrayList<>();
list.addAll(alias.subList(startIndex, startIndex+constantLimitSize));
arrayLists.add(list);
listSize = listSize - constantLimitSize;
startIndex = startIndex + constantLimitSize;
allotAliasList(arrayLists,alias,listSize,startIndex,constantLimitSize);
}else{
? ? ? ? ? ? arrayLists.add(alias.subList(startIndex, startIndex + listSize));
}
? ? ? ? return arrayLists;
}
? ? /**
? ? * 推送給設(shè)備標(biāo)識(shí)參數(shù)的用戶
? ? * @param totalAlias 設(shè)備標(biāo)識(shí)
? ? * @param notification_title 通知內(nèi)容標(biāo)題
? ? * @param msg_title 消息內(nèi)容標(biāo)題
? ? * @param msg_content 消息內(nèi)容
? ? * @param extrasparam 擴(kuò)展字段(通常傳跳轉(zhuǎn)的鏈接)
? ? * @return 0推送失敗苍匆,1推送成功
? ? */
? ? public int sendToRegistrationId(List<String> totalAlias,String notification_title,String msg_title,String msg_content,String extrasparam,Boolean apnsProduction) {
? ? ? ? int result =1;
ArrayList<List> lists =null;
try {
? ? ? ? ? ? lists = allotAliasList(new ArrayList<List>(), totalAlias, totalAlias.size(),0,1000);
}catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
}
? ? ? ? for (List alias : lists) {
? ? ? ? ? ? PushPayload pushPayload= buildPushObject_all_alias_alertWithTitle(alias,notification_title,msg_title,msg_content,extrasparam,apnsProduction);
try {
? ? ? ? ? ? ? ? PushResult pushResult=jPushClient.sendPush(pushPayload);
if(pushResult.getResponseCode()==200){
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? result =0;
}
? ? ? ? ? ? ? ? logger.info("[極光推送]PushResult result is " +pushResult);
} catch (APIConnectionException e) {
? ? ? ? ? ? ? ? result =0;
logger.error("[極光推送]Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
? ? ? ? ? ? ? ? result =0;
logger.error("[極光推送]Error response from JPush server. Should review and fix it. ", e);
logger.info("[極光推送]HTTP Status: " + e.getStatus());
logger.info("[極光推送]Error Code: " + e.getErrorCode());
logger.info("[極光推送]Error Message: " + e.getErrorMessage());
}
}
? ? ? ? jPushClient.close();
return result;
}
? ? private PushPayload buildPushObject_all_alias_alertWithTitle(List<String> alias,String notification_title,String msg_title,String msg_content,String extrasparam,Boolean apnsProduction) {
? ? ? ? return PushPayload.newBuilder()
? ? ? ? ? ? ? ? //指定要推送的平臺(tái),all代表當(dāng)前應(yīng)用配置了的所有平臺(tái)团赏,也可以傳android等具體平臺(tái)
? ? ? ? ? ? ? ? .setPlatform(Platform.all())
? ? ? ? ? ? ? ? //指定推送的接收對(duì)象留凭,all代表所有人返干,也可以指定已經(jīng)設(shè)置成功的tag或alias或該應(yīng)應(yīng)用客戶端調(diào)用接口獲取到的registration id
? ? ? ? ? ? ? ? .setAudience(Audience.alias(alias))
? ? ? ? ? ? ? ? //.setAudience(Audience.registrationId(registrationId))
? ? ? ? ? ? ? ? //jpush的通知掠械,android的由jpush直接下發(fā),iOS的由apns服務(wù)器下發(fā)汗盘,Winphone的由mpns下發(fā)
? ? ? ? ? ? ? ? .setNotification(Notification.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? //指定當(dāng)前推送的android通知
? ? ? ? ? ? ? ? ? ? ? ? .addPlatformNotification(AndroidNotification.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setAlert(msg_content)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setTitle(notification_title)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此字段為透?jìng)髯侄沃宓猓粫?huì)顯示在通知欄。用戶可以通過此字段來做一些定制需求隐孽,如特定的key傳要指定跳轉(zhuǎn)的頁面(value)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .addExtra("param", extrasparam)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? ? ? ? ? //指定當(dāng)前推送的iOS通知
? ? ? ? ? ? ? ? ? ? ? ? .addPlatformNotification(IosNotification.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //傳一個(gè)IosAlert對(duì)象癌椿,指定apns title、title菱阵、subtitle等
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setAlert(msg_content)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //直接傳alert
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此項(xiàng)是指定此推送的badge自動(dòng)加1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .incrBadge(1)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此字段的值default表示系統(tǒng)默認(rèn)聲音踢俄;傳sound.caf表示此推送以項(xiàng)目里面打包的sound.caf聲音來提醒,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 如果系統(tǒng)沒有此音頻則以系統(tǒng)默認(rèn)聲音提醒晴及;此字段如果傳空字符串褪贵,iOS9及以上的系統(tǒng)是無聲音提醒,以下的系統(tǒng)是默認(rèn)聲音
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setSound("sound.caf")
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此字段為透?jìng)髯侄慰苟恚粫?huì)顯示在通知欄。用戶可以通過此字段來做一些定制需求世舰,如特定的key傳要指定跳轉(zhuǎn)的頁面(value)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .addExtra("param", extrasparam)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //此項(xiàng)說明此推送是一個(gè)background推送动雹,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //取消此注釋,消息推送時(shí)ios將無法在鎖屏情況接收
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // .setContentAvailable(true)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? // Platform指定了哪些平臺(tái)就會(huì)像指定平臺(tái)中符合推送條件的設(shè)備進(jìn)行推送跟压。 jpush的自定義消息胰蝠,
? ? ? ? ? ? ? ? // sdk默認(rèn)不做任何處理,不會(huì)有通知提示震蒋。建議看文檔http://docs.jpush.io/guideline/faq/的
? ? ? ? ? ? ? ? // [通知與自定義消息有什么區(qū)別茸塞?]了解通知和自定義消息的區(qū)別
? ? ? ? ? ? ? ? .setMessage(Message.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? .setMsgContent(msg_content)
? ? ? ? ? ? ? ? ? ? ? ? .setTitle(msg_title)
? ? ? ? ? ? ? ? ? ? ? ? .addExtra("url", extrasparam)
? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? .setOptions(Options.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? //此字段的值是用來指定本推送要推送的apns環(huán)境,false表示開發(fā)查剖,true表示生產(chǎn)钾虐;對(duì)android和自定義消息無意義
? ? ? ? ? ? ? ? ? ? ? ? .setApnsProduction(apnsProduction)
? ? ? ? ? ? ? ? ? ? ? ? //此字段是給開發(fā)者自己給推送編號(hào),方便推送者分辨推送記錄
? ? ? ? ? ? ? ? ? ? ? ? .setSendno(1)
? ? ? ? ? ? ? ? ? ? ? ? //此字段的值是用來指定本推送的離線保存時(shí)長(zhǎng)笋庄,如果不傳此字段則默認(rèn)保存一天效扫,最多指定保留十天倔监;
? ? ? ? ? ? ? ? ? ? ? ? .setTimeToLive(86400)
? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? .build();
}
? ? public PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
? ? ? ? return PushPayload.newBuilder()
? ? ? ? ? ? ? ? .setPlatform(Platform.android_ios())
? ? ? ? ? ? ? ? .setAudience(Audience.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? .addAudienceTarget(AudienceTarget.tag("2787"))
? ? ? ? ? ? ? ? ? ? ? ? .addAudienceTarget(AudienceTarget.alias("2787"))
? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? .setMessage(Message.newBuilder()
? ? ? ? ? ? ? ? ? ? ? ? .setMsgContent("MSG_CONTENT")
? ? ? ? ? ? ? ? ? ? ? ? .addExtra("from","JPush")
? ? ? ? ? ? ? ? ? ? ? ? .build())
? ? ? ? ? ? ? ? .build();
}
? ? public PushPayload buildPushObject_all_all_alert() {
? ? ? ? return PushPayload.alertAll("Test from API Example - alert");
}
? ? public PushPayload buildPushObject_all_alias_alert() {
? ? ? ? return PushPayload.newBuilder()
? ? ? ? ? ? ? ? .setPlatform(Platform.all())
? ? ? ? ? ? ? ? .setAudience(Audience.alias("2787"))
? ? ? ? ? ? ? ? .setNotification(Notification.alert("Test from API Example - alert"))
? ? ? ? ? ? ? ? .build();
}
? ? public static void main(String[] args) {
? ? ? ? JgPushClientUtil jgPushClientUtil =new JgPushClientUtil("d74310dd52bf5e59e67fa29e","5850bd282763077e38362588");
ArrayList<String> list =new ArrayList<>();
list.add("354");
jgPushClientUtil.sendToRegistrationId(list,"title","msg_title","中秋通知",
"{\"business_code\":\"100\",\"data\":{\"id\":"+1+"}}",false);
//? ? ? ? List alias = new ArrayList<>();
//? ? ? ? for (int i = 0; i < 11; i++) {
//? ? ? ? ? ? alias.add(i+"");
//? ? ? ? }
//
//? ? ? ? ArrayList lists = jgPushClientUtil.allotAliasList(new ArrayList(), alias,
//? ? ? ? ? ? ? ? alias.size(), 0, 10);
//
//? ? ? ? System.out.println(lists);
? ? }
}