Android 快速接入
最近更新:2023-09-20

Android 快速接入

本文旨要引导用户快速集成推送服务,详细集成步骤参考 SDK 集成指南厂商通道 SDK 集成指南

SDK 下载详见 资源下载

添加工程配置

Project 根目录的主 gradle 配置

确认 Android Studio 的 Project 根目录的主 gradle 中配置了 mavenCentral 支持(新建 Project 默认配置就支持),配置华为和 FCM Maven 代码库,可根据华为和 FCM 发布的版本更新选择最新版本:

buildscript { repositories { google() mavenCentral() // hms, 若不集成华为厂商通道,可直接跳过 maven { url 'https://developer.huawei.com/repo/'} // fcm, 若不集成 FCM 通道,可直接跳过 maven { url "https://maven.google.com" } } dependencies { // fcm,若不集成 FCM 通道,可直接跳过 classpath 'com.google.gms:google-services:4.3.8' // hms,若不集成华为厂商通道,可直接跳过 classpath 'com.huawei.agconnect:agcp:1.6.0.300' } } allprojects { repositories { google() mavenCentral() //hms,若不集成华为厂商通道,可直接跳过 maven {url 'https://developer.huawei.com/repo/'} //fcm,若不集成 FCM 通道,可直接跳过 maven { url "https://maven.google.com" } } }
          buildscript {
                repositories {
                    google()
                    mavenCentral()
                    // hms, 若不集成华为厂商通道,可直接跳过
                    maven { url 'https://developer.huawei.com/repo/'}
                    // fcm, 若不集成 FCM 通道,可直接跳过
                    maven { url "https://maven.google.com" }
                  }

                 dependencies {
                    // fcm,若不集成 FCM 通道,可直接跳过
                    classpath 'com.google.gms:google-services:4.3.8'
                    // hms,若不集成华为厂商通道,可直接跳过
                    classpath 'com.huawei.agconnect:agcp:1.6.0.300'
                 }
             }

            allprojects {
                  repositories {
                    google()
                    mavenCentral()
                    //hms,若不集成华为厂商通道,可直接跳过
                    maven {url 'https://developer.huawei.com/repo/'}
                    //fcm,若不集成 FCM 通道,可直接跳过
                    maven { url "https://maven.google.com" }
                  }
              }

        
此代码块在浮窗中显示

Module 的 gradle 配置

在 Module 的 gradle 中添加依赖和 AndroidManifest 的替换变量,集成极光推送 SDK 和厂商通道 SDK,其中厂商组合选择所需的通道即可。

android { ...... defaultConfig { applicationId "com.xxx.xxx" //JPush 上注册的包名. ...... ndk { //选择要添加的对应 cpu 类型的 .so 库。 abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a' // 还可以添加 'x86', 'x86_64', 'mips', 'mips64' } manifestPlaceholders = [ JPUSH_PKGNAME : applicationId, //JPush 上注册的包名对应的 Appkey. JPUSH_APPKEY : "你的 Appkey ", //暂时填写默认值即可. JPUSH_CHANNEL : "developer-default", //若不集成厂商通道,可直接跳过以下配置 MEIZU_APPKEY : "MZ-魅族的APPKEY", MEIZU_APPID : "MZ-魅族的APPID", XIAOMI_APPID : "MI-小米的APPID", XIAOMI_APPKEY : "MI-小米的APPKEY", OPPO_APPKEY : "OP-oppo的APPKEY", OPPO_APPID : "OP-oppo的APPID", OPPO_APPSECRET : "OP-oppo的APPSECRET", VIVO_APPKEY : "vivo的APPKEY", VIVO_APPID : "vivo的APPID" HONOR_APPID : "Honor的APP ID", ] ...... } repositories { flatDir { dirs 'libs' } } ...... } dependencies { ...... // 此处以JPush 5.0.3 版本为例,注意:从 5.0.0 版本开始可以自动拉取 JCore 包,无需另外配置 implementation 'cn.jiguang.sdk:jpush:5.0.3' //若不集成厂商通道,可直接跳过以下依赖 // 极光厂商插件版本与接入 JPush 版本保持一致,下同 // 接入华为厂商 implementation 'com.huawei.hms:push:6.9.0.300' implementation 'cn.jiguang.sdk.plugin:huawei:5.0.3' // 接入 FCM 厂商 implementation 'com.google.firebase:firebase-messaging:23.1.2' implementation 'cn.jiguang.sdk.plugin:fcm:5.0.3' // 接入魅族厂商 implementation 'cn.jiguang.sdk.plugin:meizu:5.0.3' // 接入 VIVO 厂商 implementation 'cn.jiguang.sdk.plugin:vivo:5.0.3' // 接入小米厂商 implementation 'cn.jiguang.sdk.plugin:xiaomi:5.0.3' // 接入 OPPO 厂商 implementation 'cn.jiguang.sdk.plugin:oppo:5.0.3' // JPush Android SDK v4.6.0 开始,需要单独引入 oppo 厂商 aar ,请下载官网 SDK 包并把 jpush-android-xxx-release/third-push/oppo/libs 下的 aar 文件单独拷贝一份到应用 module/libs 下 implementation(name: 'com.heytap.msp-push-3.1.0', ext: 'aar') //以下为 OPPO 3.1.0 aar需要依赖 implementation 'com.google.code.gson:gson:2.6.2' implementation 'commons-codec:commons-codec:1.6' implementation 'androidx.annotation:annotation:1.1.0' // 接入荣耀厂商 implementation 'cn.jiguang.sdk.plugin:honor:5.0.3' //需要单独引入荣耀厂商 aar ,请下载官网 SDK 包并把 jpush-android-xxx-release/third-push/honor/libs 下的 aar 文件单独拷贝一份到应用 module/libs 下 implementation(name: 'HiPushSdk-v7.0.41.301', ext: 'aar') ...... } apply plugin: 'com.google.gms.google-services' apply plugin: 'com.huawei.agconnect'
          android {
                ......
                defaultConfig {
                    applicationId "com.xxx.xxx" //JPush 上注册的包名.
                    ......

                    ndk {
                        //选择要添加的对应 cpu 类型的 .so 库。
                        abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
                        // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
                    }

                    manifestPlaceholders = [
                        JPUSH_PKGNAME : applicationId,
                        //JPush 上注册的包名对应的 Appkey.
                        JPUSH_APPKEY : "你的 Appkey ", 
                        //暂时填写默认值即可.
                        JPUSH_CHANNEL : "developer-default",

                        //若不集成厂商通道,可直接跳过以下配置
                        MEIZU_APPKEY : "MZ-魅族的APPKEY",
                        MEIZU_APPID : "MZ-魅族的APPID",
                        XIAOMI_APPID : "MI-小米的APPID",
                        XIAOMI_APPKEY : "MI-小米的APPKEY",
                        OPPO_APPKEY : "OP-oppo的APPKEY",
                        OPPO_APPID : "OP-oppo的APPID",
                        OPPO_APPSECRET : "OP-oppo的APPSECRET",
                        VIVO_APPKEY : "vivo的APPKEY",
                        VIVO_APPID : "vivo的APPID"
                        HONOR_APPID : "Honor的APP ID", 
                    ]
                    ......
                }
                repositories {
                    flatDir {
                        dirs 'libs'
                    }
                }       
                ......
            }


            dependencies {
                ......
                // 此处以JPush 5.0.3 版本为例,注意:从 5.0.0 版本开始可以自动拉取 JCore 包,无需另外配置
                implementation 'cn.jiguang.sdk:jpush:5.0.3'  
                
                //若不集成厂商通道,可直接跳过以下依赖
                // 极光厂商插件版本与接入 JPush 版本保持一致,下同
                // 接入华为厂商
                implementation 'com.huawei.hms:push:6.9.0.300'
                implementation 'cn.jiguang.sdk.plugin:huawei:5.0.3'
                // 接入 FCM 厂商
                implementation 'com.google.firebase:firebase-messaging:23.1.2'
                implementation 'cn.jiguang.sdk.plugin:fcm:5.0.3'
                // 接入魅族厂商
                implementation 'cn.jiguang.sdk.plugin:meizu:5.0.3'
                // 接入 VIVO 厂商
                implementation 'cn.jiguang.sdk.plugin:vivo:5.0.3'
                // 接入小米厂商
                implementation 'cn.jiguang.sdk.plugin:xiaomi:5.0.3'
                // 接入 OPPO 厂商
                implementation 'cn.jiguang.sdk.plugin:oppo:5.0.3'
                // JPush Android SDK v4.6.0 开始,需要单独引入 oppo 厂商 aar ,请下载官网 SDK 包并把 jpush-android-xxx-release/third-push/oppo/libs 下的 aar 文件单独拷贝一份到应用 module/libs 下
                implementation(name: 'com.heytap.msp-push-3.1.0', ext: 'aar')

                //以下为 OPPO 3.1.0 aar需要依赖
                implementation 'com.google.code.gson:gson:2.6.2'
                implementation 'commons-codec:commons-codec:1.6'
                implementation 'androidx.annotation:annotation:1.1.0'
                // 接入荣耀厂商
                implementation 'cn.jiguang.sdk.plugin:honor:5.0.3' 
                //需要单独引入荣耀厂商 aar ,请下载官网 SDK 包并把 jpush-android-xxx-release/third-push/honor/libs 下的 aar 文件单独拷贝一份到应用 module/libs 下
                implementation(name: 'HiPushSdk-v7.0.41.301', ext: 'aar')
                ......
            }

            apply plugin: 'com.google.gms.google-services'
            apply plugin: 'com.huawei.agconnect'

        
此代码块在浮窗中显示

应用 Module 配置

如果选择的厂商通道包含了 Huawei 厂商通道和 FCM 厂商通道,则需要额外执行以下操作,若未选择可忽略本步骤。
FCM:在 Firebase 上创建和 JPush 上同包名的待发布应用,创建完成后下载该应用的 google-services.json 配置文件并添加到应用的 module 目录下。
Huawei:在 Huawei 上创建和 JPush 上同包名的待发布应用,创建完成后下载该应用的 agconnect-services.json 配置文件并添加到应用的 module 目录下。

配置推送必须组件

在 AndroidManifest 中配置一个 Service,以在更多手机平台上获得更稳定的支持,示例如下:

<!-- Since JCore2.0.0 Required SDK核心功能--> <!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false --> <!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 --> <service android:name="xx.xx.XService" android:enabled="true" android:exported="false" android:process=":pushcore"> <intent-filter> <action android:name="cn.jiguang.user.service.action" /> </intent-filter> </service>
          <!-- Since JCore2.0.0 Required SDK核心功能-->
<!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false -->
<!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 -->
<service android:name="xx.xx.XService"
        android:enabled="true"
        android:exported="false"
        android:process=":pushcore">
        <intent-filter>
            <action android:name="cn.jiguang.user.service.action" />
        </intent-filter>
</service>

        
此代码块在浮窗中显示

从 JPush 5.2.0 开始,需要配置继承 JPushMessageService 的广播,原来如果配了 MyReceiver和JPushMessageReceiver 现在可以弃用,示例如下:

<!-- Required since 5.2.0 --> <!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定义的Service --> <!-- 5.2.0开始所有事件将通过该类回调 --> <!-- 该广播需要继承 JPush 提供的 JPushMessageService 类, 并如下新增一个 Intent-Filter --> <service android:name="自定义 Service" android:enabled="true" android:exported="false" > <intent-filter> <action android:name="cn.jpush.android.intent.SERVICE_MESSAGE" /> <category android:name="您应用的包名" /> </intent-filter> </service>
          <!-- Required since 5.2.0 -->
<!-- 新的 tag/alias 接口结果返回需要开发者配置一个自定义的Service -->
<!-- 5.2.0开始所有事件将通过该类回调 -->
<!-- 该广播需要继承 JPush 提供的 JPushMessageService 类, 并如下新增一个 Intent-Filter -->
<service
    android:name="自定义 Service"
    android:enabled="true"
    android:exported="false" >
    <intent-filter>
            <action android:name="cn.jpush.android.intent.SERVICE_MESSAGE" />
            <category android:name="您应用的包名" />
    </intent-filter>
</service>

        
此代码块在浮窗中显示

初始化推送服务

JPush SDK 提供的 API 接口,都主要集中在cn.jpush.android.api.JPushInterface类里。

public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); // 调整点一:初始化代码前增加setAuth调用 boolean isPrivacyReady; // app根据是否已弹窗获取隐私授权来赋值 if(!isPrivacyReady){ JCollectionAuth.setAuth(context, false); // 后续初始化过程将被拦截 } JPushInterface.init() // 调整点二:隐私政策授权获取成功后调用 JCollectionAuth.setAuth(context, true); //如初始化被拦截过,将重试初始化过程 } }
          public class ExampleApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        
        // 调整点一:初始化代码前增加setAuth调用
        boolean isPrivacyReady; // app根据是否已弹窗获取隐私授权来赋值
        if(!isPrivacyReady){
            JCollectionAuth.setAuth(context, false); // 后续初始化过程将被拦截
        }
        JPushInterface.init()
        
        
        // 调整点二:隐私政策授权获取成功后调用
        JCollectionAuth.setAuth(context, true); //如初始化被拦截过,将重试初始化过程
    }
}


        
此代码块在浮窗中显示

验证推送结果

  • 集成完成后,如果输出以下日志则代表您已经集成成功。
D/JIGUANG-JPush: [ActionHelper] doAction:init ...... I/JIGUANG-JCore: [ConnectingHelper] Register succeed - juid:58446897155, registrationId:1104a89792620fb0b02, deviceId:null
          D/JIGUANG-JPush: [ActionHelper] doAction:init
......
I/JIGUANG-JCore: [ConnectingHelper] Register succeed - juid:58446897155, registrationId:1104a89792620fb0b02, deviceId:null

        
此代码块在浮窗中显示
  • 获取日志中的 registrationId,并在极光控制台 创建推送 体验推送服务。
  • 推送完成后,你可以在 推送历史 中查看推送状态、推送通道、送达率等详细数据。
文档内容是否对您有帮助?

Copyright 2011-2022, jiguang.cn, All Rights Reserved. 粤ICP备12056275号-13 深圳市和讯华谷信息技术有限公司

在文档中心打开