阿里移动推送服务端API
Posted BubbleSort
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里移动推送服务端API相关的知识,希望对你有一定的参考价值。
极光推送最近老抽风,然后推送方案又一次改变,这次给大家带来的是阿里的移动推送服务端api:
首先是引入的依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-push</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>2.4.2</version> </dependency>
然后老规矩,抽取之后的代码untils
package com.commons.utils; import java.util.Date; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.push.model.v20150827.CancelPushRequest; import com.aliyuncs.push.model.v20150827.CancelPushResponse; import com.aliyuncs.push.model.v20150827.PushRequest; import com.aliyuncs.push.model.v20150827.PushResponse; import com.aliyuncs.utils.ParameterHelper; import com.ecp.commons.api.APIConstants; import com.ecp.commons.utils.JsonUtil; public class JpushUtils { public static final String appKey = com.ecp.commons.common.PropertiesUtil.getProperty("Push.appKey"); public static final String AppSecret = com.ecp.commons.common.PropertiesUtil.getProperty("Push.AppSecret"); public static final String accessKeyId = com.ecp.commons.common.PropertiesUtil.getProperty("Push.accessKeyId"); public static final String accesssecret = com.ecp.commons.common.PropertiesUtil.getProperty("Push.secret"); /** * * @auth Ren * @date 2018年5月2日 * @decripe:把obj对象的json串推送到别名为DeviceSN的设备上,同时记录返回的msg_id * @param obj推送对象,deviceSN设备识别码,MsgType推送的业务类型(APIConstants中定义) */ public static PushResponse SendPush(Object obj, String DeviceSN, String MsgType) throws Exception { PushRequest pushRequest = new PushRequest(); pushRequest.setMethod(MethodType.POST); pushRequest.setAppKey(Long.valueOf(appKey)); pushRequest.setDeviceType(1); pushRequest.setTarget("alias"); pushRequest.setTargetValue(DeviceSN); pushRequest.setType(0); // 0:表示消息(默认为0), 1:表示通知 pushRequest.setTitle(MsgType); // 消息的标题 pushRequest.setBody(ObjectToJson(obj)); // 消息的内容 IClientProfile profile = DefaultProfile.getProfile(APIConstants.REGION_HANGZHOU, accessKeyId, accesssecret); DefaultAcsClient client = new DefaultAcsClient(profile); PushResponse pushResponse = client.getAcsResponse(pushRequest); System.out.printf("RequestId: %s, ResponseId: %s\n", pushResponse.getRequestId(), pushResponse.getResponseId()); return pushResponse; } /** * * @auth Ren * @date 2018年6月6日 * @decripe:直接推所有 * @param obj推送对象,MsgType推送的业务类型(APIConstants中定义) */ public static PushResponse SendPush(Object obj,String MsgType) throws Exception { PushRequest pushRequest = new PushRequest(); pushRequest.setMethod(MethodType.POST); pushRequest.setAppKey(Long.valueOf(appKey)); pushRequest.setDeviceType(1); pushRequest.setTarget("all"); pushRequest.setTargetValue("all"); pushRequest.setType(0); // 0:表示消息(默认为0), 1:表示通知 pushRequest.setTitle(MsgType); // 消息的标题 pushRequest.setBody(ObjectToJson(obj)); // 消息的内容 IClientProfile profile = DefaultProfile.getProfile(APIConstants.REGION_HANGZHOU, accessKeyId, accesssecret); DefaultAcsClient client = new DefaultAcsClient(profile); PushResponse pushResponse = client.getAcsResponse(pushRequest); System.out.printf("RequestId: %s, ResponseId: %s\n", pushResponse.getRequestId(), pushResponse.getResponseId()); return pushResponse; } /** * * @auth Ren * @date 2018年6月6日 * @decripe:定时推所有 * @param obj推送对象,MsgType推送的业务类型(APIConstants中定义) */ public static PushResponse sendSchedulePushAll(Object obj, Date pushtime, String MsgType) throws Exception { IClientProfile profile = DefaultProfile.getProfile(APIConstants.REGION_HANGZHOU, accessKeyId, accesssecret); DefaultAcsClient client = new DefaultAcsClient(profile); PushRequest pushRequest = new PushRequest(); pushRequest.setMethod(MethodType.POST); pushRequest.setAppKey(Long.valueOf(appKey)); pushRequest.setDeviceType(1); pushRequest.setTarget("all"); pushRequest.setTargetValue("all"); pushRequest.setType(0); // 0:表示消息(默认为0), 1:表示通知 pushRequest.setTitle(MsgType); // 消息的标题 pushRequest.setBody(ObjectToJson(obj)); // 消息的内容 String pushTime = ParameterHelper.getISO8601Time(pushtime); pushRequest.setPushTime(pushTime); PushResponse pushResponse = client.getAcsResponse(pushRequest); System.out.printf("RequestId: %s, ResponseId: %s\n", pushResponse.getRequestId(), pushResponse.getResponseId()); return pushResponse; } /** * * @auth Ren * @date 2018年6月6日 * @decripe 定时推指定设备 */ public static PushResponse sendSchedulePush(Object obj, String DeviceSN, Date pushtime, String MsgType) throws Exception{ IClientProfile profile = DefaultProfile.getProfile(APIConstants.REGION_HANGZHOU, accessKeyId, accesssecret); DefaultAcsClient client = new DefaultAcsClient(profile); PushRequest pushRequest = new PushRequest(); pushRequest.setMethod(MethodType.POST); pushRequest.setAppKey(Long.valueOf(appKey)); pushRequest.setDeviceType(1); pushRequest.setTarget("alias"); pushRequest.setTargetValue(DeviceSN); pushRequest.setType(0); // 0:表示消息(默认为0), 1:表示通知 pushRequest.setTitle(MsgType); // 消息的标题 pushRequest.setBody(ObjectToJson(obj)); // 消息的内容 String pushTime = ParameterHelper.getISO8601Time(pushtime); pushRequest.setPushTime(pushTime); PushResponse pushResponse = client.getAcsResponse(pushRequest); System.out.printf("RequestId: %s, ResponseId: %s\n", pushResponse.getRequestId(), pushResponse.getResponseId()); return pushResponse; } /** * 取消定时推送 * <p> * // */ public void testCancelPush(String ResponseId) throws Exception { CancelPushRequest request = new CancelPushRequest(); request.setAppKey(Long.valueOf(appKey)); request.setMessageId(ResponseId);//推送时返回的ResponseId。 IClientProfile profile = DefaultProfile.getProfile(APIConstants.REGION_HANGZHOU, accessKeyId, accesssecret); DefaultAcsClient client = new DefaultAcsClient(profile); CancelPushResponse response = client.getAcsResponse(request); System.out.println(response.getRequestId()); } public static String ObjectToJson(Object o) { String json = JsonUtil.getJsonString4JavaPOJO(o, "yyyy-MM-dd HH:mm:ss"); return json; } }
以上是关于阿里移动推送服务端API的主要内容,如果未能解决你的问题,请参考以下文章