|
@@ -0,0 +1,87 @@
|
|
|
+package com.ruoyi.app.util;
|
|
|
+
|
|
|
+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.exceptions.RequestException;
|
|
|
+import com.gexin.rp.sdk.http.IGtPush;
|
|
|
+import com.gexin.rp.sdk.template.NotificationTemplate;
|
|
|
+import com.gexin.rp.sdk.template.style.Style0;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 消息推送
|
|
|
+ */
|
|
|
+public class PushUtil {
|
|
|
+ private final static String appId = "mcchnSrhBP9zRvamSAiMO9";
|
|
|
+ private final static String appKey = "NhbcDNy541ALTopfixskC2";
|
|
|
+ private final static String masterSecret = "weqJivey6t7CQJa26PeaG4";
|
|
|
+ private final static String host = "http://sdk.open.api.igexin.com/apiex.htm";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param title 消息标题 例如家族消息
|
|
|
+ * @param text 消息内容 例如余震申请加入陕西凤翔余姓家族
|
|
|
+ * @param client_id 设备标识
|
|
|
+ * @return true|false
|
|
|
+ */
|
|
|
+ public static boolean sendMessage(String title, String text, String client_id) {
|
|
|
+ // 设置后,根据别名推送,会返回每个cid的推送结果
|
|
|
+ IGtPush push = new IGtPush(host, appKey, masterSecret);
|
|
|
+ NotificationTemplate template = getNotificationTemplate(title, text);
|
|
|
+ SingleMessage message = new SingleMessage();
|
|
|
+ message.setOffline(true);
|
|
|
+ // 离线有效时间,单位为毫秒
|
|
|
+ message.setOfflineExpireTime(24 * 3600 * 1000);
|
|
|
+ message.setData(template);
|
|
|
+ // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
|
|
|
+ message.setPushNetWorkType(0);
|
|
|
+ // 厂商通道下发策略
|
|
|
+ message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
|
|
|
+ Target target = new Target();
|
|
|
+ target.setAppId(appId);
|
|
|
+ target.setClientId(client_id);
|
|
|
+ IPushResult ret;
|
|
|
+ try {
|
|
|
+ ret = push.pushMessageToSingle(message, target);
|
|
|
+ } catch (RequestException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ ret = push.pushMessageToSingle(message, target, e.getRequestId());
|
|
|
+ }
|
|
|
+ if (ret.getResponse().get("result").toString().equals("ok")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ System.out.println("消息推送:" + ret.getResponse().toString());
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ boolean s = sendMessage("家族通知", "余震申请加入", "4c5889ce0d7594141127eacb697b8007");
|
|
|
+ System.out.println("ssss:" + s);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static NotificationTemplate getNotificationTemplate(String title, String text) {
|
|
|
+ NotificationTemplate template = new NotificationTemplate();
|
|
|
+ //设置APPID与APPKEY
|
|
|
+ template.setAppId(appId);
|
|
|
+ template.setAppkey(appKey);
|
|
|
+
|
|
|
+ Style0 style = new Style0();
|
|
|
+ // 设置通知栏标题与内容
|
|
|
+ style.setTitle(title);
|
|
|
+ style.setText(text);
|
|
|
+ // 设置通知是否响铃,震动,或者可清除
|
|
|
+ style.setRing(true);
|
|
|
+ style.setVibrate(true);
|
|
|
+ style.setClearable(true);
|
|
|
+ style.setChannel("通知渠道id");
|
|
|
+ style.setChannelName("通知渠道名称");
|
|
|
+ style.setChannelLevel(3); //设置通知渠道重要性
|
|
|
+ template.setStyle(style);
|
|
|
+
|
|
|
+ template.setTransmissionType(1); // 透传消息接受方式设置,1:立即启动APP,2:客户端收到消息后需要自行处理
|
|
|
+ template.setTransmissionContent("请输入您要透传的内容");
|
|
|
+
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+}
|