使用個推實現訊息推送到客戶端
由於公司業務需要,也得學習一下個推的使用,在此記錄一下。
2、註冊完之後登入進去,之後你會獲得
appId 、appKey 、masterSecret
快速入門:
private static String appId = "";
private static String appKey = "";
private static String masterSecret = "";
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
IGtPush push = new IGtPush(url, appKey, masterSecret);
// 定義"點選連結開啟通知模板",並設定標題、內容、連結
LinkTemplate template = new LinkTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTitle("歡迎使用個推!");
template.setText("這是一條推送訊息~");
template.setUrl("http://getui.com");
List<String> appIds = new ArrayList<String>();
appIds.add(appId);
// 定義"AppMessage"型別訊息物件,設定訊息內容模板、傳送的目標App列表
// 、是否支援離線傳送、以及離線訊息有效期(單位毫秒)
AppMessage message = new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(1000 * 600);
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
這樣你就實現了一個簡單的推送。
maven 專案實現個推推送訊息
配置maven 專案的pom.xml檔案
<!-- getui -->
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-fastjson</artifactId>
<version>1.0.0.1</version>
</dependency>
<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-template</artifactId>
<version>4.0.0.16</version>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-base</artifactId>
<version>4.0.0.22</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
本地倉庫若是沒有上述jar包,可以使用命令讓jar包配到倉庫裡面,cmd命令。 mvn install:install-file -DgroupId=com.alipay -DartifactId=alipay-sdk -Dversion=20171026141113 -Dpackaging=jar -Dfile=alipay-sdk-java20171026141113.jar,把相應的引數替換一下就可以了。
這裡我提供一個個推工具類:
import java.util.ArrayList;
import java.util.List;
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
public class GetuiUtil {
static String appId = "";
static String appKey = "";
static String masterSecret = "";
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
static IGtPush push;
static {
push = new IGtPush(host, appKey, masterSecret);
}
/**
* 繫結使用者cid 別名
*
* @param Alias
* @param CID
* @return
*/
public static boolean bindAlias(String alias, String CID) {
IAliasResult bindSCid = push.bindAlias(appId, alias, CID);
if (bindSCid.getResult()) {
return true;
}
return false;
}
/**
* 對單個使用者推送訊息
* @param alias
* @param msg
* @return
*/
public static boolean pushMessageToSingle(String cid, String text, String transMsg) {
IGtPush push = new IGtPush(host, appKey, masterSecret);
NotificationTemplate template = notificationTemplate("title", text, transMsg);
SingleMessage message = new SingleMessage();
message.setOffline(true);
// 離線有效時間,單位為毫秒,可選
message.setOfflineExpireTime(24 * 3600 * 1000);
message.setData(template);
// 可選,1為wifi,0為不限制網路環境。根據手機處於的網路情況,決定是否下發
message.setPushNetWorkType(0);
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
//target.setAlias(Alias);
IPushResult ret = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
System.out.println(ret.getResponse().toString());
if(ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")){
return true;
}
}
return false;
}
/**
* 指定應用的所有使用者群發推送訊息
* @param msg
*/
public static boolean pushtoAPP (String text, String transMsg){
IGtPush push = new IGtPush(host, appKey, masterSecret);
NotificationTemplate template = notificationTemplate("title", text, transMsg);
AppMessage message = new AppMessage();
message.setData(template);
message.setOffline(true);
//離線有效時間,單位為毫秒,可選
message.setOfflineExpireTime(24 * 1000 * 3600);
//推送給App的目標使用者需要滿足的條件
AppConditions cdt = new AppConditions();
List<String> appIdList = new ArrayList<String>();
appIdList.add(appId);
message.setAppIdList(appIdList);
//手機型別
List<String> phoneTypeList = new ArrayList<String>();
//省份
List<String> provinceList = new ArrayList<String>();
//自定義tag
List<String> tagList = new ArrayList<String>();
cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
cdt.addCondition(AppConditions.REGION, provinceList);
cdt.addCondition(AppConditions.TAG,tagList);
message.setConditions(cdt);
IPushResult ret = push.pushMessageToApp(message,"msg_toApp");
if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
System.out.println(ret.getResponse().toString());
if(ret.getResponse().get("result").toString().equals("ok")){
return true;
}
}
return false;
}
/**
* 對單個使用者推送透傳訊息
*
* @param alias
* @param title
* @param content
* @return
*/
public static boolean pushTransMessageToSingle(String cid, String msg) {
TransmissionTemplate template = transTemplate(cid, msg);
SingleMessage message = new SingleMessage();
message.setOffline(true);
// 離線有效時間,單位為毫秒,可選
message.setOfflineExpireTime(24 * 3600 * 1000);
message.setData(template);
// 可選,1為wifi,0為不限制網路環境。根據手機處於的網路情況,決定是否下發
message.setPushNetWorkType(0);
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
IPushResult ret = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
System.out.println(ret.getResponse().toString());
if(ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")){
return true;
}
}
return false;
}
public static void pushMessageToIBatch(List<String> alias, String msg) {
IBatch batch = push.getBatch();
IPushResult ret = null;
try {
for (int i = 0; i < alias.size(); i++) {
// 構建客戶a的透傳訊息a
constructClientTransMsg(alias.get(i), msg, batch);
}
// 構建客戶B的點選通知開啟網頁訊息b
// constructClientLinkMsg(CID_B,"msgB",batch);
ret = batch.submit();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(ret.getResponse().toString());
}
private static NotificationTemplate notificationTemplate(String title, String text, String obj) {
NotificationTemplate template = new NotificationTemplate();
// 設定APPID與APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
// 透傳訊息設定,1為強制啟動應用,客戶端接收到訊息後就會立即啟動應用;2為等待應用啟動
template.setTransmissionType(1);
template.setTransmissionContent(obj);
// 設定定時展示時間
// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
Style0 style = new Style0();
// 設定通知欄標題與內容
style.setTitle(title);
style.setText(text);
// 配置通知欄圖示
style.setLogo("XXX");
// 配置通知欄網路圖示
//style.setLogoUrl("");
// 設定通知是否響鈴,震動,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
return template;
}
private static TransmissionTemplate transTemplate(String cid, String msg) {
TransmissionTemplate template = new TransmissionTemplate();
// 設定APPID與APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(msg);
template.setTransmissionType(0); // 這個Type為int型,填寫1則自動啟動app
return template;
}
private static void constructClientTransMsg(String alias, String msg, IBatch batch) throws Exception {
SingleMessage message = new SingleMessage();
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(msg);
template.setTransmissionType(0); // 這個Type為int型,填寫1則自動啟動app
message.setPushNetWorkType(0);
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(24 * 3600 * 1000);
// 設定推送目標,填入appid和alias
Target target = new Target();
target.setAppId(appId);
target.setAlias(alias);
batch.add(message, target);
}
}
然後在你的服務端呼叫工具類裡面相應的方法傳上相應引數的值就可以了。這裡只記錄服務端的。如有bug,歡迎指正~