1. 工具
package p02;
import java.util.Map;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
/*
* 個推工具包
* (peerslee, 2017.2.12)
*/
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 = "";
// 構造器
public AppPushUtils(String appId, String appKey, String masterSecret) {
// 初始化類
this.appId = appId;
this.appKey = appKey;
this.masterSecret = masterSecret;
}
// 設定通知消息模闆
/*
* 1. appId
* 2. appKey
* 3. 要傳送到用戶端的 msg
* 3.1 标題欄:key = title,
* 3.2 通知欄内容: key = titleText,
* 3.3 穿透内容:key = transText
*/
private static NotificationTemplate getNotifacationTemplate(String appId, String appKey, Map<String, String> msg){
// 在通知欄顯示一條含圖示、标題等的通知,使用者點選後激活您的應用
NotificationTemplate template = new NotificationTemplate();
// 設定appid,appkey
template.setAppId(appId);
template.setAppkey(appKey);
// 穿透消息設定為,1 強制啟動應用
template.setTransmissionType(1);
// 設定穿透内容
System.out.println(msg.get("title") + "::" + msg.get("titleText") + "::" + msg.get("transText"));
template.setTransmissionContent(msg.get("transText"));
// 設定style
Style0 style = new Style0();
// 設定通知欄标題和内容
style.setTitle(msg.get("title"));
style.setText(msg.get("titleText"));
// 設定通知,響鈴、震動、可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
// 設定
template.setStyle(style);
return template;
}
// 對單個使用者推送消息
/*
* 1. cid
* 2. 要傳到用戶端的 msg
* 2.1 标題欄:key = title,
* 2.2 通知欄内容: key = titleText,
* 2.3 穿透内容:key = transText
*/
public IPushResult pushMsgToSingle(String cid, Map<String, String> msg) {
// 代表在個推注冊的一個 app,調用該類執行個體的方法來執行對個推的請求
IGtPush push = new IGtPush(appKey, masterSecret);
// 建立資訊模闆
NotificationTemplate template = getNotifacationTemplate(appId, appKey, msg);
//定義消息推送方式為,單推
SingleMessage message = new SingleMessage();
// 設定推送消息的内容
message.setData(template);
// 設定推送目标
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;
}
}
2. 測試
package p02;
import java.util.HashMap;
import java.util.Map;
import com.gexin.rp.sdk.base.IPushResult;
public class AppPushTest {
public static void main(String[] args) {
String appId = "EHFx0RmBuL9iuvh05WxJq9";
String appKey = "T8X5ijnhO8ACHWCPRphrO8";
String masterSecret = "hvzdfG6ZSh8uo2N7eTNqq8";
Map<String, String> msg = new HashMap<>();
msg.put("title", "peerslee, 新年快樂!");
msg.put("titleText", "O(∩_∩)O~,");
msg.put("transText", "");
String []cids = {};
AppPushUtils pushUtils = new AppPushUtils(appId, appKey, masterSecret);
for(String cid : cids) {
System.out.println("正在發送消息...");
IPushResult ret = pushUtils.pushMsgToSingle(cid, msg);
System.out.println(ret.getResponse().toString());
}
}
}
3.