前提条件

这篇文章主要讲集成阿里的EMAS推送进程中的一些坑和接入流程,主要是对接入进程的一个总结(究竟浪费了我3天时刻)。

  • 开通了EMAS服务并创建了运用(创建办法)
  • 创建了各厂商的运用(国内的厂商有必要有,要不然离线状况收不到告诉,三星在外(貌似也不算国内厂商-O-))

接入SDK

接入办法官方文档上已写好步骤,这里就不重复赘述了,这里只做个总结。

  1. 工程的setting.gradle中增加阿里云库房,代码如下:

pluginManagement {
    repositories {
        ...
        maven { url "https://maven.aliyun.com/nexus/content/repositories/releases/" }
    }
}
dependencyResolutionManagement {
//    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        ...
        maven { url "https://maven.aliyun.com/nexus/content/repositories/releases/" }
        maven {url 'https://developer.huawei.com/repo/'}//接入华为推送时需求
    }
}
  1. 工程的build.gradle中增加gradle依靠
buildscript {
    dependencies {
        ...
        classpath 'com.aliyun.ams:emas-services:1.0.4'//能够在阿里云库房查找最新的版别:https://developer.aliyun.com/mvn/search
    }
}
  1. 在项目app的build.gradle下面增加插件和依靠,并在同级目录下增加装备文件aliyun-emas-services.json

    所有依靠版别均可在阿里云库房找到最新版别:developer.aliyun.com/mvn/search

plugins {
    ...
    id 'com.aliyun.ams.emas-services'
}
android {
    defaultConfig {
        ...
        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86_64'//你支撑的cpu架构
        }
}
dependencies {
    implementation 'com.alipay.sdk:alipaysdk-android:15.8.12'//阿里云根底依靠
    implementation 'com.aliyun.ams:alicloud-android-push:3.8.4.1'//推送依靠
    implementation 'com.aliyun.ams:alicloud-android-third-push:3.8.4'//辅佐推送依靠运用厂商推送时需求,进步到达率
    implementation 'com.aliyun.ams:alicloud-android-third-push-xiaomi:3.8.4'
    implementation 'com.aliyun.ams:alicloud-android-third-push-huawei:3.8.4'
    implementation 'com.aliyun.ams:alicloud-android-third-push-vivo:3.8.4'
    implementation 'com.aliyun.ams:alicloud-android-third-push-oppo:3.8.4'
}
  1. aliyun-emas-services.json中装备信息修改

    aliyun-emas-services.json中包含多种服务,对应有status来操控是否启用,1为启用0为不启用,推送服务对应为cps_service,假如只用推送能够只开启这个服务详细可看文档

{
  "config": {
   "emas.appKey":"*******",
   "emas.appSecret":"*******",
   "emas.packageName":"*******",
   "hotfix.idSecret":"*******",
   "hotfix.rsaSecret":"*******",
   "httpdns.accountId":"*******",
   "httpdns.secretKey":"*******",
   "appmonitor.tlog.rsaSecret":"*******",
   "appmonitor.rsaSecret":"*******"
},
  "services": {
   "hotfix_service":{//移动热修复
      "status":0,
      "version":"3.3.8"
   },
   "ha-adapter_service":{//功能剖析/远程日志根底库
      "status":0,
      "version":"1.1.5.4-open"
   },
   "feedback_service":{//移动用户反应
      "status":0,
      "version":"3.4.2"
   },
   "tlog_service":{//远程日志
      "status":0,
      "version":"1.1.4.5-open"
   },
   "httpdns_service":{//HTTPDNS
      "status":0,
      "version":"2.3.0-intl"
   },
   "apm_service":{//功能剖析
      "status":0,
      "version":"1.1.1.0-open"
   },
   "man_service":{
      "status":1,
      "version":"1.2.7"
   },
   "cps_service":{//移动推送
      "status":1,
      "version":"3.8.4.1"
   }
},
  "use_maven":true,//true表明运用远程依靠,false表明运用本地libs依靠
  "proguard_keeplist":"********"
}
  1. 项目app的AndroidManifest.xml文件中装备appkey
...
<uses-permission android:name="android.permission.INTERNET" /> <!-- 网络拜访 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 检查Wi-Fi网络状况 -->
<uses-permission android:name="android.permission.GET_TASKS" /> <!-- 推送运用,运转状况 -->
<uses-permission android:name="android.permission.REORDER_TASKS" /> <!-- 推送运用,运转状况 -->
<application
    android:allowBackup="false" 
    tools:replace="android:allowBackup">
    <meta-data
        android:name="com.alibaba.app.appkey"
        android:value="阿里云的APPKEY" />
    <meta-data
        android:name="com.alibaba.app.appsecret"
        android:value="阿里云的appsecret" />
    <meta-data
        android:name="com.huawei.hms.client.appid"
        android:value="appid=华为的appid" />
    <meta-data
        android:name="com.vivo.push.api_key"
        android:value="vivo的apikey" />
    <meta-data
        android:name="com.vivo.push.app_id"
        android:value="vivo的appid" />
    <receiver
        android:name=".push.PushReceiver"//接纳告诉的广播继成自MessageReceiver当app为在线状况或走阿里云通道时调用
        android:exported="false">
        <intent-filter>
            <action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.alibaba.sdk.android.push.RECEIVE" />
        </intent-filter>
    </receiver>
    <activity
        android:name=".push.PushPopupActivity"//辅佐推送Activity继成自AndroidPopupActivity当走厂商途径告诉被点击时调用到onSysNoticeOpened()办法中
        android:exported="true"
        android:label="@string/title_activity_third_push_notice"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait" >
        <!-- scheme 装备开始 vivo通道需求增加 -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="${applicationId}"
                android:path="/thirdpush"
                android:scheme="agoo" />
        </intent-filter>
        <!-- scheme 装备结束  -->
    </activity>
  1. 初始化SDK

    在Application的onCreate()办法中初始化

open class BaseApp : Application() {
    override fun onCreate() {
        super.onCreate()
        PushServiceFactory.init(this)
        initNotificationChannel(this)
        MiPushRegister.register(this, "小米的appid", "小米的appkey")
        HuaWeiRegister.register(this)
        VivoRegister.register(this)
        OppoRegister.register(this,"oppo的appkey","oppo的appSecret")
        val pushService = PushServiceFactory.getCloudPushService()
        if (ProjectSwitch.isDebug) {
            pushService.setLogLevel(CloudPushService.LOG_DEBUG) //仅适用于Debug包,正式包不需求此行
        }
        pushService.setNotificationShowInGroup(true)
        pushService.register(this, object : CommonCallback {
            override fun onSuccess(response: String?) {
            }
            override fun onFailed(errorCode: String, errorMessage: String) {
            }
        })
    }
    private fun initNotificationChannel(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val mNotificationManager: NotificationManager? =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
            // 告诉途径的id,后台推送选择途径时对应的id。
            val id = "1"
            // 用户能够看到的告诉途径的名字。
            val name: CharSequence = "告诉途径的名字"
            // 用户能够看到的告诉途径的描绘。
            val description = "途径的描绘"
            val importance: Int = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(id, name, importance)
            // 装备告诉途径的特点。
            mChannel.setDescription(description)
            // 设置告诉出现时的闪灯(假如Android设备支撑的话)。
            mChannel.enableLights(true)
            mChannel.setLightColor(Color.RED)
            // 设置告诉出现时的轰动(假如Android设备支撑的话)。
            mChannel.enableVibration(true)
            mChannel.setVibrationPattern(longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400))
            // 最后在notificationmanager中创建该告诉途径。
            mNotificationManager?.createNotificationChannel(mChannel)
        }
    }
}

各厂商集成步骤

检查官方辅佐通道集成文档help.aliyun.com/document_de…

问题处理

完结上面一堆后本认为能够大功告成了吗,no~no~no~,怎么可能这么简单,接下来就是踩坑之旅了。

  1. Android13对告诉做了权限操控,需求显现恳求告诉权限:developer.android.google.cn/about/versi…
  2. 各厂商对告诉做了限制处理,导致有必要请求对应途径才干正常运用告诉:
  • 小米推送创建告诉类别详细见文档:文档中心 (mi.com)
  • 华为默许一天能够推送两条,如需求更多则需求请求自分类权益:音讯分类标准(huawei.com)
  • vivo默许一天两条,如需更多则需求请求音讯分类:vivo敞开渠道
  • oppo默许一天两条,如需更多需求请求音讯分类:OPPO 敞开渠道