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

极光推送常用的几个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和masterSecret
protected 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/353549.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 9附加流

Java 9即将发布&#xff01; 它不仅仅是Jigsaw项目 。 &#xff08;我也很惊讶。&#xff09;它给平台带来了很多小的变化&#xff0c;我想一一看一下。 我将标记所有这些帖子&#xff0c;您可以在这里找到它们。 让我们从…开始 流 Streams学习了两个新技巧。 第一个处理前缀…

MFC注册快捷键

1. 使用RegisterHotKey()注册快捷键&#xff1b;2. OnHotKey()函数中响应快捷键&#xff1b;3. 程序退出时&#xff0c;使用UnregisterHotKey(hWnd, m_HotKeyId)取消快捷键注册。

Hibernate---对象的三种状态

Hibernate---对象的三种状态 简而言之&#xff0c;hibernate本就是面向对象的基于ORM的框架&#xff0c;位于dao层&#xff0c;对数据进行操作的框架。我就谈谈hibernate的对象的三种状态。他们分别为&#xff1a;游离&#xff0c;持久和瞬时。通过代码来详解一下吧。 hibernat…

VS2008 C++ 项目添加“依赖”、“库目录”和“包含目录”

1. 添加编译所需要&#xff08;依赖&#xff09;的 lib 文件[解决方案资源管理器]“项目->属性->配置属性->连接器->输入->附加依赖项”里填写“winsock.lib”&#xff0c;多个 lib 以空格隔开。 &#xff08;等同于“#pragma comment(lib, "winsock.lib&q…

IDEA项目搭建四——使用Mybatis实现Dao层

一、引入mybatis及mysql的jar包 可以从阿里云上面查找版本&#xff0c;db操作放在dao层所以打开该层的pom.xml文件&#xff0c;找到<dependencies>节点增加两个引入 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifac…

连词我们…讨厌

最近&#xff0c;我写了与实现相关的名称&#xff0c;并提供了一些示例&#xff0c;这些示例由于方法名称与主体之间的紧密关系而导致方法名称不正确。 有一会儿&#xff0c;我们有以下代码&#xff1a; boolean isComplexOrUnreadableWithTests() { return (complex || unre…

C#常见编译错误

CSharp类型初始值设定项引发异常&#xff1a;类的静态变量初始化遇到异常&#xff0c;或者构造函数中遇到异常

python函数的 全局变量与局部变量

一、函数的全局变量 1、什么是全局变量 顶着头开始写&#xff0c;没有任何缩进&#xff0c;在py文件的任何位置都能调用 #!/usr/bin/env python # _*_ coding:utf8 _*_ name"gouguoqi"name"gouguoqi" def change_name():print ("111",(name)) …

C#程序将DLL包进EXE方法

有时候我们在发布程序的时候只想发布一个EXE&#xff0c;而编写程序的时候往往会有多个DLL&#xff0c;这个时候如果能把这些DLL装进EXE将是一个很令人振奋的事情&#xff0c;事实上对于C#程序有很多方法如下&#xff1a;1. 使用微软的ILMerge&#xff08;缺点&#xff1a;不支…

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI 在重启项目中会遇到[org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [*********] in DispatcherServlet with name SpringMVC 这个…

spring本地化默认英文_Spring3国际化和本地化

spring本地化默认英文我最近想将Spring 3提供的国际化和本地化功能添加到我当前的项目之一中。 我浏览了Spring文档&#xff0c;然后在Internet上搜索以找到一些资源。 但是我找不到能够满足客户要求的资源。 大多数教程都像hello world应用程序&#xff0c;它提供了基本的理解…

我所知道的Ribbon库

QT&#xff1a; http://www.devmachines.com/qtitanribbon-overview.html http://qribbon.sourceforge.net MFC、c#可以到微软官方下载 System.Windows.Forms.Ribbon35.DLL - Type: Managed DLL - An Open Source Ribbon Control for .NET WinForm - Read more: http://…

JUnit 5 –动态测试

在定义测试时&#xff0c;JUnit 4有一个很大的弱点&#xff1a;它必须在编译时发生。 现在&#xff0c;JUnit 5将解决此问题&#xff01; Milestone 1 刚刚发布 &#xff0c;并带有全新的动态测试&#xff0c;该动态测试允许在运行时创建测试。 总览 本系列中有关JUnit 5的其他…

win8.1自带metro应用不工作解决办法

输入如下命令 powershell -ExecutionPolicy Unrestricted Add-AppxPackage -DisableDevelopmentMode -Register $Env:SystemRoot\WinStore\AppxManifest.XML powershell -ExecutionPolicy Unrestricted Add-AppxPackage -DisableDevelopmentMode -Register $Env:SystemRoot\Im…

python基础-网络基础知识和网络编程

之前对这一块的知识,总是记不住,这次正好有系统的学习,所以决定好好的梳理一下 1. 计算机网络基础知识 1.1 互联网协议和OSI模型 *协议模型互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层,如下图所示 *每层运行常见物理设备 *每层运行常见的协议 1.2 基础网络概念…

HDU 1999 不可摸数

不可摸数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5334 Accepted Submission(s): 1405 Problem Descriptions(n)是正整数n的真因子之和&#xff0c;即小于n且整除n的因子和.例如s(12)1234616.如果任…

C语言返回文件大小的功能(fseek和ftell的使用)

需求&#xff1a;有时候读文件时&#xff0c;需要知道文件的字符总的个数&#xff0c;可能是为了提前定义缓冲区大小或者拷贝文件等等。也可以用于动态创建数组。在进行这两个问题之前&#xff0c;先来了解一下两个函数&#xff0c;这两个函数配合就能够实现计算大小的功能。函…

自定义相册、九宫格显示图片

一 自定义相册 结合Glide图片库&#xff0c;加载显示本地图片&#xff0c;并可以实现单选&#xff0c;多选&#xff0c;预览功能。特点 加载最近新增图片&#xff0c;GridView显示分文件夹选择图片支持单选&#xff0c;多选&#xff08;最大9张&#xff09;支持大图预览以库的形…

设计一代码,逆置带头结点的动态单链表L

有两种方法&#xff1a; 一是&#xff1a;用头插法建立单链表&#xff0c;自然而然就实现了逆置的动态链表。 代码&#xff1a; #include<stdio.h> #include<malloc.h> typedef int datatype; typedef struct node {datatype data;struct node * next; }linklist…

dll生成lib

来自http://suddymail.org/show-160-1.html没有尝试过&#xff0c;仅供参考。其他链接&#xff1a;http://hi.baidu.com/songxiuying/item/af67755203840f948d12ed6d为无LIB的DLL制作LIB函数符号输入库 本文介绍了在VC中针对无LIB时的DLL隐式链接,制作可供VC使用的LIB函数符号…