首页 > 其他分享 >springboot整合极光推送使用的基本案例

springboot整合极光推送使用的基本案例

时间:2022-09-03 11:11:43浏览次数:70  
标签:springboot get 极光 parm newBuilder build msg 推送 JPushClient

1.maven依赖

<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.9</version>
</dependency>

2.工具类

/**
* 极光推送工具类
* Created by liftsail on 2022/9/3.
*/
public class JPushUtil {

// 设置好账号的app_key和masterSecret是必须的
private static String APP_KEY = "";
private static String MASTER_SECRET = "";


//极光推送>>Android
//Map<String, String> parm是我自己传过来的参数,可以自定义参数
public static void jpushAndroid(Map<String, String> parm) {

//创建JPushClient(极光推送的实例)
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
//推送的关键,构造一个payload
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android())//指定android平台的用户
// .setAudience(Audience.all())//你项目中的所有用户
.setAudience(Audience.alias(parm.get("alias")))//设置别名发送,单发,点对点方式
//.setAudience(Audience.tag("tag1"))//设置按标签发送,相当于群发
// .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
.setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm)) //发送内容
.setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(7200).build())
// apnProduction指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式) 不用设置也没关系
// TimeToLive 两个小时的缓存时间
.setMessage(Message.content(parm.get("msg")))//自定义信息
.build();

try {
PushResult pu = jpushClient.sendPush(payload);
System.out.println(pu.toString());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}

//极光推送>>ios
//Map<String, String> parm是我自己传过来的参数,可以自定义参数
public static void jpushIOS(Map<String, String> parm) {

//创建JPushClient
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())//ios平台的用户
.setAudience(Audience.all())//所有用户
//.setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(parm.get("msg"))
.setBadge(+1)
.setSound("happy")//这里是设置提示音(更多可以去官网看看)
.addExtras(parm)
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(false).build())
.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
.build();

try {
PushResult pu = jpushClient.sendPush(payload);
System.out.println(pu.toString());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}


/**
* 极光推送>>All所有平台
*
* @param parm
*/
public static void jpushAll(Map<String, String> parm) {

//创建JPushClient
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
//创建option
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.all()) //所有平台的用户
.setAudience(Audience.alias(parm.get("alias")))//设置别名发送,单发,点对点方式
//.setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder() //发送ios
.setAlert(parm.get("msg")) //消息体
.setBadge(+1)
.setSound("happy") //ios提示音
.addExtras(parm) //附加参数
.build())
.addPlatformNotification(AndroidNotification.newBuilder() //发送android
.addExtras(parm) //附加参数
.setAlert(parm.get("msg")) //消息体
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
System.out.println(pu.toString());
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}

3.业务代码中应用

//准备参数
Map<String, String> parm = new HashMap<>();
parm.put("alias", equipment.getSerialNumber());//设置别名
parm.put("msg", "1001");//消息体
parm.put("title", "");
parm.put("id", + );
parm.put("type", "1");
JPushUtil.jpushAll(parm);//方法调用

标签:springboot,get,极光,parm,newBuilder,build,msg,推送,JPushClient
From: https://www.cnblogs.com/liftsail/p/16652176.html

相关文章

  • springboot学习
    springboot学习官方文档:https://spring.io/projects/spring-boot1、简介1.1、什么是spirngboot?springboot在spring的基础之上,搭建起来的框架,能够帮助我们整合市面上......
  • springboot配置类@ConfigurationProperties报错Not registered via @EnableConfigurat
    添加一个@Component可以解决此问题,只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能。......
  • springboot启动图标banner
    将springboot项目中的banner.txt文件替换成下面代码即可${AnsiColor.BRIGHT_YELLOW}┏━┓┏━┓┏┛......
  • 连接数据库 springboot环境下默认使用的是h2数据库,你没有对h2进行配置,这里我是mysql数
    springboot环境下默认使用的是h2数据库,你没有对h2进行配置,这里我是mysql数据库,需要配置一下mysql数据库Description:Failedtobindpropertiesunder''tocom.za......
  • Linux--部署SpringBoot应用
    打包SpringBoot项目部署方式一:手动部署1、将打包好的jar包上传到Linux服务器中mkdir-p/opt/java62/app2、前台启动SpringBoot应用编译jar包:java-jarhelloworld......
  • springboot的简单使用(3)
    1.5第五章接口架构风格—RESTful1.5.1认识RESTREST(英文:RepresentationalStateTransfer,简称REST)一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客......
  • 放弃FastDFS!SpringBoot整合MinIO实现分布式文件服务,真香!
    今天分享一个非常不错且开源的分布式存储组件MinIO,有很多朋友在用。什么是MinIO?Minio是个基于Golang编写的开源对象存储套件,基于ApacheLicensev2.0开源协议,虽然轻量......
  • 外置tomcat方式部署springboot
    目录外置tomcat方式部署springboot1、打包方式的改变2、去除springboot中内置的tomcat(.xml中)3、启动类中增加继承SpringBootServletInitializer,重写configure方法外置tom......
  • springboot~Screw生成数据库文档
    数据库说明文档,在我们开发项目时是非常必要的,有时项目交付时,客户也是需要让我们提供的,而如果人工编写,比如耗时,通过screw组件来生成文档,非常方便。源代码和使用:https://g......
  • springboot启动报错
    springboot启动报错Description:FieldstationInformationServiceincom.htkj.aicheck.service.TrainCheckServicerequiredabeanoftype'com.htkj.ai.server.service......