随玩推送Android集成文档v1.0

417 阅读3分钟

集成 Push SDK

1、在主工程下创建libs文件夹

2、导入以下aar

1.png

3、添加依赖

dependencies {
    ···
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation fileTree(include: ['*.aar'], dir: 'libs')
    implementation 'com.huawei.hms:push:6.5.0.300'
    implementation 'com.meizu.flyme.internet:push-internal:4.1.4'
    implementation 'com.squareup.okhttp3:okhttp:3.12.3'
    implementation 'com.google.code.gson:gson:2.8.9'
}

4、华为相关配置

添加应用的AppGallery Connect配置文件

登录AppGallery Connect网站,点击“我的项目”。

在项目列表中找到您的项目,在项目中点击需要集成HMS Core SDK的应用。

在“项目设置 > 常规”页面的“应用”区域,点击“agconnect-services.json”下载配置文件。

2.png

将“agconnect-services.json”文件拷贝到应用级根目录下。

3.png

配置HMS Core SDK的Maven仓地址

Android Studio的代码库配置在Gradle插件7.0以下版本、7.0版本和7.1及以上版本有所不同。请根据您当前的Gradle插件版本,选择对应的配置过程。

7.0以下版本

打开Android Studio项目级“build.gradle”文件。

4.png

添加HUAWEI AGC插件以及Maven代码库。

在“buildscript > repositories”中配置HMS Core SDK的Maven仓地址。

在“allprojects > repositories”中配置HMS Core SDK的Maven仓地址。

如果App中添加了“agconnect-services.json”文件则需要在“buildscript > dependencies”中增加AGC插件配置。

buildscript {
    repositories {
        google()
        jcenter()
        // 配置HMS Core SDK的Maven仓地址。
        maven {url'https://developer.huawei.com/repo/'}
    }
    dependencies {
        ...
        // 增加AGC插件配置,请您参见[AGC插件依赖关系](https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-sdk-changenotes-0000001058732550#section7117746172220)选择合适的AGC插件版本。
        classpath'com.huawei.agconnect:agcp:*1.6.0.300*'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        // 配置HMS Core SDK的Maven仓地址。
        maven {url'https://developer.huawei.com/repo/'}
    }
}
7.0版本

打开Android Studio项目级“build.gradle”文件。

5.png

添加HUAWEI AGC插件以及Maven代码库。

在“buildscript > repositories”中配置HMS Core SDK的Maven仓地址。

如果App中添加了“agconnect-services.json”文件则需要在“buildscript > dependencies”中增加AGC插件和Android Gradle插件配置。

buildscript {
    repositories {
        google()
        jcenter()
        // 配置HMS Core SDK的Maven仓地址。
        maven {url'https://developer.huawei.com/repo/'}
    }
    dependencies {
        ...
        // 增加AGC插件配置,请您参见[AGC插件依赖关系](https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-sdk-changenotes-0000001058732550#section7117746172220)选择合适的AGC插件版本。
        classpath'com.huawei.agconnect:agcp:*1.6.0.300*'
    }
}

打开项目级“settings.gradle”文件,配置HMS Core SDK的Maven仓地址。

dependencyResolutionManagement {
    ...
    repositories {
        google()
        jcenter()
        // 配置HMS Core SDK的Maven仓地址。
        maven {url'https://developer.huawei.com/repo/'}
    }
}
7.1及以上版本

打开Android Studio项目级“build.gradle”文件。

6.png

如果App中添加了“agconnect-services.json”文件则需要在“buildscript > dependencies”中增加AGC插件和Android Gradle插件配置。

buildscript {
  dependencies {
        ...
        // 增加AGC插件配置,请您参见[AGC插件依赖关系](https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-sdk-changenotes-0000001058732550#section7117746172220)选择合适的AGC插件版本。
        classpath'com.huawei.agconnect:agcp:*1.6.0.300*'
    }
}

打开项目级“settings.gradle”文件,配置HMS Core SDK的Maven仓地址。

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        // 配置HMS Core SDK的Maven仓地址。
        maven { url'https://developer.huawei.com/repo/' }
    }
}
dependencyResolutionManagement {
    ...
    repositories {
        google()
        mavenCentral()
        // 配置HMS Core SDK的Maven仓地址。
        maven { url'https://developer.huawei.com/repo/' }
    }
}
添加AGC插件配置。请根据实际情况选择:

方式一:在文件头部声明下一行添加如下配置。

applyplugin:'com.huawei.agconnect'

方式二:在plugins中添加如下配置。

plugins {
    id'com.android.application'
    // 添加如下配置
    id'com.huawei.agconnect'
}

5、配置Manifest文件

使用以下内容时切记替换“你的xxxx”等相关信息

权限授予
<uses-permission android:name="android.permission.INTERNET" />

<!-- 小米推送 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
    android:name="你的App包名.permission.MIPUSH_RECEIVE"
    android:protectionLevel="signature" /> 
<uses-permission android:name="你的App包名.permission.MIPUSH_RECEIVE" />
<!-- 小米推送 -->

<!-- 魅族推送 -->
<!--可选,用于兼容 Flyme5 且推送服务是旧版本的情况-->
<!-- <uses-permission android:name="android.permission.READ_PHONE_STATE" />-->
<!-- 兼容 Flyme5 的权限配置-->
<uses-permission android:name="com.meizu.flyme.push.permission.RECEIVE" />
<permission
    android:name="你的App包名.push.permission.MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="你的App包名.push.permission.MESSAGE" />
<!-- 兼容 Flyme3 的权限配置-->
<uses-permission android:name="com.meizu.c2dm.permission.RECEIVE" />
<permission    
    android:name="你的App包名.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="你的App包名.permission.C2D_MESSAGE" />
<!-- 魅族推送 -->
元数据、服务和广播配置
        <!-- 华为推送 -->
        <service
            android:name="com.suiwan.push.huawei.HuaWeiPushService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <!-- 华为推送 -->

        <!-- 小米推送 -->
        <service
            android:name="com.xiaomi.push.service.XMPushService"
            android:enabled="true"
            android:process=":pushservice" />
        <!--注:此service必须在3.0.1版本以后(包括3.0.1版本)加入-->
        <service
            android:name="com.xiaomi.push.service.XMJobService"
            android:enabled="true"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:process=":pushservice" />
        <service
            android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
            android:enabled="true"
            android:exported="true" />
        <!--注:此service必须在2.2.5版本以后(包括2.2.5版本)加入-->
        <service
            android:name="com.xiaomi.mipush.sdk.MessageHandleService"
            android:enabled="true" />

        <receiver
            android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.xiaomi.push.service.receivers.PingReceiver"
            android:exported="false"
            android:process=":pushservice">
            <intent-filter>
                <action android:name="com.xiaomi.push.PING_TIMER" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.suiwan.push.xiaomi.XiaoMiPushReceiver"
            android:exported="true">
            <!--这里com.xiaomi.mipushdemo.DemoMessageRreceiver改成app中定义的完整类名-->
            <intent-filter>
                <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.xiaomi.mipush.ERROR" />
            </intent-filter>
        </receiver>
        <!-- 小米推送 -->

        <!-- Vivo推送 -->
        <!--Vivo Push开放平台中应用的appid 和api key-->
        <meta-data
            android:name="com.vivo.push.api_key"
            android:value="你的apiKey" />
        <meta-data
            android:name="com.vivo.push.app_id"
            android:value="你的appId" />
        <!--push应用定义消息receiver声明-->
        <receiver
            android:name="com.suiwan.push.vivo.VivoPushReceiver"
            android:exported="false">
            <intent-filter>
                <!--接收push消息-->
                <action android:name="com.vivo.pushclient.action.RECEIVE" />
            </intent-filter>
        </receiver>
        <!--Vivo Push需要配置的service、activity-->
        <service
            android:name="com.vivo.push.sdk.service.CommandClientService"
            android:exported="true"
            android:permission="com.push.permission.UPSTAGESERVICE" />
        <!-- Vivo推送 -->

        <!-- 魅族推送 -->
        <!-- Push 应用定义消息 receiver 声明 -->
        <receiver android:name="com.suiwan.push.meizu.MeiZuPushReceiver">
            <intent-filter>
                <!-- 接收 push 消息 -->
                <action android:name="com.meizu.flyme.push.intent.MESSAGE" />
                <!-- 接收 register 消息 -->
                <action android:name="com.meizu.flyme.push.intent.REGISTER.FEEDBACK" />
                <!-- 接收 unregister 消息-->
                <action android:name="com.meizu.flyme.push.intent.UNREGISTER.FEEDBACK" />
                <!-- 兼容低版本 Flyme3 推送服务配置 -->
                <action android:name="com.meizu.c2dm.intent.REGISTRATION" />
                <action android:name="com.meizu.c2dm.intent.RECEIVE" />

                <category android:name="你的App包名" />
            </intent-filter>
        </receiver>
        <!-- 魅族推送 -->

6、添加相关忽略

# 华为推送
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.huawei.hianalytics.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}

# 小米推送
-keep class com.suiwan.push.xiaomi.XiaoMiPushReceiver {*;}

# OPPO推送
-keep public class * extends android.app.Service
-keep class com.heytap.msp.** { *;}

# Vivo推送
-dontwarn com.vivo.push.**
-keep class com.vivo.push.**{*; }
-keep class com.vivo.vms.**{*; }
-keep class com.suiwan.push.vivo.VivoPushReceiver {*;}

7、创建推送通道

//建议在application中调用
private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = "服务通知"
        val importance = NotificationManager.IMPORTANCE_HIGH
        val mChannel = when (Build.BRAND.lowercase()) {
            "huawei", "honor" -> {
                NotificationChannel("你的推送通道", name, importance)
            }
            "xiaomi" -> {
                NotificationChannel("你的推送通道", name, importance)
            }
            "oppo", "oneplus", "realme" -> {
                NotificationChannel("你的推送通道", name, importance)
            }
            "vivo", "iqoo" -> {
                null
            }
            "meizu" -> {
                null
            }
            else -> null
        }
        mChannel?.let { c ->
            val description = "服务通知"
            c.description = description
            c.enableLights(true)
            c.enableVibration(true)
            c.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            val notificationManager =
                getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(c)
        }
    }
}

8、注册推送服务

private fun registerPush() {
    PushManager.register(
        this,
        "你的AppId",
        "你的UUID",
        PushContext(
            HUAWEI_APP_ID = "你的HUAWEI_APP_ID",

            XIAOMI_APP_ID = "你的XIAOMI_APP_ID",
            XIAOMI_APP_KEY = "你的XIAOMI_APP_KEY",

            OPPO_APP_KEY = "你的OPPO_APP_KEY",
            OPPO_APP_SECRET = "你的OPPO_APP_SECRET",

            MEIZU_APP_ID = "你的MEIZU_APP_ID",
            MEIZU_APP_KEY = "你的MEIZU_APP_KEY"
        )
    )
}

9、注销推送服务

private fun unregisterPush() {
    PushManager.unregister(this)
}