随笔 - 833  文章 - 0  评论 - 9  阅读 - 35万

App集成极光推送开发流程[关键步骤]

1.客户端集成SDK

1.1初始化

JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
JPushInterface.init(this); // 初始化 JPush

1.2设置别名及标签

复制代码
//设置别名(如果已设置,则无需重新设置),存储别名保存状态
SharedPreferences mySharedPreferences = getSharedPreferences("test",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
boolean hasSetTagAlias = mySharedPreferences.getBoolean("hasSetTagAlias",false);
if(!hasSetTagAlias){
   JPushUtil.setTag(LoginActivity.this,mDepartment.substring(0,12));
   JPushUtil.setAlias(LoginActivity.this,userId.replaceAll("-","_"));
   editor.putBoolean("hasSetTagAlias", true);
}
复制代码

2.服务端集成SDK

2.1导入依赖jar包(Maven项目配置pom.xml文件即可)

2.2构造通送内容,发起推送

复制代码
/**
     * 别名推送
     * @param alert        推送标题
     * @param ticketId    点击通知进入的工单id
     * @param alias        推送给指定的别名(以用户的id为别名)
     */
    public static void pushByAlias(String alert,String ticketId,String alias){
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
        // For push, all you need do is to build PushPayload object.
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
                                                      .setAudience(Audience.all())
                                                      .setNotification(Notification.newBuilder()
                                                      .addPlatformNotification(AndroidNotification.newBuilder()
                                                            .setAlert(alert)
                                                            .addExtra("id", ticketId)
                                                            .build())
                                                      .build())
                                                      .setAudience(Audience.newBuilder()
                                                      .addAudienceTarget(AudienceTarget.alias(alias))
                                                      .build())
                                                      .build();

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }
    }
复制代码
复制代码
/**
     * 标签推送
     * @param alert        推送标题
     * @param ticketId    点击通知进入的工单id
     * @param alias        推送给指定的别名(以用户的id为别名)
     */
    public static void pushByTag(String alert,String ticketId,String tag){
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
        // For push, all you need do is to build PushPayload object.
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
                                                      .setAudience(Audience.all())
                                                      .setNotification(Notification.newBuilder()
                                                      .addPlatformNotification(AndroidNotification.newBuilder()
                                                            .setAlert(alert)
                                                            .addExtra("id", ticketId)
                                                            .build())
                                                      .build())
                                                      .setAudience(Audience.newBuilder()
                                                      .addAudienceTarget(AudienceTarget.tag(tag))
                                                      .build())
                                                      .build();

        try {
            PushResult result = jpushClient.sendPush(payload);
            logger.info("Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            logger.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            logger.error("Should review the error, and fix the request", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
        }
    }
复制代码

 

posted on   Simle  阅读(636)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2016-10-25 Eclipse与IntelliJ IDEA区别
2015-10-25 oracle数据库自动备份脚本
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示