极光推送常用的几个api方法总结,抽取出了utils类,利用MsgType进行业务类型区别,方便app端收到推送后进行不同处理:

首先引入依赖:

<!-- 极光推送 --><dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.3.4</version></dependency><dependency><groupId>cn.jpush.api</groupId><artifactId>jiguang-common</artifactId><version>1.1.1</version></dependency>

package com.commons.utils;import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.ecp.commons.exception.APIException;
import com.ecp.commons.utils.JsonUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.schedule.ScheduleResult;public class JpushUtils {//读取配置中的appkey和masterSecretprotected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);public static final String appKey = com.ecp.commons.common.PropertiesUtil.getProperty("jPush.appKey");public static final String masterSecret = com.ecp.commons.common.PropertiesUtil.getProperty("jPush.masterSecret");/*** * @auth Ren* @date 2018年5月2日* @decripe 定时推送,利用DeviceSN做别名,点对点发送,同时记录返回的msg_id* @param obj推送对象,deviceSN设备识别码,定时的时间date,MsgType推送的业务类型(APIConstants中定义),*            name推送的名称*/public static ScheduleResult sendSchedulePush(Object obj, String deviceSN, Date date, String MsgType, String name) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = format.format(date);ScheduleResult result = null;PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.alias(deviceSN)).build();try {result = jPushClient.createSingleSchedule(name, time, push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());}return result;}/*** * @auth Ren* @date 2018年5月2日* @decripe 定时推送,推送到所有设备,同时记录返回的msg_id* @param obj推送对象,定时的时间date,MsgType推送的业务类型(APIConstants中定义),name推送的名称*/public static ScheduleResult sendSchedulePushAll(Object obj, Date date, String MsgType, String name) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = format.format(date);ScheduleResult result = null;PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.all()).build();try {result = jPushClient.createSingleSchedule(name, time, push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());}return result;}/*** * @auth Ren* @date 2018年5月2日* @decripe 删除定时任务* @param scheduleId定时任务的Id*/public static void DeleteSchedule(String scheduleId) {try {JPushClient jPushClient = new JPushClient(masterSecret, appKey);jPushClient.deleteSchedule(scheduleId);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());}}/*** * @auth Ren* @date 2018年5月2日* @decripe:把obj对象的json串推送到别名为DeviceSN的设备上,同时记录返回的msg_id* @param obj推送对象,deviceSN设备识别码,MsgType推送的业务类型(APIConstants中定义)*/public static PushResult SendPush(Object obj, String DeviceSN, String MsgType) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.alias(DeviceSN)).build();PushResult result = null;try {result = jPushClient.sendPush(push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);LOG.error("Sendno: " + push.getSendno());} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());LOG.info("Msg ID: " + e.getMsgId());LOG.error("Sendno: " + push.getSendno());}if (result == null) {throw new APIException("与设备通话失败,请联系管理员处理!");}return result;}/*** * @auth Ren* @date 2018年5月2日* @decripe 把obj对象的json串推送到所有设备上* @param obj推送对象,MsgType推送的业务类型(APIConstants中定义)*/public static PushResult SendPushAll(Object obj, String MsgType) {JPushClient jPushClient = new JPushClient(masterSecret, appKey);String objStr = ObjectToJson(obj);PushPayload push = PushPayload.newBuilder().setPlatform(Platform.all()).setMessage(Message.newBuilder().setMsgContent(objStr).addExtras(Collections.singletonMap("MsgType", MsgType)).build()).setAudience(Audience.all()).build();PushResult result = null;try {result = jPushClient.sendPush(push);LOG.info("Got result - " + result);LOG.info("send objStr - " + objStr);System.out.println(result);System.out.println(objStr);} catch (APIConnectionException e) {LOG.error("Connection error. Should retry later. ", e);LOG.error("Sendno: " + push.getSendno());} catch (APIRequestException e) {LOG.error("Error response from JPush server. Should review and fix it. ", e);LOG.info("HTTP Status: " + e.getStatus());LOG.info("Error Code: " + e.getErrorCode());LOG.info("Error Message: " + e.getErrorMessage());LOG.info("Msg ID: " + e.getMsgId());LOG.error("Sendno: " + push.getSendno());}if (result == null) {throw new APIException("推送失败,请联系管理员处理!");}return result;}public static String ObjectToJson(Object o) {String json = JsonUtil.getJsonString4JavaPOJO(o, "yyyy-MM-dd HH:mm:ss");return json;}
}

转载于:https://www.cnblogs.com/self-studyRen/p/9141725.html

极光推送服务端API(定时推送任务,推送到指定设备,推送到所有设备)相关推荐

  1. 阿里移动推送服务端API

    极光推送最近老抽风,然后推送方案又一次改变,这次给大家带来的是阿里的移动推送服务端api: 首先是引入的依赖 <dependency><groupId>com.aliyun&l ...

  2. OPPO消息推送服务器,OPPO推送平台服务端API.PDF

    OPPO推送平台服务端API.PDF OPPO推送平台服务端API 修订记录: 版本号 修订人 修订日期 修订描述 V0.1 宫建涛 2017-03-28 初始版本 V0.2 宫建涛 2017-07- ...

  3. php推送手机,PHP_解析php做推送服务端实现ios消息推送,准备工作1.获取手机注册应用 - phpStudy...

    解析php做推送服务端实现ios消息推送 准备工作1.获取手机注册应用的deviceToken(iphone手机注册应用时返回唯一值deviceToken) 2.获取ck.pem文件(做手机端的给) ...

  4. 【HMS Core】华为登录后返回错误码 8 、账号服务如何授权、推送服务端获取用户信息异常

    1.[HMS core][游戏登陆][问题描述] 调用华为登录后返回错误码 8 [解决方案] 错误码8的话一般在定义为内部错误(引起该错误码的原因很多),但是一般重试基本可以解决该问题(错误码).如果 ...

  5. 详解个推java服务端集成

    随时随地技术实战干货,获取项目源码.学习资料,请关注源代码社区公众号(ydmsq666) 一.简介 个推是商用级的移动应用消息推送云服务解决方案,客户端SDK支持Android和iOS两大平台,云端支 ...

  6. 云信服务器代码,云信一键登录服务端API文档-一键登录-网易云信开发文档

    一键登录 > 服务端 API 文档 一键登陆服务端API文档 接口概述 API调用说明 本文档中,所有调用网易云信服务端接口的请求都需要按此规则校验. API checksum校验 以下参数需要 ...

  7. C#开发BIMFACE系列19 服务端API之获取模型数据4:获取多个构件的共同属性

    系列目录     [已更新最新开发文章,点击查看详细] 在前几篇博客中介绍了一个三维文件/模型包含多个构建,每个构建又是由多种材质组成,每个构建都有很多属性.不同的构建也有可能包含相同的属性. 上图中 ...

  8. C#开发BIMFACE系列20 服务端API之获取模型数据5:批量获取构件属性

    系列目录     [已更新最新开发文章,点击查看详细] 在<C#开发BIMFACE系列18 服务端API之获取模型数据3:获取构件属性>中介绍了获取单个文件/模型的单个构建的属性,本篇介绍 ...

  9. C#开发BIMFACE系列18 服务端API之获取模型数据3:获取构件属性

    系列目录     [已更新最新开发文章,点击查看详细] 本篇主要介绍如何获取单文件/模型下单个构建的属性信息. 请求地址:GET https://api.bimface.com/data/v2/fil ...

最新文章

  1. Caused by: java.sql.BatchUpdateException
  2. go的打包依赖构建工具-dep
  3. hadoop商品推荐_百战卓越班学员学习经验分享:商品推荐
  4. 数据结构实验之栈与队列十一:refresh的停车场
  5. js中输出变量的类型和输出对象的的属性/方法/成员函数
  6. nginx 高并发优化参数
  7. 疯狂动物消消乐html5游戏在线玩,疯狂动物消消乐免费
  8. 信息学奥赛一本通(1007:计算(a+b)×c的值)
  9. 第一个C#程序—C#基础回顾
  10. 「leetcode」216.组合总和【回溯算法】详解!
  11. 温州大学c语言作业布置的网站,2016年温州大学物理与电子信息工程学院综合卷之C语言程序设计复试笔试仿真模拟题...
  12. 与时俱进 挪威央行运用大数据预测经济情况
  13. 2021编辑器Eclipse汉化中文教程
  14. Python中in的用法小结
  15. ButterKnife被弃用,ViewBinding才是findView的未来?
  16. poc测试环境准备_POC测试经验总结
  17. emeditor的快捷键
  18. 2023杭州之江中复百日誓师动员大会
  19. JavaScript html 图片滑动切换效果,幻灯片式切换,新闻展示,滚动新闻
  20. java程序员从笨鸟到菜鸟之_Java程序员从笨鸟到菜鸟之(九十一)跟我学jquery(七)jquery动画大体验...

热门文章

  1. java控制图片移动_多线程控制图片移动
  2. 适合利用计算机模拟的是,计算机模拟在数学建模中的应用
  3. 倒数日电脑版_应用日报|iOS 或更名为 iPhoneOS,倒数日 Mac 版上线限时免费
  4. document中输出html字符串流,HTML DOMDocument从段落后面的标签中获取字符串
  5. Python3求解找到小镇的法官问题
  6. yxcms安装环境php,Windows7下PHP开发环境安装配置图文方法
  7. 嵌入式linux文件系统启动,嵌入式Linux之文件系统启动分析【原创】
  8. linux shc shell脚本_详解shell脚本加密解密软件—gzese和shc
  9. 光端机与光电转换器的区别介绍
  10. 【渝粤教育】国家开放大学2018年春季 0177-22T电机学(二) 参考试题