個推推送專案實用(二)
阿新 • • 發佈:2018-12-31
第二步:直接上程式碼,不廢話,有問題聯絡我留言
宣告:個推裡面的demo程式碼應用到實際專案中,需要改編成工具類!!!
需要配置的Maven包
<!--訊息推送-->
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.0.1.7</version>
</dependency>
<dependency >
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-template</artifactId>
<version>4.0.0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-template</artifactId>
<version>4.0.0.6</version>
</dependency >
工具類程式碼:
package cn.xdf.api.business.app.notice.common;
import cn.xdf.api.business.manager.common.entity.AppMsgDevice;
import cn.xdf.api.business.manager.common.entity.AppMsgInfo;
import cn.xdf.api.common.BusinessConstant;
import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 訊息推送工具類
*/
public class NoticeUtil {
private static Logger log = LoggerFactory.getLogger(NoticeUtil.class);
private static final String IOS = "iOS";
private static final String ANDROID = "Android";
public static void main(String[] args) throws Exception {
}
/**
* 傳送訊息至所有安裝app使用者
* 通過clientid 傳送
*/
public static IPushResult pushMsgToAll(AppMsgInfo appMsgInfo){
if(StringUtils.isEmpty(appMsgInfo)){
log.error("in NoticeUtil#pushMsgToUsers method,appMsgInfo is null");
return null;
}
System.setProperty("gexin_pushList_needDetails", "true");
IGtPush push = new IGtPush(BusinessConstant.geTuiHost, BusinessConstant.appKey, BusinessConstant.masterSecret);
AppMessage message = new AppMessage();
// 通知透傳模板
TransmissionTemplate template = getTransmissionTemplate(appMsgInfo);
message.setData(template);
message.setOffline(true);
//離線有效時間,單位為毫秒,可選
message.setOfflineExpireTime(24 * 1000 * 3600);
//推送給App的目標使用者需要滿足的條件
AppConditions cdt = new AppConditions();
List<String> appIdList = new ArrayList<String>();
appIdList.add(BusinessConstant.appId);
message.setAppIdList(appIdList);
message.setConditions(cdt);
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
return ret;
}
/**
* 分平臺傳送訊息至app使用者 Android、iOS
* 通過clientid 傳送
*/
public static IPushResult pushMsgByType(AppMsgInfo appMsgInfo,String type){
if(StringUtils.isEmpty(appMsgInfo) || !StringUtils.hasText(type)){
log.error("in NoticeUtil#pushMsgToUsers method,appMsgInfo or type is null");
return null;
}
System.setProperty("gexin_pushList_needDetails", "true");
IGtPush push = new IGtPush(BusinessConstant.geTuiHost, BusinessConstant.appKey, BusinessConstant.masterSecret);
AppMessage message = new AppMessage();
TransmissionTemplate template = getTransmissionTemplate(appMsgInfo);
message.setData(template);
message.setOffline(true);
//離線有效時間,單位為毫秒,可選
message.setOfflineExpireTime(24 * 1000 * 3600);
//推送給App的目標使用者需要滿足的條件
AppConditions cdt = new AppConditions();
List<String> appIdList = new ArrayList<String>();
appIdList.add(BusinessConstant.appId);
message.setAppIdList(appIdList);
//手機型別
List<String> phoneTypeList = new ArrayList<String>();
phoneTypeList.add(type.toUpperCase());// Android/iOS
cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
message.setConditions(cdt);
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
return ret;
}
/**
* 傳送訊息至指定app使用者
* 通過clientid 傳送
*/
public static IPushResult pushMsgToUsers(AppMsgInfo appMsgInfo, List<AppMsgDevice> appMsgDeviceList){
if(appMsgDeviceList==null || appMsgDeviceList.size()==0 || StringUtils.isEmpty(appMsgInfo)){
log.error("in NoticeUtil#pushMsgToUsers method,appMsgInfo or clientList is null");
return null;
}
System.setProperty("gexin_pushList_needDetails", "true");
List<Target> list = getTargets(appMsgDeviceList);
// 配置返回每個別名及其對應cid的使用者狀態,可選
IGtPush push = new IGtPush(BusinessConstant.geTuiHost, BusinessConstant.appKey, BusinessConstant.masterSecret);
ListMessage message = new ListMessage();
TransmissionTemplate template = getTransmissionTemplate(appMsgInfo);
message.setData(template);
// 設定訊息離線,並設定離線時間
message.setOffline(true);
// 離線有效時間,單位為毫秒,可選
message.setOfflineExpireTime(24 * 1000 * 3600);
// 配置推送目標
String taskId = push.getContentId(message);
IPushResult ret = push.pushMessageToList(taskId, list);
System.out.println(ret.getResponse().toString());
return ret;
}
public static LinkTemplate getLinkTemplate(AppMsgInfo appMsgInfo){
LinkTemplate template = new LinkTemplate();
template.setAppId(BusinessConstant.appId);
template.setAppkey(BusinessConstant.appKey);
// 設定通知欄標題與內容
template.setTitle(appMsgInfo.getTitle());//推送標題
template.setText(appMsgInfo.getContent());//推送內容
// 配置通知欄圖示
template.setLogo("");
// 配置通知欄網路圖示
template.setLogoUrl("");
// 設定通知是否響鈴,震動,或者可清除
template.setIsRing(true);
template.setIsVibrate(true);
template.setIsClearable(true);
template.setUrl(appMsgInfo.getLink());//推送url路徑
return template;
}
public static TransmissionTemplate getTransmissionTemplate(AppMsgInfo appMsgInfo){
TransmissionTemplate template = new TransmissionTemplate ();
template.setAppId(BusinessConstant.appId);
template.setAppkey(BusinessConstant.appKey);
template.setTransmissionContent(JSONObject.toJSON(appMsgInfo).toString());
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
//在已有數字基礎上加1顯示,設定為-1時,在已有數字上減1顯示,設定為數字時,顯示指定數字
payload.setAutoBadge("+1");
payload.setContentAvailable(1);
payload.setSound("default");
payload.setCategory("$由客戶端定義");
//簡單模式APNPayload.SimpleMsg
// payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
//自定義型別引數
payload.addCustomMsg("type", appMsgInfo.getSendType());//推送型別1、全部,2android,3iOS,4使用者Id
payload.addCustomMsg("link", appMsgInfo.getLink());//推送url路徑
//字典模式使用APNPayload.DictionaryAlertMsg
payload.setAlertMsg(getDictionaryAlertMsg(appMsgInfo));
// 新增多媒體資源
/* payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
.setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
.setOnlyWifi(true));*/
template.setAPNInfo(payload);
return template;
}
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(AppMsgInfo appMsgInfo){
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setTitle(appMsgInfo.getTitle());//推送標題
alertMsg.setBody(appMsgInfo.getContent());//推送內容
alertMsg.setActionLocKey("ActionLockey");
alertMsg.setLocKey("LocKey");
alertMsg.addLocArg("loc-args");
alertMsg.setLaunchImage("launch-image"); // iOS8.2以上版本支援 alertMsg.setTitle("Title");
alertMsg.setTitleLocKey("TitleLocKey");
alertMsg.addTitleLocArg("TitleLocArg");
return alertMsg;
}
public static List<Target> getTargets(List<AppMsgDevice> appMsgDeviceList){
List list = new ArrayList();
for(int i=0;i<appMsgDeviceList.size();i++){
Target temp = new Target();
temp.setAppId(BusinessConstant.appId);
temp.setClientId(appMsgDeviceList.get(i).getClientId());
list.add(temp);
}
return list;
}
}