首页 > 其他分享 >极光推送服务端

极光推送服务端

时间:2024-03-25 15:58:04浏览次数:26  
标签:addProperty 极光 JsonObject channel push 推送 服务端

使用极光推送能够实现app的消息推送,需要将 JPush Android SDK 集成到app里,JPush Android SDK 创建到 JPush Cloud 的长连接,为 App 提供长期在线的能力。 当开发者想要及时地推送消息到达 App 时,只需要调用 JPush API 推送,或者使用其他方便的智能推送工具,即可轻松与用户交流。

JPush Android SDK 集成:

SDK 概述 - 极光文档

极光推送消息通道分为 极光通道 和 厂商通道 , 推送时候,如果极光通道在线会默认优先选择极光推动进行推送,如果极光通道不在线会采用厂商通道进行推送,关于厂商通道的办理方式,可以参考官方提供的指南,本文介绍极光推送服务端api的简单操作步骤:

一、引入极光依赖

<!--JPush-->
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.2.2</version>
        </dependency>

二、添加极光相关配置(AppKey和AppSrcit)

使用前需要到极光官网申请应用,并获取到密钥

#极光推送应用配置
JPush:
  APP_KEY: 
  MASTER_SECRET: 

三、初始化极光客户端

@Slf4j
@Configuration
public class JPushConfig {

    @Value("${JPush.APP_KEY}")
    private String APP_KEY;

    @Value("${JPush.MASTER_SECRET}")
    private String MASTER_SECRET;

    @Bean(name = "jpushClient")
    public JPushClient jpushClient() {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        log.info("jpushClient init accomplish");
        return jpushClient;
    }

}

四、推送工具类进行推送

@Slf4j
@Component
public class JPushUtil {

    @Autowired
    private JPushClient jpushClient;

    /**
     * 批量推送
     */
    public PushResult batchPushWarning(List<String> rids, String title, String content) {
        PushPayload pushPayload = buildNotificationPushObject(rids, title, content);
        PushResult pushResult = null;
        try {
            pushResult = jpushClient.sendPush(pushPayload);
        } catch (Exception e) {
            log.error("批量推送 - 极光推送出错");
            e.printStackTrace();
        }
        return pushResult;
    }

    /**
     * 逐条推送
     */
    public PushResult push(String rid, String title, String content) {
        PushPayload pushPayload = buildNotificationPushObject(rid, title, content);
        PushResult pushResult = null;
        try {
            pushResult = jpushClient.sendPush(pushPayload);
        } catch (Exception e) {
            log.error("单挑 - 极光推送出错");
            e.printStackTrace();
        }
        return pushResult;
    }

    /**
     * 批量推送
     */
    public static PushPayload buildNotificationPushObject(List<String> rids, String title, String content) {
        //设置推送通道下发策略
        Map<String, JsonObject> thirdMap = new HashMap<>();
        setThirdPartyChannel(thirdMap);
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(rids))
                .setOptions(Options.newBuilder()
                        //设置android通道下发策略
                        .setThirdPartyChannelV2(thirdMap)
                        .build())
                //设置通知消息
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        //设置android通知消息
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .build())
                        .build())
                .build();
    }

    /**
     * 逐条推送
     */
    public static PushPayload buildNotificationPushObject(String rid, String title, String content) {
        //设置推送通道下发策略
        Map<String, JsonObject> thirdMap = new HashMap<>();
        setThirdPartyChannel(thirdMap);
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(rid))
                .setOptions(Options.newBuilder()
                        //设置android通道下发策略
                        .setThirdPartyChannelV2(thirdMap)
                        .build())
                //设置通知消息
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        //设置android通知消息
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .build())
                        .build())
                .build();
    }


    /**
     * 设置第三方通道策略
     * distribution 为字符串类型,取值不能为空字符串,只能是"ospush"、"jpush"、"secondary_push"。
     * 值为 ospush 表示推送强制走厂商通道下发;
     * 值为 jpush 表示推送强制走极光通道下发;
     * 值为 secondary_push 表示推送优先走极光,极光不在线再走厂商,厂商作为辅助;【官方建议此种方式】
     * options.third_party_channel 的 key 只支持 xiaomi、huawei、meizu、oppo、vivo、fcm;
     */
    public static void setThirdPartyChannel(Map<String, JsonObject> thirdMap) {

        //华为厂商参数配置
        JsonObject hwParam = new JsonObject();
        hwParam.addProperty("distribution", "secondary_push");
//        hwParam.addProperty("importance","NORMAL");
        //华为 category
//        hwParam.addProperty("category","WORK");

        //小米厂商参数配置
        JsonObject xmParam = new JsonObject();
        xmParam.addProperty("distribution", "secondary_push");
        //小米channel_id
//        xmParam.addProperty("channel_id", "1001");

        //OPPO厂商参数配置
        JsonObject oppoParam = new JsonObject();
        oppoParam.addProperty("distribution", "secondary_push");
        //oppo channel_id
//        oppoParam.addProperty("channel_id", "1002");

        //VIVO厂商参数配置
        JsonObject vivoParam = new JsonObject();
        vivoParam.addProperty("distribution", "secondary_push");
        //1系统消息,0运营消息
//        vivoParam.addProperty("classification", 1);
        //vivo category
//        vivoParam.addProperty("category", "SUBSCRIPTION");

        //荣耀厂商参数配置
        JsonObject honorParam = new JsonObject();
        honorParam.addProperty("distribution", "secondary_push");
        //LOW不重要消息,NORMAL重要消息
        honorParam.addProperty("importance", "NORMAL");

        //添加到third_party_channel结构体
        thirdMap.put("huawei", hwParam);
        thirdMap.put("xiaomi", xmParam);
        thirdMap.put("oppo", oppoParam);
        thirdMap.put("vivo", vivoParam);
        thirdMap.put("honor", honorParam);
    }
}

五、厂商策略

    	Map<String,JsonObject> third_party_channel = new HashMap<>();
        //华为厂商参数配置
        JsonObject hwParam = new JsonObject();
        hwParam.addProperty("distribution","secondary_push");//推送策略
        hwParam.addProperty("importance","NORMAL");
        hwParam.addProperty("category","WORK");//华为 category
        //小米厂商参数配置
        JsonObject xmParam = new JsonObject();
        xmParam.addProperty("distribution","secondary_push");
        xmParam.addProperty("channel_id","1001");//小米channel_id
        //OPPO厂商参数配置
        JsonObject oppoParam = new JsonObject();
        oppoParam.addProperty("distribution","secondary_push");
        oppoParam.addProperty("channel_id","1002");//oppo channel_id
        //VIVO厂商参数配置
        JsonObject vivoParam = new JsonObject();
        vivoParam.addProperty("distribution","secondary_push");
        vivoParam.addProperty("classification",1);//1系统消息,0运营消息
        vivoParam.addProperty("category","SUBSCRIPTION");//vivo category
        //荣耀厂商参数配置
        JsonObject honorParam = new JsonObject();
        honorParam.addProperty("distribution","secondary_push");
        honorParam.addProperty("importance","NORMAL");//LOW不重要消息,NORMAL重要消息

        //添加到third_party_channel结构体
        third_party_channel.put("huawei",hwParam);
        third_party_channel.put("xiaomi",xmParam);
        third_party_channel.put("oppo",oppoParam);
        third_party_channel.put("vivo",vivoParam);
        third_party_channel.put("honor",honorParam);

        Options option = Options.newBuilder()
                        .addCustom("classification",1)//根据消息类型填入
                        .setApnsProduction(false)
                        .setTimeToLive(86400)
                        .setThirdPartyChannelV2(third_party_channel)
                        .build();
	
	//然后在payload构建时,setOptions中传入option
	//以上参数为举例,实际options需要的参数以业务需要为准
"third_party_channel":{
    "xiaomi":{
              "distribution":"secondary_push", 
              "channel_id":"1001" //填入小米申请的channel_id
    },
    "huawei":{
              "distribution":"secondary_push", 
              "importance":"NORMAL", //NORMAL代表重要消息,LOW代表不重要消息
              "category":"IM", //填入华为申请的category
              "target_user_type":0 //0代表普通消息(默认),1代表测试消息,测试消息1天限量500条
    },
    "honor":{
              "distribution":"secondary_push", 
              "importance":"NORMAL" //NORMAL代表重要消息,LOW代表不重要消息
    },
    "meizu":{
              "distribution":"secondary_push"
    },
    "oppo":{
              "distribution":"secondary_push", 
              "channel_id":"1002" //填入oppo申请的channel_id
    },
    "vivo":{
            "distribution":"secondary_push",
            "classification": 1, //0代表运营消息,1代表系统消息
            "category":"TODO", //填入vivo申请的category
            "push_mode":0  //0表示正式推送,1表示测试推送
    }
}

厂商通道需要逐一去申请厂商参数,此过程较为繁琐,申请到厂商参数并配置到极光应用控制台后,还需要争对厂商去申请厂商的配额,并在消息中携带相关参数,完成离线推送,这块需要参考极光官方的指导手册进行。

创建推送 - 极光文档

厂商限制要求

参数配置参考:厂商推送分类限制及适配方案 · BDS技术支持组

QPS限制:厂商限额及 QPS 限制 - 极光文档

标签:addProperty,极光,JsonObject,channel,push,推送,服务端
From: https://blog.csdn.net/m0_51607909/article/details/137016816

相关文章

  • python POST推送多种数据
    python POST推送多种数据 importrequests,os,json,hashlibfromrequests.exceptionsimportTimeout#小程序数据推送defpostDataToServer(self,param1,param2,param3,param4):url='http://www.baidu.com/t1/dfgdfgsdfgsdfgsdfbdfgsdgsdfgsdfgjfhjf......
  • Uni-app 之uni-push1.0服务端推送
    一、配置1、uni-push1.0文档https://uniapp.dcloud.net.cn/unipush-v1.html2、服务端推送文档https://docs.getui.com/getui/server/rest_v2/push/二、示例//0:站内信,1:消息$type=isset($type)?$type:0;$clickType='intent';$payload=array('type'=>�......
  • .net core 实现微信支付-小程序支付(服务端代码)
    前言前段时间研究了下微信支付-小程序支付的功能。但微信支付文档中关于.netC#的语言的sdk没有,只有javago和php版本的,当然社区也有很多已经集成好的微信支付.netcoresdk,但他们的的又都太臃肿了(我只需要个微信支付即可),集成度也特别深,用起来还得先学习下他们的文档,拿来主义固......
  • requests.post传的data如果是直接使用python dict封装,有些服务端接收不了这种数据类型
    平时在自己的php项目里,使用dict方式组装data,然后requests.post,一点问题都没有。但是调了后端一个java的微服务接口,结果就一直报错422: 最后问了一下开发,得到提示“python好像还有个毛病,python的json对象转字符串的时候,转出来的字符串不是标准json字符串,还要做个字符串处理,变成......
  • 【python】flask服务端响应与重定向处理
    ✨✨欢迎大家来到景天科技苑✨✨......
  • 微信小程序/公众号推送服务通知功能实现(公众号)
    #微信小程序/公众号推送服务通知   第一部分只介绍公众号,小程序的在第二部分再行介绍A、前期准备工作        1、准备好已经通审核及认证成功公众号    2、登录公众号平台    3、打开设置与开发中的基本配置        4、记录好开发者......
  • golang fasthttp服务端的简单实现
    使用示例:packagemainimport("github.com/buaazp/fasthttprouter""github.com/valyala/fasthttp""log")funcmain(){//创建路由r:=fasthttprouter.New()r.GET("/",Index)iferr:=fasthttp.Listen......
  • 网络常用服务端口
    网络常用端口有很多,以下是一些常见的端口HTTP:超文本传输协议,默认端口是80,用于网页浏览和Web服务。HTTPS:安全的HTTP协议,默认端口是443,用于安全的网页连接和加密通信。FTP:文件传输协议,用于传输文件,默认端口是21。SMTP:简单邮件传输协议,用于发送邮件,默认端口是25。POP3......
  • 实时数据传输的新里程——Server-Sent Events(SSE)消息推送技术
    目录一、背景介绍二、SSE场景案例三、工作原理    3.1SSE工作流程    3.2工作原理一、背景介绍    传统的请求模型是客户端发送一个请求到服务端,然后服务端做相应的处理,然后再将结果返回给客户端,这种“一问一答”的方式随着时间的推移并不......
  • 团购小程序源码系统:快递代收+社区便利店+推送商品等功能 带完整的安装部署教程
    在移动互联网高速发展的今天,小程序以其轻便、快捷、无需下载的特点,迅速成为商家与用户之间的桥梁。为了满足社区团购市场的需求,小编给大家分享一款功能强大的团购小程序源码系统,该系统集成了快递代收、社区便利店、推送商品等多项功能,为商家提供了一个高效、便捷的运营平台。......