|
@@ -0,0 +1,182 @@
|
|
|
+package com.shawn.util;
|
|
|
+
|
|
|
+import com.gexin.rp.sdk.base.impl.SingleMessage;
|
|
|
+import com.gexin.rp.sdk.base.impl.Target;
|
|
|
+import com.gexin.rp.sdk.base.payload.APNPayload;
|
|
|
+import com.gexin.rp.sdk.exceptions.RequestException;
|
|
|
+import com.gexin.rp.sdk.http.IGtPush;
|
|
|
+import com.gexin.rp.sdk.template.TransmissionTemplate;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by dinfeng on 2017/12/21
|
|
|
+ * 个推工具类
|
|
|
+ */
|
|
|
+public class PushUtils {
|
|
|
+
|
|
|
+
|
|
|
+ public final static String appId = "GKa6qa12heALjEXZlAn1U3";
|
|
|
+
|
|
|
+ public final static String appKey = "fLvPjR8hni7VFMkgjh8lx2";
|
|
|
+
|
|
|
+ public final static String masterSecret = "KjxrC6vTLr5wiZu55cCnS8";
|
|
|
+
|
|
|
+ public final static String host = "http://sdk.open.api.igexin.com/apiex.htm";
|
|
|
+
|
|
|
+ public static String test_clientId = "c69869085580f3b77f2972403fbdd5b3";
|
|
|
+ public static String test_clientId2 = "200cb24ae7af3c4096cc2c46569ed530";
|
|
|
+
|
|
|
+
|
|
|
+ final static IGtPush push = new IGtPush(host, appKey, masterSecret);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 个推发送信息
|
|
|
+ *
|
|
|
+ * @param clientIds 设备号数组
|
|
|
+ * @param title
|
|
|
+ * @param information 信息
|
|
|
+ * @param json 业务id
|
|
|
+ */
|
|
|
+ public static void push(List<String> clientIds, String title, String information, String json) {
|
|
|
+
|
|
|
+
|
|
|
+ TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
|
|
|
+ //推送ios
|
|
|
+ final SingleMessage iMessage = new SingleMessage();
|
|
|
+ iMessage.setOffline(true);
|
|
|
+ // 离线有效时间,单位为毫秒,可选
|
|
|
+ iMessage.setOfflineExpireTime(24 * 3600 * 1000);
|
|
|
+ iMessage.setData(transmissionTemplate);
|
|
|
+ // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
|
|
|
+ iMessage.setPushNetWorkType(0);
|
|
|
+
|
|
|
+ //个推
|
|
|
+ for (String clientId : clientIds) {
|
|
|
+ final Target target = new Target();
|
|
|
+ target.setAppId(appId);
|
|
|
+ target.setClientId(clientId);
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ new Thread(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ push.pushMessageToSingle(iMessage, target);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }).start();
|
|
|
+
|
|
|
+ } catch (RequestException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ push.pushMessageToSingle(iMessage, target, e.getRequestId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 个推发送信息
|
|
|
+ *
|
|
|
+ * @param clientId 设备号数组
|
|
|
+ * @param information 信息
|
|
|
+ * @param json 业务id
|
|
|
+ */
|
|
|
+ public static void push(String clientId, String title, String information, String json) {
|
|
|
+
|
|
|
+
|
|
|
+ TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
|
|
|
+ //推送ios
|
|
|
+ final SingleMessage iMessage = new SingleMessage();
|
|
|
+ iMessage.setOffline(true);
|
|
|
+ // 离线有效时间,单位为毫秒,可选
|
|
|
+ iMessage.setOfflineExpireTime(24 * 3600 * 1000);
|
|
|
+ iMessage.setData(transmissionTemplate);
|
|
|
+ // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
|
|
|
+ iMessage.setPushNetWorkType(0);
|
|
|
+
|
|
|
+
|
|
|
+ //个推
|
|
|
+ final Target target = new Target();
|
|
|
+ target.setAppId(appId);
|
|
|
+ target.setClientId(clientId);
|
|
|
+ try {
|
|
|
+
|
|
|
+ new Thread(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ push.pushMessageToSingle(iMessage, target);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }).start();
|
|
|
+
|
|
|
+ } catch (RequestException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ push.pushMessageToSingle(iMessage, target, e.getRequestId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 苹果使用透传消息,通过设置payload信息,实现待机时的推送提醒;
|
|
|
+ * 对安卓来说就是没有payload的概念,所以需要去解析透传的消息,把title和body放在透传消息中
|
|
|
+ */
|
|
|
+ private static TransmissionTemplate getTemplate(String title, String msg, String json) {
|
|
|
+ title = title == null ? "" : title;
|
|
|
+ msg = msg == null ? "" : msg;
|
|
|
+ TransmissionTemplate template = new TransmissionTemplate();
|
|
|
+ template.setAppId(appId);
|
|
|
+ template.setAppkey(appKey);
|
|
|
+ template.setTransmissionContent(json);
|
|
|
+ template.setTransmissionType(2);
|
|
|
+ APNPayload payload = new APNPayload();
|
|
|
+ //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
|
|
|
+ payload.setAutoBadge("+1");
|
|
|
+ payload.setContentAvailable(0);
|
|
|
+ payload.setSound("default");
|
|
|
+ payload.setCategory("$由客户端定义");
|
|
|
+ payload.addCustomMsg("payload",json);//好假
|
|
|
+
|
|
|
+ //字典模式使用APNPayload.DictionaryAlertMsg
|
|
|
+ //payload.setAlertMsg(getDictionaryAlertMsg(msg, title));
|
|
|
+
|
|
|
+ template.setAPNInfo(payload);
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String msg, String title) {
|
|
|
+ APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
|
|
|
+ alertMsg.setBody(msg);//通知小内容
|
|
|
+ alertMsg.setActionLocKey("ActionLockey");
|
|
|
+ alertMsg.setLocKey("LocKey");
|
|
|
+ alertMsg.addLocArg("loc-args");
|
|
|
+ alertMsg.setLaunchImage("launch-image");
|
|
|
+
|
|
|
+ // iOS8.2以上版本支持
|
|
|
+ alertMsg.setTitle(title);//通知标题
|
|
|
+ alertMsg.setTitleLocKey("");
|
|
|
+ alertMsg.addTitleLocArg("");
|
|
|
+ return alertMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static JSONObject buildJson(String kind, String kindData) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ if (kind != null) {
|
|
|
+ jsonObject.put("kind", kind);
|
|
|
+ jsonObject.put("kind_data", kindData);
|
|
|
+ }else{
|
|
|
+ jsonObject.put("kind", "");
|
|
|
+ jsonObject.put("kind_data", "");
|
|
|
+ }
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ push("71963777bd63e0f0a5b6490f04bf1ae4", "niai", "123", buildJson("editNo", "123456789").toString());
|
|
|
+ }
|
|
|
+}
|