极光推送的代码实现

简介: 极光推送的代码实现

一:去官网下载SDK或者直接在maven项目中进行相应配置

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>cn.jpush.api</groupId>
        <artifactId>jpush-client</artifactId>
        <version>3.2.17</version>
    </dependency>
  </dependencies>

二:按照API文档示例进行相应的修改添加

进行推送的关键在于构建一个 PushPayload 装载对象。

推送的话大致分为:

  1. 对所有平台所有设备上的进行推送
  2. 对所有平台指定推送目标进行推送
  3. 对Android平台上的指定设备进行推送
  4. 对IOS平台上的指定设备进行推送
  5. 对Android+IOS平台上的指定设备进行推送
  6. 给指定设备发送SMS信息

接下来我们需要在SDK的基础之上设置合适的调用参数进行取用

package com.test.push.messagepush01;
import java.util.Map;
import cn.jiguang.common.ClientConfig;
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.SMS;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
public class PushTest{
    private static final String MASTER_SECRET="9a3869c9d2fec8333db*****";//JPush服务器端下发的master_key
    private static final String APP_KEY="0e621306bf07eb4eefc*****";//JPush服务器端下发的app_key
    /**
     * 构建推送对象:对所有平台,所有设备,内容为 alert的通知
     * @param alter
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert(String alter) {
        return PushPayload.alertAll(alter);
    } 
    /**
     * 所有平台,推送目标是别名为 "alias",通知内容为 alert
     * @param alias
     * @param alert
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String alias,Object alert) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.alert(alert))
                .build();
    }
    /**
     * 构建推送对象:平台是 Android,目标是 tag的设备,通知内容是alert,并且标题为title。
     * @param tag
     * @param alert
     * @param title
     * @param extras
     * @return
     */
    public static PushPayload buildPushObject_android_tag_alertWithTitle(String tag,String alert,String title,Map<String, String> extras) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(alert, title, extras))
                .build();
    }
    /**
     * 构建推送对象:平台是 iOS,推送目标是 tags(可以是一个设备也可以是多个设备),推送内容同时包括通知与消息 - 通知信息是alert,消息内容是 msgContent,角标数字为badge(应用程序左上角或者右上角的数字),通知声音为sound,并且附加字段 from = "JPush"。
     * 通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。
     * APNs 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)
     * @param alert
     * @param msgContent
     * @param badge
     * @param sound
     * @param tags
     * @return
     */
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(Object alert,String msgContent,int badge,String sound,String...tags) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and(tags))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(badge)
                                .setSound(sound)
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                 .setMessage(Message.content(msgContent))
                 .setOptions(Options.newBuilder()
                         .setApnsProduction(true)
                         .build())
                 .build();
    }
    /**
     * 构建推送对象:平台是 Andorid 与 iOS,推送的设备有(推送目标为tags和推送目标别名为aliases),推送内容是 - 内容为 msg_content的消息,并且附加字段 from = JPush。
     * @param msg_content
     * @param tags
     * @param aliases
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String msg_content,String[] tags,String[] aliases) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(tags))
                        .addAudienceTarget(AudienceTarget.alias(aliases))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msg_content)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }
    /**
     * 构建推送对象:推送内容包含SMS信息
     * @param title
     * @param sendSMSContent
     * @param delayTime
     * @param aliases
     */
    public static void testSendWithSMS(String title,String sendSMSContent,int delayTime,String... aliases) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        try {
            SMS sms = SMS.content(sendSMSContent, delayTime);
            PushResult result = jpushClient.sendAndroidMessageWithAlias(title, sendSMSContent, sms, aliases);
            System.out.println("Got result - " + result);
        } catch (APIConnectionException e) {
            System.out.println("Connection error. Should retry later. "+e);
        } catch (APIRequestException e) {
            System.out.println("Error response from JPush server. Should review and fix it. "+e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
        }
    }
    public static void main(String[] args) {
        String master_secret=PushTest.MASTER_SECRET;
        String app_key=PushTest.APP_KEY;
        JPushClient jpushClient = new JPushClient(master_secret,app_key, null, ClientConfig.getInstance());
        PushPayload payload = PushTest.buildPushObject_all_all_alert("消息推送");
        //PushPayload payload=PhicommPush.buildPushObject_android_tag_alertWithTitle("tag1", "123", "123", null);
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);
        } catch (APIConnectionException e) {
            // Connection error, should retry later
            System.out.print("Connection error, should retry later "+e);
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            System.out.println("根据返回的错误信息核查请求是否正确"+e);
            System.out.println("HTTP 状态信息码: " + e.getStatus());
            System.out.println("JPush返回的错误码: " + e.getErrorCode());
            System.out.println("JPush返回的错误信息: " + e.getErrorMessage());
        }
    }
}

三、查看推送日志:

03425934a1e849c13a562070572f9888.png

四、在极光开发者服务中查看刚才推送的消息

登录到极光开发者页面中https://www.jiguang.cn/


ed3ee3cc2aadbc2c5834bedcf8c8eb97.png

推送多环境配置见:http://www.cnblogs.com/lingyejun/p/7943779.html


目录
相关文章
|
机器学习/深度学习 自然语言处理 算法
在Python中进行自然语言处理(NLP)的文本预处理
在Python中进行自然语言处理(NLP)的文本预处理
675 1
|
9月前
|
监控 API 数据安全/隐私保护
OpenClaw阿里云/本地台部署+商业落地指南|5类实战案例+API配置与避坑要点
2026年,OpenClaw(Clawdbot)已从技术工具演进为具备商业落地能力的经济代理载体,其核心价值并非直接“生成收益”,而是通过自动化执行、实时数据监控、决策效率提升构建竞争优势,最终转化为可量化的经济价值。通过对公开案例的梳理发现,成功的商业落地多集中在决策辅助、自动化流程、知识服务三大方向,而落地的前提是完成稳定的部署与合理的模型配置。本文结合5个真实商业案例,拆解OpenClaw的商业应用逻辑与避坑要点,完整提供阿里云及本地多系统部署流程、免费大模型API配置方法,形成一套“部署-配置-落地-避坑”的完整方案,全程不含营销词汇,兼顾实用性与可操作性。
961 0
|
10月前
|
人工智能 安全 搜索推荐
阿里云部署OpenClaw活动:9.9元定制AI助理,快速拥有OpenClaw超级助理
阿里云推出OpenClaw部署活动,助力用户快速构建7*24小时全天候AI智能助理。活动主打9.9元起的轻量应用服务器方案(2核2G/200M带宽),并提供无影云电脑(企业版/个人版)、无影AgentBay及ECS等多种部署选择,支持钉钉、飞书等IM工具集成。用户可通过简单配置实现邮件处理、日程规划、代码开发等任务执行,完成从“聊天工具”到“可执行智能体”的升级。配合阿里云百炼先用后返(最高返200元)及Coding Plan套餐优惠,OpenClaw以高性价比赋能职场、编程及生活场景,打造专属AI员工。
1643 1
|
关系型数据库 MySQL 数据库
MySQL - 不使用外键约束的实操(二)
MySQL - 不使用外键约束的实操(二)
1281 0
|
5月前
|
人工智能 前端开发 Linux
Codex 桌面版安装 + CC Switch 接入第三方 API 完整教程(2026 最新)
2026最新教程:手把手教你安装Codex桌面版,通过CC Switch v3.17.0一键接入Fenno等国产API(兼容OpenAI Responses格式),跳过账号登录,完整启用代码审查、多步任务与上下文感知功能。零基础友好,全程图文实操。(239字)
19316 8
|
存储 负载均衡 NoSQL
Tomcat 9.X(9.0.74)集群实现Session共享(基于redisson)
本文主要介绍了tomcat集群环境下基于redis+Redisson实现session共享,分享给大家,供广大从业者学习和参考。
13924 4
|
前端开发 JavaScript IDE
构建高效的前端开发环境
本文探讨了如何构建一个高效的前端开发环境,包括选择合适的开发工具、优化工作流程和提高代码质量的方法。通过本文的指导,读者可以更好地提升前端开发效率,快速构建出高质量的网站和应用程序。
|
安全 Linux
CentOS下载ISO镜像的方法
访问CentOS官方网站(https://www.centos.org/download/),在“Downloads”页面找到ISO镜像下载链接,选择所需版本和架构(如x86_64)开始下载。CentOS分为Linux版和Stream版,前者每两年发行一次并提供10年安全维护,后者为滚动更新。旧版本可在Vault(https://vault.centos.org/)下载。建议选择DVD格式镜像,包含完整系统和常用软件。
41490 16
CentOS下载ISO镜像的方法

热门文章

最新文章