版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_38990811/article/details/80256927
前言:公司要做app推送,选择了友盟,刚接触推送这一块,也不怎么熟悉,碰到了一些问题,比如友盟推送是分平台:Android和IOS都是不同的key和secret.然后就是如果想在本地测试,那么就要去友盟后台关闭白名单......等等
特别是刚接触的同学特别需要注意是:
1.deviceToken是IOS或者Android来获取传递给我们的,不是我们自己能获取的.
2.如果需要使用别名推送,那么也是IOS或者Android来将deviceToken和用户的唯一属性进行绑定的,不需要我们后端处理,但是每次绑定的时候,是需要几分钟左右,友盟那边才会生成,不是秒级的,他们绑定后,我们会定义一个别名类型,他们定义好后,会提供给我们.我们只需要将这个值赋予alias_type就行.
3.友盟提供的demo中就有利用token推送,也有广播推送,但是没提供别名推送
API消息推送常见问题及解决方法小览:http://bbs.umeng.com/thread-10472-1-1.html
接口调用错误码详解:http://dev.umeng.com/push/android/api-doc#4_8
参考网址:https://blog.csdn.net/weilai_zhilu/article/details/77976015
这里我就只提供别名推送:
import java.util.ResourceBundle;
import com.alibaba.fastjson.JSONObject;
public class PushUtil {
private static String appAndroidkey = "";
private static String appAndroidMasterSecret = "";
private static String appIOSkey = "";
private static String appIOSMasterSecret = "";
private static String aliasType = ""; //别名类型,与手机端统一的一个自定义类型 值可随便写但必须统一
// http接口:http://msg.umeng.com/api/send
// https接口:https://msgapi.umeng.com/api/send
private static String url = "";
private static boolean mode = false; //模式 true正式模式;false测试模式
private static PushClient client = new PushClient();
static {
if(appAndroidkey.equals("")) {
ResourceBundle resource = ResourceBundle.getBundle("push");
appAndroidkey = resource.getString("appAndroidkey");
appAndroidMasterSecret = resource.getString("appAndroidMasterSecret");
appAndroidkey = resource.getString("appIOSkey");
appAndroidMasterSecret = resource.getString("appIOSMasterSecret");
aliasType = resource.getString("aliasType");
url = resource.getString("url");
mode = resource.getObject("mode") == "true" ? true : false;
}
}
/**
* 发送安卓别名推送通知
*
* @author zhy
* @param alias 别名
* @param title 大标题
* @param text 内容
* @throws Exception
*/
public static void sendAndroidCustomizedcast(String alias, String title, String text) throws Exception {
JSONObject paramMap = new JSONObject();
paramMap.put("appkey", appAndroidkey);
String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
paramMap.put("timestamp", timestamp);
paramMap.put("type", "customizedcast");
paramMap.put("alias", alias);
paramMap.put("alias_type", aliasType);
JSONObject payload = new JSONObject();
payload.put("display_type", "notification");
JSONObject body = new JSONObject();
//body.put("ticker", UserControllerUtil.getUserColumn(alias, "deviceTokens"));
body.put("title", title);
body.put("text", text);
body.put("after_open", "go_custom");
payload.put("body", body);
paramMap.put("payload", payload);
paramMap.put("production_mode", mode);
client.send(url, paramMap.toJSONString(), appAndroidMasterSecret);
}
/**
* 发送苹果别名推送通知
*
* @author zhy
* @param alias 别名
* @param title 大标题
* @param body 内容
* @throws Exception
*/
public static void sendIOSCustomizedcast(String alias, String title, String body) throws Exception {
JSONObject paramMap = new JSONObject();
paramMap.put("appkey", appIOSkey);
String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
paramMap.put("timestamp", timestamp);
paramMap.put("type", "customizedcast");
paramMap.put("alias", alias);
paramMap.put("alias_type", aliasType);
JSONObject payload = new JSONObject();
payload.put("display_type", "notification");
JSONObject aps = new JSONObject();
JSONObject alert = new JSONObject();
alert.put("title", title);
alert.put("body", body);
aps.put("alert", alert);
aps.put("sound", "default");
payload.put("aps", aps);
//paramMap.put("description", "消息111");
paramMap.put("payload", payload);
paramMap.put("production_mode", mode);
client.send(url, paramMap.toJSONString(), appIOSMasterSecret);
}
public static void main(String[] args) {
try {
sendIOSCustomizedcast("别名", "标题","内容");
} catch (Exception ex) {
ex.printStackTrace();
//如果在本地要测试,则去友盟后台取消白名单,然后mode设置为true(正式环境).即可发送成功
/**
* 本地的json数据,
*/
//{"alias":"17671632486","alias_type":"CoinLa.PhoneNumber","appkey":"5a9fa4dbf43e48028e000237",
//"description":"测试消息111","payload":{"aps":{"alert":{"body":"测试消息1111","title":"测试消息"},"badge":1,"sound":"default"},
//"display_type":"notification"},"production_mode":true,"timestamp":"1525855267","type":"customizedcast"}
/**
* 线上测试的json数据
*/
//{"policy":{"expire_time":"2018-05-12 16:32:52"},"description":"阿达",
//"production_mode":true,"appkey":"5a9fa4dbf43e48028e000237",
//"payload":{"aps":{"alert":{"title":"阿达","subtitle":"阿达","body":"阿达"},"sound":"default"}},
//"alias":"17671632486","alias_type":"CoinLa.PhoneNumber","type":"customizedcast","timestamp":"1525854888505"}
}
}
}
发送类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
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.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class PushClient {
private Logger log = LogManager.getLogger(PushClient.class);
// 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(String url, String postBody, String appIOSMasterSecret) throws Exception {
String sign = DigestUtils.md5Hex(("POST" + url + postBody + appIOSMasterSecret).getBytes("utf8"));
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();
log.debug(String.format("Response Code : %s", 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) {
log.debug("success");
} else {
log.debug("fail");
}
return true;
}
}
pom:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>