1.相关概念
3. 下载扩展包
3.1 手动集成
将辅助通道扩展包下载解压后拷贝到你项目的Lib目录下,下载地址。 如果使用辅助通道扩展包v3.0.4及以上版本,需要将推送SDK升级到v3.0.11及以上版本。新版辅助通道扩展包以aar形式透出,省却manifest文件配置,减少出错概率。
将辅助通道扩展包放置到app module的libs路径下,并在app module的build.gradle文件中添加如下配置:
- [backcolor=transparent]repositories [backcolor=transparent]{
- [backcolor=transparent] flatDir [backcolor=transparent]{
- [backcolor=transparent] dirs [backcolor=transparent]'libs'[backcolor=transparent] [backcolor=transparent]//this way we can find the .aar file in libs folder
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
- [backcolor=transparent]...
- [backcolor=transparent]dependencies [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]......
- [backcolor=transparent] compile[backcolor=transparent]([backcolor=transparent]name[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'alicloud-android-third-push-3.0.5'[backcolor=transparent],[backcolor=transparent] ext[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'aar'[backcolor=transparent])
- [backcolor=transparent]}
3.2 Maven集成
- 项目顶层build.gradle中添加Maven仓库地址:
- [backcolor=transparent]allprojects [backcolor=transparent]{
- [backcolor=transparent] repositories [backcolor=transparent]{
- [backcolor=transparent] maven [backcolor=transparent]{
- [backcolor=transparent] url [backcolor=transparent]'http://maven.aliyun.com/nexus/content/repositories/releases/'
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
- gradle添加依赖:
- [backcolor=transparent]dependencies [backcolor=transparent]{
- [backcolor=transparent] compile [backcolor=transparent]'com.aliyun.ams:alicloud-android-third-push:3.0.5@aar'
- [backcolor=transparent]}
3.3 Firebase依赖
如需配置GCM/GCM通道还需要添加Firebase SDK依赖:
- [backcolor=transparent]dependencies [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]......
- [backcolor=transparent] compile [backcolor=transparent]([backcolor=transparent]'com.google.firebase:firebase-messaging:9.6.1'[backcolor=transparent])
- [backcolor=transparent]}
同时在AndroidManifest文件中添加如下配置:
- [backcolor=transparent]<receiver
- [backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.iid.FirebaseInstanceIdReceiver"
- [backcolor=transparent] [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"true"
- [backcolor=transparent] [backcolor=transparent]android:permission[backcolor=transparent]=[backcolor=transparent]"com.google.android.c2dm.permission.SEND"[backcolor=transparent] [backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<intent-filter>
- [backcolor=transparent] [backcolor=transparent]<action[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.android.c2dm.intent.RECEIVE"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]<action[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.android.c2dm.intent.REGISTRATION"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]<category[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.taobao.taobao"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]</intent-filter>
- [backcolor=transparent]</receiver>
- [backcolor=transparent]<receiver
- [backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
- [backcolor=transparent] [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"false"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent]<service
- [backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.alibaba.sdk.android.push.AgooFirebaseInstanceIDService"
- [backcolor=transparent] [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"true"[backcolor=transparent] [backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<intent-filter[backcolor=transparent] [backcolor=transparent]android:priority[backcolor=transparent]=[backcolor=transparent]"-500"[backcolor=transparent] [backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<action[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.INSTANCE_ID_EVENT"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]</intent-filter>
- [backcolor=transparent]</service>
- [backcolor=transparent]<service
- [backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.alibaba.sdk.android.push.AgooFirebaseMessagingService"
- [backcolor=transparent] [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"true"[backcolor=transparent] [backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<intent-filter[backcolor=transparent] [backcolor=transparent]android:priority[backcolor=transparent]=[backcolor=transparent]"-500"[backcolor=transparent] [backcolor=transparent]>
- [backcolor=transparent] [backcolor=transparent]<action[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.MESSAGING_EVENT"[backcolor=transparent] [backcolor=transparent]/>
- [backcolor=transparent] [backcolor=transparent]</intent-filter>
- [backcolor=transparent]</service>
4. Proguard配置
- 如果集成推送SDK的工程开启代码混淆,在Proguard配置的基础上,需要添加以下辅助通道的Proguard配置。
- [backcolor=transparent]# 小米通道
- [backcolor=transparent]-[backcolor=transparent]keep [backcolor=transparent]class[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]xiaomi[backcolor=transparent].**[backcolor=transparent] [backcolor=transparent]{*;}
- [backcolor=transparent]-[backcolor=transparent]dontwarn com[backcolor=transparent].[backcolor=transparent]xiaomi[backcolor=transparent].**
- [backcolor=transparent]# 华为通道
- [backcolor=transparent]-[backcolor=transparent]keep [backcolor=transparent]class[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]huawei[backcolor=transparent].**[backcolor=transparent] [backcolor=transparent]{*;}
- [backcolor=transparent]-[backcolor=transparent]dontwarn com[backcolor=transparent].[backcolor=transparent]huawei[backcolor=transparent].**
- [backcolor=transparent]# GCM/FCM通道
- [backcolor=transparent]-[backcolor=transparent]keep [backcolor=transparent]class[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]google[backcolor=transparent].[backcolor=transparent]firebase[backcolor=transparent].**{*;}
- [backcolor=transparent]-[backcolor=transparent] dontwarn com[backcolor=transparent].[backcolor=transparent]google[backcolor=transparent].[backcolor=transparent]firebase[backcolor=transparent].**
5. 在应用中初始化辅助通道
将以下代码加入你application.onCreate()方法中初始通道。 注意:辅助通道注册务必在Application中执行且放在推送SDK初始化代码之后,否则可能导致辅助通道注册失败
- [backcolor=transparent]// 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
- [backcolor=transparent]MiPushRegister[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent]([backcolor=transparent]applicationContext[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"小米AppID"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"小米AppKey"[backcolor=transparent]);
- [backcolor=transparent]// 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。
- [backcolor=transparent]HuaWeiRegister[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent]([backcolor=transparent]applicationContext[backcolor=transparent]);
- [backcolor=transparent]//GCM/FCM辅助通道注册
- [backcolor=transparent]GcmRegister[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent]([backcolor=transparent]this[backcolor=transparent],[backcolor=transparent] sendId[backcolor=transparent],[backcolor=transparent] applicationId[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]//sendId/applicationId为步骤获得的参数
本方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。