ios和android指定用户推送消息和通知
配置参数
push.properties
#accessKeyId=LTAIm5EOe8mlkIkT
#accessKeySecret =fE23DvOe4tT4A0YLQH1Cm8JeMEJvlS
#appKey = 24876817
#appKey1 = 24878831
accessKeyId=LTAIfF7uzwkRGkQ6
accessKeySecret =xPeGIMcGCHWbxu68akeB8i7c0YGceK
appKey =24874448
appKey1 =24889807
# 发送的deviceId, 列表用逗号分隔 比如: 111 或是 111,222
deviceIds = 75a2e2a16d***********133498154b,236c320868***********1a20c04d8b0e,2790***********6a2239b8ce6f9f34b
deviceId = 75a2e2a***********328133498154b
# 发送的account, 列表用逗号分隔 比如: account1 或是 account1,account2
accounts =176***********3741
account = li**ng
# 发送的alias, 列表用逗号分隔 比如: alias1,alias2
aliases = _YOUR_ALIAS_***********_HERE_
alias = _YOUR_***********_
# 发送的tag, tag表达式参考文档:https://help.aliyun.com/document_detail/48055.html?
tag = tag1
tagExpression = {"and": [ {"tag": "男性" },{"tag": "90后"},{"not": {"tag": "IT"} } ] }
# 目前该值固定,不用动
regionId = cn-hangzhou
#消息推送 ios环境 DEV :表示开发环境, PRODUCT: 表示生产环境
ApnsEnv =DEV
推送工具类:
import com.alibaba.druid.support.json.JSONUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.push.model.v20160801.*;
import com.aliyuncs.utils.ParameterHelper;
import com.tcwl.vsmp.app.PushAppController;
import com.tcwl.vsmp.common.BaseController;
import com.tcwl.vsmp.common.OilConstant;
import com.tcwl.vsmp.customer.common.CustConstant;
import com.tcwl.vsmp.customer.po.MsgInfo;
import com.tcwl.vsmp.customer.service.PushService;
import com.tcwl.vsmp.utils.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
/**
* Created by gyf on 5/8.
*/
@Controller
public class PushController
{
protected static String region;
protected static long appKey;
protected static long appKey1;
protected static String env;
protected static DefaultAcsClient client;
/**
* push service
*/
@Autowired
private PushService pushService;
/**
* 先执行静态代码块
*/
static
{
InputStream inputStream = PushController.class.getClassLoader()
.getResourceAsStream("push.properties");
Properties properties = new Properties();
try
{
properties.load(inputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
String accessKeyId = properties.getProperty("accessKeyId");
System.out.println(
"先在 push.properties 配置文件中配置 accessKeyId:" + accessKeyId);
String accessKeySecret = properties.getProperty("accessKeySecret");
System.out.println("先在 push.properties 配置文件中配置 accessKeySecret:"
+ accessKeySecret);
String key = properties.getProperty("appKey");
System.out.println("先在 push.properties 配置文件中配置 appKey:" + key);
env = properties.getProperty("ApnsEnv");
region = properties.getProperty("regionId");
appKey = Long.valueOf(key);
appKey1 = Long.valueOf(properties.getProperty("appKey1"));
IClientProfile profile = DefaultProfile.getProfile(region, accessKeyId,
accessKeySecret);
client = new DefaultAcsClient(profile);
}
/**
*
* 给ios和android指定用户推送消息和通知
* @param accounts 可以有,多个账号,账号之间用“,”分割
* @param title
* @param body
* @param url
* @throws Exception
*/
public static void sendPushByAccount(String accounts, String title,
String body, String url) throws Exception
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("url", url);
String extParameters = JSONUtils.toJSONString(map);
System.out.println(extParameters);
// ios部分
System.out.println("ios通知:根据账户推送消息 ");
PushNoticeToiOSRequest iOSRequest1 = new PushNoticeToiOSRequest();
iOSRequest1.setProtocol(ProtocolType.HTTPS);
iOSRequest1.setMethod(MethodType.POST);
iOSRequest1.setAppKey(appKey);
iOSRequest1.setApnsEnv(env);
iOSRequest1.setTarget("ACCOUNT");
iOSRequest1.setTargetValue(accounts);
iOSRequest1.setTitle(title);
iOSRequest1.setBody(body);
iOSRequest1.setExtParameters(extParameters);
PushNoticeToiOSResponse pushNoticeToiOSResponse = client
.getAcsResponse(iOSRequest1);
System.out.printf("RequestId: %s, MessageId: %s\n",
pushNoticeToiOSResponse.getRequestId(),
pushNoticeToiOSResponse.getMessageId());
// 安卓部分--通知----------------------------------------------------------
System.out.println("安卓:根据账户推送消息");
// 高级接口
PushRequest pushRequest = new PushRequest();
pushRequest.setProtocol(ProtocolType.HTTPS);
pushRequest.setMethod(MethodType.POST);
// 推送目标
pushRequest.setAppKey(appKey1);
pushRequest.setTarget("ACCOUNT");
pushRequest.setTargetValue(accounts);
pushRequest.setPushType("NOTICE"); // 消息类型 MESSAGE NOTICE
pushRequest.setDeviceType("ALL"); // 设备类型 ANDROID iOS ALL.
// 推送配置
pushRequest.setTitle(title); // 消息的标题
pushRequest.setBody(body); // 消息的内容
// 推送配置: Android
pushRequest.setAndroidNotifyType("BOTH");
pushRequest.setAndroidOpenType("NONE");
pushRequest.setAndroidExtParameters(extParameters);
// 12小时后消息失效,
String expireTime = ParameterHelper.getISO8601Time(
new Date(System.currentTimeMillis() + 12 * 3600 * 1000));
pushRequest.setExpireTime(expireTime);
pushRequest.setStoreOffline(true);
PushResponse pushResponse = client.getAcsResponse(pushRequest);
System.out.printf("RequestId: %s, MessageID: %s\n",
pushResponse.getRequestId(), pushResponse.getMessageId());
// ios部分 -------------------------------------------------------------
try
{
Thread.sleep(2000); // 1000 毫秒,也就是1秒.
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
}
System.out.println("ios消息:根据账户推送消息 ");
PushMessageToiOSRequest iOSRequest = new PushMessageToiOSRequest();
iOSRequest.setProtocol(ProtocolType.HTTPS);
iOSRequest.setMethod(MethodType.POST);
iOSRequest.setAppKey(appKey);
iOSRequest.setTarget("ACCOUNT");
iOSRequest.setTargetValue(accounts);
iOSRequest.setTitle(title);
iOSRequest.setBody(body);
PushMessageToiOSResponse pushMessageToiOSResponse = client
.getAcsResponse(iOSRequest);
System.out.printf("RequestId: %s, MessageId: %s\n",
pushMessageToiOSResponse.getRequestId(),
pushMessageToiOSResponse.getMessageId());
}
/**
* * 给ios全局发送消息和通知(比如升级)
* @param title
* @param body
* @param picurl
* @param bigpicurl
* @throws Exception
*/
public void PushToPhone(String title, String body, String picurl,
String bigpicurl) throws Exception
{
String logstartTime = DateUtils.getCurrentDate("yyyy-MM-dd");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("picurl", picurl);
map.put("bigpicurl", bigpicurl);
map.put("pushtime", logstartTime);
String extParameters = JSONUtils.toJSONString(map);
// ios部分
// 通知-----------------------------------------------------------------
System.out.println("ios部分 通知");
PushNoticeToiOSRequest iOSRequest1 = new PushNoticeToiOSRequest();
// 安全性比较高的内容建议使用HTTPS
iOSRequest1.setProtocol(ProtocolType.HTTPS);
// 内容较大的请求,使用POST请求
iOSRequest1.setMethod(MethodType.POST);
iOSRequest1.setAppKey(appKey);
// iOS的通知是通过APNS中心来发送的,需要填写对应的环境信息.
// DEV :表示开发环境, PRODUCT: 表示生产环境
iOSRequest1.setApnsEnv(env);
iOSRequest1.setTarget("ALL");
iOSRequest1.setTargetValue("all");
iOSRequest1.setTitle(title);
iOSRequest1.setBody(body);
iOSRequest1.setExtParameters(extParameters);
PushNoticeToiOSResponse pushNoticeToiOSResponse = client
.getAcsResponse(iOSRequest1);
System.out.printf("RequestId: %s, MessageId: %s\n",
pushNoticeToiOSResponse.getRequestId(),
pushNoticeToiOSResponse.getMessageId());
// 安卓部分--通知----------------------------------------------------------
System.out.println("安卓部分");
// 高级接口
PushRequest pushRequest = new PushRequest();
// 安全性比较高的内容建议使用HTTPS
pushRequest.setProtocol(ProtocolType.HTTPS);
// 内容较大的请求,使用POST请求
pushRequest.setMethod(MethodType.POST);
// 推送目标
pushRequest.setAppKey(appKey1);
// 推送目标: DEVICE:按设备推送 ALIAS : 按别名推送
// ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
pushRequest.setTarget("ALL");
// 根据Target来设定,如Target=DEVICE, 则对应的值为
// 设备id1,设备id2.
// 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
pushRequest.setTargetValue("all");
pushRequest.setPushType("NOTICE"); // 消息类型 MESSAGE NOTICE
pushRequest.setDeviceType("ALL"); // 设备类型 ANDROID iOS ALL.
// 推送配置
pushRequest.setTitle(title); // 消息的标题
pushRequest.setBody(body); // 消息的内容
// 推送配置: Android
pushRequest.setAndroidNotifyType("BOTH");
pushRequest.setAndroidOpenType("NONE");
pushRequest.setAndroidExtParameters(extParameters);
String expireTime = ParameterHelper.getISO8601Time(
new Date(System.currentTimeMillis() + 12 * 3600 * 1000));
// 离线消息是否保存,若保存,
// 在推送时候,用户即使不在线,下一次上线则会收到
pushRequest.setExpireTime(expireTime);
pushRequest.setStoreOffline(true);
PushResponse pushResponse = client.getAcsResponse(pushRequest);
System.out.printf("RequestId: %s, MessageID: %s\n",
pushResponse.getRequestId(), pushResponse.getMessageId());
// ios部分 消息-------------------------------------------------------
try
{
Thread.sleep(2000); // 1000 毫秒,也就是1秒.
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
}
System.out.println("ios部分 消息");
PushMessageToiOSRequest iOSRequest = new PushMessageToiOSRequest();
iOSRequest.setProtocol(ProtocolType.HTTPS);
iOSRequest.setMethod(MethodType.POST);
iOSRequest.setAppKey(appKey);
iOSRequest.setTarget("ALL");
iOSRequest.setTargetValue("all");
iOSRequest.setTitle(title);
iOSRequest.setBody(body);
PushMessageToiOSResponse pushMessageToiOSResponse = client
.getAcsResponse(iOSRequest);
System.out.printf("RequestId: %s, MessageId: %s\n",
pushMessageToiOSResponse.getRequestId(),
pushMessageToiOSResponse.getMessageId());
}
发送通知:
js:
//消息推送
$("#pushMsgPush").on("click",function () {
var pushCustomer_code = $("#pushCustomer_code").val();
if (pushCustomer_code == null || pushCustomer_code.trim() == '') {
alert("请输入推送账户");
return;
}
var msgtitle = $("#msgtitlePush").val();
if (msgtitle == null || msgtitle.trim() == '') {
alert("请输入推送标题");
return;
}
var msg = $("#msgPush").val();
if (msg == null || msg.trim() == '') {
alert("请输入推送内容");
return;
}
var pushdept = $("#pushdeptPush").val();
if (pushdept == null || pushdept.trim() == '') {
alert("请输入推送部门");
return;
}
var pushtime =getNowFormatDate();
var pushInfo = {
"customer_code": pushCustomer_code,
"title": msgtitle,
"msg": msg,
"pushdept": pushdept,
"pushtime": pushtime,
"pushtype": 1,
"status": 1
}
var data_ = JSON.stringify(pushInfo);
$("#pushMsgPush").attr("disabled",'disabled');
$.ajax({
url: "../../../customer/sendPush",
type: "POST",
data: data_,
contentType: "application/json;charset=utf-8",
dataType: "text",
success: function (data) {
alert(data);
window.location.reload();
},
error: function (data) {
alert("ajax异常");
}
});
})
控制器:
@RequestMapping(value = "/sendPush", method = RequestMethod.POST, produces =
{ "application/json;charset=UTF-8" })
public @ResponseBody String sendPush(HttpServletRequest request,
HttpServletResponse response, @RequestBody MsgInfo msgInfo)
{
String startTime = DateUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss");
try
{
Integer insertStatus = pushService.addPush(msgInfo);
PushController.sendPushByAccount(msgInfo.getCustomer_code(),msgInfo.getTitle(),msgInfo.getMsg(),
msgInfo.getUrl());
return "添加成功";
}
catch (Exception e)
{
e.printStackTrace();
BaseController.saveLog(
SecurityUtils.getSubject().getPrincipals().toString(),
startTime, DateUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss"),
BaseController.getRequestIp(request), "客户管理", "根据指定账户推送消息", 0,
1, "");
return "操作异常";
}
}