最近应公司业务需求需要完善友盟推送,认真看了官方文档后其实很简单,只需要细心些,然后找个靠谱的移动端配合你接收很快就能推送成功。
友盟官方API地址:https://developer.umeng.com/docs/66632/detail/68343
图中右侧目录是对应各推送模式下的参数释义,仔细看每个参数,然后和移动端商议好双方传送字段名。
{
"payload": {
"aps": {
"badge": 0,
"alert": "测试标题",
"sound": "default"
},
"message": "{"result":{"arr0":[{"messageTitle":"测试标题","pushContent":"测试内容, ios推送"}]}}"
},
"appkey": "5843b2d398f4a9d2804001cf40",
"type": "unicast",
"production_mode": "false",
"device_tokens": "1842d612874fd0f8a15cfaf76ecf4b876c43482cfcd66a96f7cc68f93d3084",
"policy": {
"start_time": "2018-10-28 12:23:23"
}
}
// 运行Demo.java 打印出发送内容如上:
// 这里需要注意appkey和appsecret是Android和IOS在UM官方注册提供的。
// 在外网环境移动端才可接收到推送。本地环境会提示IP不在服务器白名单,如下:
{"ret":"FAIL","data":{"error_msg":"IP不在白名单中, 请到网站后台添加您的服务器IP或关闭IP白名单功能","error_code":"2054"}} Failed to send the notification!
// IOS单播---需要用户deviceToken
/**
* 使用的是sdk的IOSUnicast对象
* deviceToken可使用测试机的token,移动端获取存入服务器数据库
* object里面存放是IOS实际取的内容,其它参数是UM原定的可看业务而定,也可以和移动端自定义格式
* 使用customizedcast.getPostBody()打印推送内容
* /
public void ios() throws Exception{
String appkey = "585b2d398f45354a9d2804001cf0";
String appMasterSecret = "qjhlck6p4brfmn70adjof5434sxmr4ie0lie";
//IOS单播
IOSUnicast unicast = new IOSUnicast(appkey, appMasterSecret);
JSONObject result = new JSONObject();
JSONObject custom = new JSONObject();
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
//object.put("sound", "default");
object.put("messageTitle", "IOS推送标题"); //IOS推送标题
object.put("pushContent","推送的内容, ios推送");//推送内容
array.put(object);
result.put("arr0", array);
custom.put("result", result);
unicast.setDeviceToken("1842d612874fd0f8a1ecf76c2ddea82cfcd66a96f7cc68f93d3084");
unicast.setAlert("IOS推送标题");
unicast.setBadge(0);
unicast.setStartTime("2018-10-28 12:23:23");
unicast.setSound("default");
unicast.setTestMode();
unicast.setCustomizedField("message", custom.toString());
System.out.println("==iosDeviceToken:"+unicast.getPostBody());
client.send(unicast);
}
//android自定义播
/**
* 使用的是sdk的AndroidCustomizedcast对象
* object里面存放是android实际取的内容,其它参数是UM原定的可看业务而定
* setCustomField里面是android实际读取的,也可和移动端自定义数据结构
* 使用customizedcast.getPostBody()打印推送的内容
* 此处Alias = persionId || userId
* /
public void android() throws Exception{
String key = "585b2bd74543734be459bd001442";
String secret = "yoknur0n2iuqw543qjjoegyikbkj4aksej2";
//android自定义播
AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(key, secret);
JSONObject result = new JSONObject();
JSONObject custom = new JSONObject();
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
object.put("sound", "todayTask");
object.put("messageTitle", "测试推送标题"); //推送标题
object.put("pushContent","测试推送的内容, android推送"); //推送内容
array.put(object);
result.put("arr0", array);
custom.put("result", result);
customizedcast.setCustomField(custom);
customizedcast.setAlias("1815", "SINA_WEIBO"); // 此处Alias = persionId || userId
customizedcast.setTicker("测试推送标题");
customizedcast.setTitle("测试推送标题");
customizedcast.setText("测试推送的内容, android推送");
customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
customizedcast.setProductionMode();
System.out.println("发送内容:"+customizedcast.getPostBody());
client.send(customizedcast);
}
//IOS自定义播---同上Andriod自定义播
public void iosAlias() throws Exception{
String appkey = "585b2d398f1ddd2804001cf0";
String appMasterSecret = "qjhlck6p4br89jhn70adjofsxmr4ie0lie";
IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey, appMasterSecret);
JSONObject result = new JSONObject();
JSONObject custom = new JSONObject();
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
//object.put("sound", "default");
object.put("messageTitle", "测试推送标题"); //推送的标题
object.put("pushContent","测试推送的内容, ios推送"); //推送的内容
array.put(object);
result.put("arr0", array);
custom.put("result", result);
customizedcast.setAlias("1815", "IOSAPP"); //此处Alias = persionId || userId
customizedcast.setAlert("测试推送标题");
customizedcast.setDescription("customizedcast");
customizedcast.setBadge(0);
customizedcast.setStartTime("2018-10-28 12:23:23");
customizedcast.setSound("default");
customizedcast.setCustomizedField("message", custom.toString());
customizedcast.setTestMode();
System.out.println("==iosAlias:"+customizedcast.getPostBody());
client.send(customizedcast);
}
全篇原创纯手码,如有疑问,欢迎指导!