Commit 0816366d by hewei

Merge branch 'future/strokeList' into 'master'

添加 app和商户推送

See merge request hewei/Jumeirah!79
parents 55f650d5 0f5d15b0
package io.geekidea.springbootplus.test;
import com.jumeirah.common.factory.PushFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PushTest {
@Autowired
private PushFactory pushFactory;
/**
* app推送:1.您有一条新消息(客服回复推送)
* 2.您的订单已报价(商家端报价完后推送)
* 3.您的订单已完成(商家端完成行程完后推送
*
* @throws Exception
*/
@Test
public void pu() throws Exception {
pushFactory.getService(1).unicast("Ar4opHQcGn-UA1-fZ7O6PAeNSJs9rCEEI5XRvS_0rQke", "2", "您的订单已报价");
pushFactory.getService(1).unicast("Ar4opHQcGn-UA1-fZ7O6PAeNSJs9rCEEI5XRvS_0rQke", "3", "您的订单已完成");
}
}
...@@ -12,4 +12,7 @@ public interface PushService { ...@@ -12,4 +12,7 @@ public interface PushService {
* @param deviceToken * @param deviceToken
*/ */
void unicast(String deviceToken); void unicast(String deviceToken);
void unicast(String deviceToken,String pushType,String title) throws Exception;
} }
package com.jumeirah.common.push;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class PushClient {
// The host
protected static final String host = "http://msg.umeng.com";
// The upload path
protected static final String uploadPath = "/upload";
// The post path
protected static final String postPath = "/api/send";
// The user agent
protected final String USER_AGENT = "Mozilla/5.0";
// This object is used for sending the post request to Umeng
protected HttpClient client = new DefaultHttpClient();
public boolean send(UmengNotification msg) throws Exception {
String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
msg.setPredefinedKeyValue("timestamp", timestamp);
String url = host + postPath;
String postBody = msg.getPostBody();
String sign = DigestUtils.md5Hex(("POST" + url + postBody + msg.getAppMasterSecret()).getBytes(StandardCharsets.UTF_8));
url = url + "?sign=" + sign;
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
StringEntity se = new StringEntity(postBody, "UTF-8");
post.setEntity(se);
// Send the post request and get the response
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
System.out.println("Response Code : " + status);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
if (status == 200) {
System.out.println("Notification sent successfully.");
} else {
System.out.println("Failed to send the notification!");
}
return true;
}
// Upload file with device_tokens to Umeng
public String uploadContents(String appkey, String appMasterSecret, String contents) throws Exception {
// Construct the json string
JSONObject uploadJson = new JSONObject();
uploadJson.put("appkey", appkey);
String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
uploadJson.put("timestamp", timestamp);
uploadJson.put("content", contents);
// Construct the request
String url = host + uploadPath;
String postBody = uploadJson.toString();
String sign = DigestUtils.md5Hex(("POST" + url + postBody + appMasterSecret).getBytes(StandardCharsets.UTF_8));
url = url + "?sign=" + sign;
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
StringEntity se = new StringEntity(postBody, "UTF-8");
post.setEntity(se);
// Send the post request and get the response
HttpResponse response = client.execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
// Decode response string and get file_id from it
JSONObject respJson = new JSONObject(result.toString());
String ret = respJson.getString("ret");
if (!ret.equals("SUCCESS")) {
throw new Exception("Failed to upload file");
}
JSONObject data = respJson.getJSONObject("data");
String fileId = data.getString("file_id");
// Set file_id into rootJson using setPredefinedKeyValue
return fileId;
}
}
package com.jumeirah.common.push;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.HashSet;
public abstract class UmengNotification {
// Keys can be set in the root level
protected static final HashSet<String> ROOT_KEYS = new HashSet<String>(Arrays.asList("appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id",
"filter", "production_mode", "feedback", "description", "thirdparty_id", "mipush", "mi_activity", "channel_properties"));
// Keys can be set in the policy level
protected static final HashSet<String> POLICY_KEYS = new HashSet<String>(Arrays.asList("start_time", "expire_time", "max_send_num"));
// This JSONObject is used for constructing the whole request string.
protected final JSONObject rootJson = new JSONObject();
// The app master secret
protected String appMasterSecret;
// Set predefined keys in the rootJson, for extra keys(Android) or customized keys(IOS) please
// refer to corresponding methods in the subclass.
public abstract boolean setPredefinedKeyValue(String key, Object value) throws Exception;
public String getPostBody() {
return rootJson.toString();
}
protected final String getAppMasterSecret() {
return appMasterSecret;
}
public void setAppMasterSecret(String secret) {
appMasterSecret = secret;
}
protected void setProductionMode(Boolean prod) throws Exception {
setPredefinedKeyValue("production_mode", prod.toString());
}
///正式模式
public void setProductionMode() throws Exception {
setProductionMode(true);
}
///测试模式
public void setTestMode() throws Exception {
setProductionMode(false);
}
///发送消息描述,建议填写。
public void setDescription(String description) throws Exception {
setPredefinedKeyValue("description", description);
}
///定时发送时间,若不填写表示立即发送。格式: "YYYY-MM-DD hh:mm:ss"。
public void setStartTime(String startTime) throws Exception {
setPredefinedKeyValue("start_time", startTime);
}
///消息过期时间,格式: "YYYY-MM-DD hh:mm:ss"。
public void setExpireTime(String expireTime) throws Exception {
setPredefinedKeyValue("expire_time", expireTime);
}
///发送限速,每秒发送的最大条数。
public void setMaxSendNum(Integer num) throws Exception {
setPredefinedKeyValue("max_send_num", num);
}
//厂商弹窗activity
public void setChannelActivity(String activity) throws Exception {
setPredefinedKeyValue("mipush", "true");
setPredefinedKeyValue("mi_activity", activity);
}
//厂商属性配置
public void setChannelProperties(String xiaoMiChannelId) throws Exception {
JSONObject object = new JSONObject();
object.put("xiaomi_channel_id", xiaoMiChannelId);
setPredefinedKeyValue("channel_properties", object);
}
}
package com.jumeirah.common.push; package com.jumeirah.common.push.umeng;
import org.json.JSONObject; import org.json.JSONObject;
......
package com.jumeirah.common.push; package com.jumeirah.common.push.umeng;
public class App { public class App {
......
package com.jumeirah.common.push; package com.jumeirah.common.push.umeng;
import com.jumeirah.common.push.android.AndroidBroadcast; import com.jumeirah.common.push.umeng.android.AndroidBroadcast;
import com.jumeirah.common.push.android.AndroidCustomizedcast; import com.jumeirah.common.push.umeng.android.AndroidCustomizedcast;
import com.jumeirah.common.push.android.AndroidFilecast; import com.jumeirah.common.push.umeng.android.AndroidFilecast;
import com.jumeirah.common.push.android.AndroidGroupcast; import com.jumeirah.common.push.umeng.android.AndroidGroupcast;
import com.jumeirah.common.push.android.AndroidUnicast; import com.jumeirah.common.push.umeng.android.AndroidUnicast;
import com.jumeirah.common.push.ios.IOSBroadcast; import com.jumeirah.common.push.umeng.ios.IOSBroadcast;
import com.jumeirah.common.push.ios.IOSCustomizedcast; import com.jumeirah.common.push.umeng.ios.IOSCustomizedcast;
import com.jumeirah.common.push.ios.IOSFilecast; import com.jumeirah.common.push.umeng.ios.IOSFilecast;
import com.jumeirah.common.push.ios.IOSGroupcast; import com.jumeirah.common.push.umeng.ios.IOSGroupcast;
import com.jumeirah.common.push.ios.IOSUnicast; import com.jumeirah.common.push.umeng.ios.IOSUnicast;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
......
package com.jumeirah.common.push; package com.jumeirah.common.push.umeng;
import org.json.JSONObject; import org.json.JSONObject;
......
package com.jumeirah.common.push.umeng;
import com.jumeirah.common.factory.PushService;
import com.jumeirah.common.push.umeng.android.AndroidNotification;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author: JJww
* @Date:2020/11/5
*/
@Component
public class UmengAndroidPushServiceImpl extends AndroidNotification implements PushService {
@Autowired
private PushClient pushClient;
private static final String APP_KEY = "5f963f3ea1491772a2aef713";
private static final String APP_MASTER_SECRET = "ti5gomxtvmkgehmwgtbq9rtsfhy0khsi";
@PostConstruct
public void init() throws Exception {
setAppMasterSecret(APP_MASTER_SECRET);
setPredefinedKeyValue("appkey", APP_KEY);
}
@Override
@SneakyThrows
public void unicast(String deviceToken) {
this.setTitle("您有一条新信息");
this.setText("");
this.goAppAfterOpen();
this.setPredefinedKeyValue("device_tokens", deviceToken);
this.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
this.setProductionMode();
this.setExtraField("pushType", "1");//自定义字段 推送类型:客服消息
this.setPredefinedKeyValue("type", "unicast");//单播
pushClient.send(this);
}
@Override
public void unicast(String deviceToken, String pushType, String title) throws Exception {
this.setTitle(title);
this.setText("");
this.goAppAfterOpen();
this.setPredefinedKeyValue("device_tokens", deviceToken);
this.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
this.setProductionMode();
this.setExtraField("pushType", pushType);//自定义字段 推送类型:客服消息
this.setPredefinedKeyValue("type", "unicast");//单播
pushClient.send(this);
}
}
package com.jumeirah.common.push.umeng;
import com.jumeirah.common.factory.PushService;
import com.jumeirah.common.push.umeng.ios.IOSNotification;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author: JJww
* @Date:2020/11/5
*/
@Component
public class UmengIosPushServiceImpl extends IOSNotification implements PushService {
@Autowired
private PushClient pushClient;
private static final String APP_KEY = "5f963f3ea1491772a2aef713";
private static final String APP_MASTER_SECRET = "ti5gomxtvmkgehmwgtbq9rtsfhy0khsi";
@PostConstruct
public void init() throws Exception {
setAppMasterSecret(APP_MASTER_SECRET);
setPredefinedKeyValue("appkey", APP_KEY);
}
@Override
@SneakyThrows
public void unicast(String deviceToken) {
this.setAlert("您有一条新信息");
this.setBadge(0);
this.setSound("default");
this.setProductionMode();
this.setPredefinedKeyValue("device_tokens", deviceToken);
this.setCustomizedField("pushType", "1");//自定义字段 推送类型:客服消息
this.setPredefinedKeyValue("type", "unicast");
pushClient.send(this);
}
@Override
public void unicast(String deviceToken, String pushType, String title) throws Exception {
this.setAlert(title);
this.setBadge(0);
this.setSound("default");
this.setProductionMode();
this.setPredefinedKeyValue("device_tokens", deviceToken);
this.setCustomizedField("pushType", pushType);//自定义字段 推送类型:客服消息
this.setPredefinedKeyValue("type", "unicast");
pushClient.send(this);
}
}
package com.jumeirah.common.push.android; package com.jumeirah.common.push.umeng.android;
import com.jumeirah.common.push.AndroidNotification; import com.jumeirah.common.push.umeng.AndroidNotification;
public class AndroidBroadcast extends AndroidNotification { public class AndroidBroadcast extends AndroidNotification {
public AndroidBroadcast(String appkey, String appMasterSecret) throws Exception { public AndroidBroadcast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.android; package com.jumeirah.common.push.umeng.android;
import com.jumeirah.common.push.AndroidNotification; import com.jumeirah.common.push.umeng.AndroidNotification;
public class AndroidCustomizedcast extends AndroidNotification { public class AndroidCustomizedcast extends AndroidNotification {
public AndroidCustomizedcast(String appkey, String appMasterSecret) throws Exception { public AndroidCustomizedcast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.android; package com.jumeirah.common.push.umeng.android;
import com.jumeirah.common.push.AndroidNotification; import com.jumeirah.common.push.umeng.AndroidNotification;
public class AndroidFilecast extends AndroidNotification { public class AndroidFilecast extends AndroidNotification {
public AndroidFilecast(String appkey, String appMasterSecret) throws Exception { public AndroidFilecast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.android; package com.jumeirah.common.push.umeng.android;
import com.jumeirah.common.push.AndroidNotification; import com.jumeirah.common.push.umeng.AndroidNotification;
import org.json.JSONObject; import org.json.JSONObject;
public class AndroidGroupcast extends AndroidNotification { public class AndroidGroupcast extends AndroidNotification {
......
package com.jumeirah.common.push.android; package com.jumeirah.common.push.umeng.android;
import com.jumeirah.common.push.AndroidNotification; import com.jumeirah.common.push.umeng.AndroidNotification;
public class AndroidUnicast extends AndroidNotification { public class AndroidUnicast extends AndroidNotification {
public AndroidUnicast(String appkey, String appMasterSecret) throws Exception { public AndroidUnicast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.ios; package com.jumeirah.common.push.umeng.ios;
import com.jumeirah.common.push.IOSNotification; import com.jumeirah.common.push.umeng.IOSNotification;
public class IOSBroadcast extends IOSNotification { public class IOSBroadcast extends IOSNotification {
public IOSBroadcast(String appkey, String appMasterSecret) throws Exception { public IOSBroadcast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.ios; package com.jumeirah.common.push.umeng.ios;
import com.jumeirah.common.push.IOSNotification; import com.jumeirah.common.push.umeng.IOSNotification;
public class IOSCustomizedcast extends IOSNotification { public class IOSCustomizedcast extends IOSNotification {
public IOSCustomizedcast(String appkey, String appMasterSecret) throws Exception { public IOSCustomizedcast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.ios; package com.jumeirah.common.push.umeng.ios;
import com.jumeirah.common.push.IOSNotification; import com.jumeirah.common.push.umeng.IOSNotification;
public class IOSFilecast extends IOSNotification { public class IOSFilecast extends IOSNotification {
public IOSFilecast(String appkey, String appMasterSecret) throws Exception { public IOSFilecast(String appkey, String appMasterSecret) throws Exception {
......
package com.jumeirah.common.push.ios; package com.jumeirah.common.push.umeng.ios;
import com.jumeirah.common.push.IOSNotification; import com.jumeirah.common.push.umeng.IOSNotification;
import org.json.JSONObject; import org.json.JSONObject;
public class IOSGroupcast extends IOSNotification { public class IOSGroupcast extends IOSNotification {
......
package com.jumeirah.common.push.ios; package com.jumeirah.common.push.umeng.ios;
import com.jumeirah.common.push.IOSNotification; import com.jumeirah.common.push.umeng.IOSNotification;
public class IOSUnicast extends IOSNotification { public class IOSUnicast extends IOSNotification {
public IOSUnicast(String appkey, String appMasterSecret) throws Exception { public IOSUnicast(String appkey, String appMasterSecret) throws Exception {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment