工作中经常会遇到服务器向App推送消息的需求,一般企业中选择用极光推送的比较多,在集成极光时发现极光的文档并不完整,网上的文章也很多不能直接使用,这里列出我在工作中集成极光的全部代码,只需要按照如下代码保证一次性实现。

1.pom.xml

cn.jpush.api    jpush-client    3.3.10cn.jpush.api    jiguang-common    1.1.4

2.application.yml

jpush:  appKey: xxx  masterSecret: xxxx  apnsProduction: false   # 是否生成环境,true表示生成环境

3.MyJPushClient

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.Options;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.push.model.notification.AndroidNotification;import cn.jpush.api.push.model.notification.IosNotification;import cn.jpush.api.push.model.notification.Notification;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.util.List;/** * 极光推送客户端 * * @author Mengday Zhang * @version 1.0 * @since 2019-04-01 */@Componentpublic class MyJPushClient {    @Value("${jpush.appKey}")    private String appKey;    @Value("${jpush.masterSecret}")    private String masterSecret;    @Value("${jpush.apnsProduction}")    private boolean apnsProduction;    private static JPushClient jPushClient = null;    private static final int RESPONSE_OK = 200;    private static final Logger logger = LoggerFactory.getLogger(MyJPushClient.class);    public JPushClient getJPushClient() {        if (jPushClient == null) {            jPushClient = new JPushClient(masterSecret, appKey);        }        return jPushClient;    }    /**     * 推送到alias列表     *     * @param alias             别名或别名组     * @param notificationTitle 通知内容标题     * @param msgTitle          消息内容标题     * @param msgContent        消息内容     * @param extras            扩展字段     */    public void sendToAliasList(List alias, String notificationTitle, String msgTitle, String msgContent, String extras) {        PushPayload pushPayload = buildPushObject_all_aliasList_alertWithTitle(alias, notificationTitle, msgTitle, msgContent, extras);        this.sendPush(pushPayload);    }    /**     * 推送到tag列表     *     * @param tagsList          Tag或Tag组     * @param notificationTitle 通知内容标题     * @param msgTitle          消息内容标题     * @param msgContent        消息内容     * @param extras            扩展字段     */    public void sendToTagsList(List tagsList, String notificationTitle, String msgTitle, String msgContent, String extras) {        PushPayload pushPayload = buildPushObject_all_tagList_alertWithTitle(tagsList, notificationTitle, msgTitle, msgContent, extras);        this.sendPush(pushPayload);    }    /**     * 发送给所有安卓用户     *     * @param notificationTitle 通知内容标题     * @param msgTitle          消息内容标题     * @param msgContent        消息内容     * @param extras        扩展字段     */    public void sendToAllAndroid(String notificationTitle, String msgTitle, String msgContent, String extras) {        PushPayload pushPayload = buildPushObject_android_all_alertWithTitle(notificationTitle, msgTitle, msgContent, extras);        this.sendPush(pushPayload);    }    /**     * 发送给所有IOS用户     *     * @param notificationTitle 通知内容标题     * @param msgTitle          消息内容标题     * @param msgContent        消息内容     * @param extras        扩展字段     */    public void sendToAllIOS(String notificationTitle, String msgTitle, String msgContent, String extras) {        PushPayload pushPayload = buildPushObject_ios_all_alertWithTitle(notificationTitle, msgTitle, msgContent, extras);        this.sendPush(pushPayload);    }    /**     * 发送给所有用户     *     * @param notificationTitle 通知内容标题     * @param msgTitle          消息内容标题     * @param msgContent        消息内容     * @param extras        扩展字段     */    public void sendToAll(String notificationTitle, String msgTitle, String msgContent, String extras) {        PushPayload pushPayload = buildPushObject_android_and_ios(notificationTitle, msgTitle, msgContent, extras);        this.sendPush(pushPayload);    }    private PushResult sendPush(PushPayload pushPayload) {        logger.info("pushPayload={}", pushPayload);        PushResult pushResult = null;        try {            pushResult = this.getJPushClient().sendPush(pushPayload);            logger.info("" + pushResult);            if (pushResult.getResponseCode() == RESPONSE_OK) {                logger.info("push successful, pushPayload={}", pushPayload);            }        } catch (APIConnectionException e) {            logger.error("push failed: pushPayload={}, exception={}", pushPayload, e);        } catch (APIRequestException e) {            logger.error("push failed: pushPayload={}, exception={}", pushPayload, e);        }        return pushResult;    }    /**     * 向所有平台所有用户推送消息     *     * @param notificationTitle     * @param msgTitle     * @param msgContent     * @param extras     * @return     */    public PushPayload buildPushObject_android_and_ios(String notificationTitle, String msgTitle, String msgContent, String extras) {        return PushPayload.newBuilder()                .setPlatform(Platform.android_ios())                .setAudience(Audience.all())                .setNotification(Notification.newBuilder()                        .setAlert(notificationTitle)                        .addPlatformNotification(AndroidNotification.newBuilder()                                .setAlert(notificationTitle)                                .setTitle(notificationTitle)                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("androidNotification extras key", extras)                                .build()                        )                        .addPlatformNotification(IosNotification.newBuilder()                                // 传一个IosAlert对象,指定apns title、title、subtitle等                                .setAlert(notificationTitle)                                // 直接传alert                                // 此项是指定此推送的badge自动加1                                .incrBadge(1)                                // 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音                                .setSound("default")                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("iosNotification extras key", extras)                                // 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification                                // .setContentAvailable(true)                                .build()                        )                        .build()                )                // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .setTitle(msgTitle)                        .addExtra("message extras key", extras)                        .build())                .setOptions(Options.newBuilder()                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义                        .setApnsProduction(apnsProduction)                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录                        .setSendno(1)                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒                        .setTimeToLive(86400)                        .build())                .build();    }    /**     * 向所有平台单个或多个指定别名用户推送消息     *     * @param aliasList     * @param notificationTitle     * @param msgTitle     * @param msgContent     * @param extras     * @return     */    private PushPayload buildPushObject_all_aliasList_alertWithTitle(List aliasList, String notificationTitle, String msgTitle, String msgContent, String extras) {        // 创建一个IosAlert对象,可指定APNs的alert、title等字段        // IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();        return PushPayload.newBuilder()                // 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台                .setPlatform(Platform.all())                // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id                .setAudience(Audience.alias(aliasList))                // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发                .setNotification(Notification.newBuilder()                        // 指定当前推送的android通知                        .addPlatformNotification(AndroidNotification.newBuilder()                                .setAlert(notificationTitle)                                .setTitle(notificationTitle)                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("androidNotification extras key", extras)                                .build())                        // 指定当前推送的iOS通知                        .addPlatformNotification(IosNotification.newBuilder()                                // 传一个IosAlert对象,指定apns title、title、subtitle等                                .setAlert(notificationTitle)                                // 直接传alert                                // 此项是指定此推送的badge自动加1                                .incrBadge(1)                                // 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音                                .setSound("default")                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("iosNotification extras key", extras)                                // 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification                                // 取消此注释,消息推送时ios将无法在锁屏情况接收                                // .setContentAvailable(true)                                .build())                        .build())                // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .setTitle(msgTitle)                        .addExtra("message extras key", extras)                        .build())                .setOptions(Options.newBuilder()                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义                        .setApnsProduction(apnsProduction)                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录                        .setSendno(1)                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;                        .setTimeToLive(86400)                        .build())                .build();    }    /**     * 向所有平台单个或多个指定Tag用户推送消息     *     * @param tagsList     * @param notificationTitle     * @param msgTitle     * @param msgContent     * @param extras     * @return     */    private PushPayload buildPushObject_all_tagList_alertWithTitle(List tagsList, String notificationTitle, String msgTitle, String msgContent, String extras) {        //创建一个IosAlert对象,可指定APNs的alert、title等字段        //IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();        return PushPayload.newBuilder()                // 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台                .setPlatform(Platform.all())                // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id                .setAudience(Audience.tag(tagsList))                // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发                .setNotification(Notification.newBuilder()                        // 指定当前推送的android通知                        .addPlatformNotification(AndroidNotification.newBuilder()                                .setAlert(notificationTitle)                                .setTitle(notificationTitle)                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("androidNotification extras key", extras)                                .build())                        // 指定当前推送的iOS通知                        .addPlatformNotification(IosNotification.newBuilder()                                // 传一个IosAlert对象,指定apns title、title、subtitle等                                .setAlert(notificationTitle)                                // 直接传alert                                // 此项是指定此推送的badge自动加1                                .incrBadge(1)                                // 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音                                .setSound("default")                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("iosNotification extras key", extras)                                // 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification                                // 取消此注释,消息推送时ios将无法在锁屏情况接收                                // .setContentAvailable(true)                                .build())                        .build())                // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .setTitle(msgTitle)                        .addExtra("message extras key", extras)                        .build())                .setOptions(Options.newBuilder()                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义                        .setApnsProduction(apnsProduction)                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录                        .setSendno(1)                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;                        .setTimeToLive(86400)                        .build())                .build();    }    /**     * 向android平台所有用户推送消息     *     * @param notificationTitle     * @param msgTitle     * @param msgContent     * @param extras     * @return     */    private PushPayload buildPushObject_android_all_alertWithTitle(String notificationTitle, String msgTitle, String msgContent, String extras) {        return PushPayload.newBuilder()                // 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台                .setPlatform(Platform.android())                // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id                .setAudience(Audience.all())                // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发                .setNotification(Notification.newBuilder()                        // 指定当前推送的android通知                        .addPlatformNotification(AndroidNotification.newBuilder()                                .setAlert(notificationTitle)                                .setTitle(notificationTitle)                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("androidNotification extras key", extras)                                .build())                        .build()                )                // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .setTitle(msgTitle)                        .addExtra("message extras key", extras)                        .build())                .setOptions(Options.newBuilder()                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义                        .setApnsProduction(apnsProduction)                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录                        .setSendno(1)                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒                        .setTimeToLive(86400)                        .build())                .build();    }    /**     * 向ios平台所有用户推送消息     *     * @param notificationTitle     * @param msgTitle     * @param msgContent     * @param extras     * @return     */    private PushPayload buildPushObject_ios_all_alertWithTitle(String notificationTitle, String msgTitle, String msgContent, String extras) {        return PushPayload.newBuilder()                // 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台                .setPlatform(Platform.ios())                // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id                .setAudience(Audience.all())                // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发                .setNotification(Notification.newBuilder()                        // 指定当前推送的android通知                        .addPlatformNotification(IosNotification.newBuilder()                                // 传一个IosAlert对象,指定apns title、title、subtitle等                                .setAlert(notificationTitle)                                // 直接传alert                                // 此项是指定此推送的badge自动加1                                .incrBadge(1)                                // 此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音                                .setSound("default")                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)                                .addExtra("iosNotification extras key", extras)                                // 此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification                                // .setContentAvailable(true)                                .build())                        .build()                )                // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别                .setMessage(Message.newBuilder()                        .setMsgContent(msgContent)                        .setTitle(msgTitle)                        .addExtra("message extras key", extras)                        .build())                .setOptions(Options.newBuilder()                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义                        .setApnsProduction(apnsProduction)                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录                        .setSendno(1)                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒                        .setTimeToLive(86400)                        .build())                .build();    }    public static void main(String[] args) {//        MyJPushClient jPushUtil = new MyJPushClient();//        List aliasList = Arrays.asList("239");//        String notificationTitle = "notificationTitle";//        String msgTitle = "msgTitle";//        String msgContent = "msgContent";//        jPushUtil.sendToAliasList(aliasList, notificationTitle, msgTitle, msgContent, "exts");    }}

4.test

@RunWith(SpringRunner.class)@SpringBootTestpublic class JPushApplicationTests {    @Autowired    private MyJPushClient jPushClient;    @Test    public void testJPush() {        List aliasList = Arrays.asList("239");        String notificationTitle = "notification_title";        String msgTitle = "msg_title";        String msgContent = "msg_content";        jPushClient.sendToAliasList(aliasList, notificationTitle, msgTitle, msgContent, "exts");    }}

获取示例源码请转发该文章并关注Java实用技术,私信获取极光推送源码。

极光推送指定用户推送_干货|SpringBoot集成极光推送完整实现代码(建议收藏)...相关推荐

  1. 极光推送指定用户推送_App用户都睡着了?是时候用推送和活动唤醒一波了!

    想要运营好一款App,引流.留存.促活三大环节必不可少.引流解决了用户来的问题,留存解决了用户留下来的问题,而促活解决的是让一部分新注册用户以及许久没有动静的老用户,在平台中再次活跃起来.今天,我们就 ...

  2. 极光推送 简书android,(Android)react-native集成极光推送

    在Android中使用reactnative集成极光推送步骤如下: (1)在AndroidManifest中声明网络权限,获取包名到极光推送官网添加应用,获取AppKey,该key需要注册到应用中以获 ...

  3. java推箱子随机地图的产生_为什么没有人研究推箱子地图自动生成算法?算法随机生成地图,不需要地图库!?...

    其实是有的,可以参考 Ty Taylor 的 The Art and Science of Procedural Puzzle Generation,https://www.youtube.com/w ...

  4. 物联网卡设置_物联网卡这样设置一下上网全程4G!建议收藏!

    物联网卡这样设置一下上网全程4G!建议收藏! 由于手机的厂商不同或者运营商设置问题,有极少数手机需要设置接入点方可使用数据网络,每个运营商的APN接入点都是不变的,并且有用户询问网速最快接入点,以下是 ...

  5. php个推设置指定用户收到推送消息,请问怎么给指定用户推送信息

    大家好,我第一次用这个系统.现在的需求是:A怎么将信息只推送给B,或者系统指定给B推送信息.能不能根据$socket->id单独去推送信息?下面是代码,请问如何去实现? $m = new Mem ...

  6. 微信小程序推送消息java开发_干货 | 微信小程序推送消息简单Demo

    在开始前,你需要准备:注册微信小程序 一个简单的springBoot 项目 微信开发者工具 正式 微信小程序发送消息主要通过WxMaTemplateMessage 类来推送 public class ...

  7. sap 用户权限表_干货丨SAP系统的RPA实施技巧

    SAP SAP(System Applications and Products)是SAP公司的产品--企业管理解决方案的软件名称. SAP是其ERP(Enterprise-Wide Resource ...

  8. 需要用sq语句 修改大批量用户的密码_网站文章seo优化及修改已收录文章建议

    一.网站文章seo优化: <1> 文章标题优化 1.文章标题要新颖,不必要太看重语法,逻辑,但一定要有新意. 2.符合用户搜索需求,直接戳中用户的痛点. 3.标题中融入文章关键词,关键词出 ...

  9. python 删除断点_给 Python 开发者的四条忠告!强烈建议收藏

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 天元浪子 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

最新文章

  1. 管理者和领导者有哪些区别?
  2. R卡方检验(CHI-SQUARE TEST)
  3. 【bzoj3866】The Romantic Hero dp
  4. svm学习之线性部分总结
  5. Allegro中元器件位号重排并反标回原理图
  6. 写 Python 到底用什么编辑器好?鹅厂程序猿吵翻了
  7. 说说基于网络的五种IO模型
  8. ExtJs懒人笔记(2) ExtJs页面布局
  9. 教你轻松构建基于 Serverless 架构的小程序
  10. Ubuntu 修复windows启动项
  11. EDEM软件简单介绍
  12. QT笔记——Q_Q 和Q_D 学习
  13. Open3D 渐进式形态学滤波
  14. 每个前端都值得拥有自己的组件库,就像每个冬天都拥有春秋裤
  15. c语言编写消防车声音程序教程,51单片机蜂鸣器模拟救护车消防车等各种报警喇叭声音的学习源代码...
  16. Springboot+Redis初体验
  17. nrf52 ESB通信协议底层探讨
  18. HTML通过JS动态生成简单网页
  19. 如何让游戏手柄joystick的按键映射键盘keyboard按键,方便不支持手柄的pygame游戏可以通过简单设置后用手柄进行操控
  20. 误GHOST、误一键恢复灾难应急方案

热门文章

  1. zzulioj1111: 多个整数的逆序输出(函数专题)
  2. 堆排序算法---属于选择排序
  3. tensorflow适用于python版本_tensorflow用python哪个版本更好?
  4. ming window 交叉编译_opencv3编译pc端及交叉编译arm端
  5. pyspark sparksession_PySpark 处理数据和数据建模
  6. mysql核心参数_MySQL技术体系之核心参数
  7. 计算机本地磁盘D无法扩展,计算机上的本地磁盘D突然无法打开,表明它需要格式化...
  8. linux下qq怎么截图,ubuntu 12.04使用QQ截图安装教程
  9. MySQL Replication需要注意的问题
  10. redis-full-check