個推開發(fā)者中心地址
服務端大致開發(fā)步驟
1.注冊開發(fā)者賬號;
2.創(chuàng)建應用;
3.查看應用配置, 包含AppID,AppSecret,AppKey,MasterSecret等信息;
4.引入官方依賴,進行服務端開發(fā);
倉庫
<repositories>
<repository>
<id>getui-nexus</id>
<url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
</repository>
</repositories>
依賴
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.0.1.17</version>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-base</artifactId>
<version>4.0.0.22</version>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-template</artifactId>
<version>4.0.0.16</version>
</dependency>
數(shù)據(jù)透傳推送工具類(引用自一篇博客,原文無從查找,侵刪)
public class AppPushUtils {
//定義常量, appId、appKey、masterSecret 在"個推控制臺"中獲得的應用配置
// 由IGetui管理頁面生成,是您的應用與SDK通信的標識之一,每個應用都對應一個唯一的AppID
private static String appId = "";
// 預先分配的第三方應用對應的Key,是您的應用與SDK通信的標識之一。
private static String appKey = "";
// 個推服務端API鑒權碼,用于驗證調用方合法性。在調用個推服務端API時需要提供。(請妥善保管,避免通道被盜用)
private static String masterSecret = "";
// 設置通知消息模板
/*
* 1. appId
* 2. appKey
* 3. 要傳送到客戶端的 msg
* 3.1 標題欄:key = title,
* 3.2 通知欄內容: key = titleText,
* 3.3 穿透內容:key = command
*/
private static TransmissionTemplate getNotifacationTemplate(String appId, String appKey, Map<String, String> msg) {
// 在通知欄顯示一條含圖標、標題等的通知,用戶點擊后激活您的應用
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(msg.get("transText"));
// 設置appid,appkey
template.setAppId(appId);
template.setAppkey(appKey);
// 穿透消息設置為,1 強制啟動應用
template.setTransmissionType(1);
// 設置穿透內容
return template;
}
/**
* 對單個用戶推送消息
*
* @param cid 用戶的clientId
* @param msg 透傳的參數(shù)
* @return
*/
public static IPushResult pushMsgToSingle(String cid, Map<String, String> msg) {
// 代表在個推注冊的一個 app,調用該類實例的方法來執(zhí)行對個推的請求
IGtPush push = new IGtPush(appKey, masterSecret);
// 創(chuàng)建信息模板
TransmissionTemplate template = getNotifacationTemplate(appId, appKey, msg);
//定義消息推送方式為,單推
SingleMessage message = new SingleMessage();
// 設置推送消息的內容
message.setData(template);
//設置過期時間
message.setOfflineExpireTime(24 * 3600 * 1000);
//不限制網(wǎng)絡環(huán)境
message.setPushNetWorkType(0);
// 設置推送目標
Target target = new Target();
target.setAppId(appId);
// 設置cid
target.setClientId(cid);
// 獲得推送結果
IPushResult result = push.pushMessageToSingle(message, target);
/*
* 1. 失?。簕result=sign_error}
* 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
* 3. 異常
*/
return result;
}
/**
* 對多個用戶推送透傳消息
*
* @param cids
* @param msg
* @return
*/
public static IPushResult pushMsgToList(List<String> cids, Map<String, String> msg) {
// 代表在個推注冊的一個 app,調用該類實例的方法來執(zhí)行對個推的請求
IGtPush push = new IGtPush(appKey, masterSecret);
// 創(chuàng)建信息模板
TransmissionTemplate template = getNotifacationTemplate(appId, appKey, msg);
//定義消息推送方式為,多推
ListMessage message = new ListMessage();
// 設置推送消息的內容
message.setData(template);
//獲取ContentId
String contentId = push.getContentId(message);
// 設置推送目標
List<Target> targetList = new ArrayList<>();
Target target;
for (String cid : cids) {
target = new Target();
target.setAppId(appId);
target.setClientId(cid);
targetList.add(target);
}
// 獲得推送結果
IPushResult result = push.pushMessageToList(contentId, targetList);
/*
* 1. 失敗:{result=sign_error}
* 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
* 3. 異常
*/
return result;
}
/**
* 判斷是否推送成功
*
* @param result
* @return
*/
public static boolean isPushSuccess(IPushResult result) {
if (result.getResponse() != null && result.getResponse().get("result").toString().equals("ok")) {
return true;
}
return false;
}