集成极光组装包推送集成指南

一、添加工程配置
1、Project 根目录的主 gradle
确认 android studio 的 Project 根目录的主 gradle 中配置了 mavenCentral 支持(新建 project 默认配置就支持),配置华为和 FCM Maven 代码库,,可根据华为和 FCM 发布的版本更新选择最新版本:

buildscript {
        repositories {
            google()
            jcenter()
            mavenCentral()
            // hms
            maven { url 'http://developer.huawei.com/repo/'}
            // fcm
            maven { url "https://maven.google.com" }
          }

         dependencies {
            // fcm
            classpath 'com.google.gms:google-services:4.3.4'
            // hms
            classpath 'com.huawei.agconnect:agcp:1.4.2.300'
         }
     }

    allprojects {
          repositories {
            google()
            jcenter()
            mavenCentral()
            //hms
            maven {url 'http://developer.huawei.com/repo/'}
            //fcm
            maven { url "https://maven.google.com" }
          }
      }

2、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 : "你的 Appkey ", //JPush 上注册的包名对应的 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"
                ]
                ......
            }
            ......
        }


        dependencies {
            ......
            // 接入极光module
            implementation project(':jiguang')
            // 接入华为厂商
            implementation 'com.huawei.hms:push:5.3.0.301'
            // 接入oppo 厂商 aar 需要单独引入
// **请将 jiguang/libs 下 com.heytap.msp-push-x.x.x.aar 单独拷贝一份到应用 module/libs 下**
            implementation(name: 'com.heytap.msp-push-2.1.0', ext: 'aar')
            ......
        }
    
        apply plugin: 'com.google.gms.google-services'
        apply plugin: 'com.huawei.agconnect'

3、应用 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>    

从JPush3.0.7开始,需要配置继承JPushMessageReceiver的广播,原来如果配了MyReceiver现在可以弃用。示例如下。

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

四 、初始化推送服务

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

public class ExampleApplication extends Application {
    @Override
        public void onCreate() {
            super.onCreate();
            //设置调试模式
            JPushInterface.setDebugMode(true);
            //初始化
            JPushInterface.init(this);
       }
   }

三、集成各个三方推送详情请跳至下个链接

⚠️注意方法数超过65535,导入multidex包。

defaultConfig {
    multiDexEnabled true
}
dependencies{
    implementation 'com.android.support:multidex:1.0.3'
}

public class App extends MultiDexApplication {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容