开发者社区> 问答> 正文

如何实现移动推送辅助通道配置?



1.相关概念

  • 辅助通道:移动推送针对小米、华为设备管控较严的情况特意接入华为,小米推送辅助通道以提高在华为、小米设备上的到达率。移动推送优先选择自有通道进行推送消息下发,只有在自有通道断连时选择辅助通道下发消息。当前辅助通道通过华为、小米推送下发透传消息,消息到达应用后经移动推送SDK处理后触发onNotification,onMessage回调。小米、华为推送在下发透传消息时并不保证会拉起被杀死进程(相关机制可参考小米、华为推送官网),所以辅助通道在进程被杀死情况下无法保证消息一定到达。
  • 辅助弹窗:辅助弹窗通过系统通道下发通知,可以在进程被杀死情况下推送成功。由于辅助弹窗通过在对应设备上推送通知实现,因而通过辅助弹窗下发的通知不会触发onNotification回调。当前移动推送已接入小米、华为辅助弹窗。其中华为弹窗到达率统计只覆盖用户点击华为弹窗推送通知的场景,未点击部分暂未覆盖,小米弹窗到达率统计覆盖所有场景。


2. 配置应用

  • 在 小米开放平台 注册你的App, 得到相应的小米AppID,小米AppKey,小米AppSecert。在控制台App详情设置你的小米AppSecert。(注意:小米开发者平台的消息推送开关记得打开)
  • 同理在 华为开发者联盟 注册App,应用审核通过后,能够得到华为的AppID和AppSecert。在控制台App详情中设置你的华为AppID和AppSecert。(注意,您的app不能是草稿状态,必须是审核中,或者通过审核的状态,不然通道不会生效。请确保您在华为控制台激活了推送通道功能)
  • 在FCM推送平台创建项目(注:Google已将GCM推送迁移至firebase,改称FCM)。接入前提手机必须安装google play services, 否则注册不成功, 大部分国内的手机是谷歌服务被剥离了:在Firebase控制台需要先创建一个项目,然后再在项目下新增App
  • 下载对应App的google-services.json文件, 我们只需要这个json文件中的”project_number”, “mobilesdk_app_id”这两个key对应的value, 分别记录为sendId/applicationId, 下面注册过程中需要用到:



    3. 下载扩展包



    3.1 手动集成


    将辅助通道扩展包下载解压后拷贝到你项目的Lib目录下,下载地址。 如果使用辅助通道扩展包v3.0.4及以上版本,需要将推送SDK升级到v3.0.11及以上版本。新版辅助通道扩展包以aar形式透出,省却manifest文件配置,减少出错概率。
    将辅助通道扩展包放置到app module的libs路径下,并在app module的build.gradle文件中添加如下配置:
    1. [backcolor=transparent]repositories [backcolor=transparent]{
    2. [backcolor=transparent]    flatDir [backcolor=transparent]{
    3. [backcolor=transparent]        dirs [backcolor=transparent]'libs'[backcolor=transparent] [backcolor=transparent]//this way we can find the .aar file in libs folder
    4. [backcolor=transparent]    [backcolor=transparent]}
    5. [backcolor=transparent]}
    6. [backcolor=transparent]...
    7. [backcolor=transparent]dependencies [backcolor=transparent]{
    8. [backcolor=transparent]    [backcolor=transparent]......
    9. [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])
    10. [backcolor=transparent]}


    3.2 Maven集成

    • 项目顶层build.gradle中添加Maven仓库地址:
    1. [backcolor=transparent]allprojects [backcolor=transparent]{
    2. [backcolor=transparent]    repositories [backcolor=transparent]{
    3. [backcolor=transparent]        maven [backcolor=transparent]{
    4. [backcolor=transparent]            url [backcolor=transparent]'http://maven.aliyun.com/nexus/content/repositories/releases/'
    5. [backcolor=transparent]        [backcolor=transparent]}
    6. [backcolor=transparent]    [backcolor=transparent]}
    7. [backcolor=transparent]}
    • gradle添加依赖:
    1. [backcolor=transparent]dependencies [backcolor=transparent]{
    2. [backcolor=transparent]    compile [backcolor=transparent]'com.aliyun.ams:alicloud-android-third-push:3.0.5@aar'
    3. [backcolor=transparent]}


    3.3 Firebase依赖


    如需配置GCM/GCM通道还需要添加Firebase SDK依赖:
    1. [backcolor=transparent]dependencies [backcolor=transparent]{
    2. [backcolor=transparent]    [backcolor=transparent]......
    3. [backcolor=transparent]    compile [backcolor=transparent]([backcolor=transparent]'com.google.firebase:firebase-messaging:9.6.1'[backcolor=transparent])
    4. [backcolor=transparent]}

    同时在AndroidManifest文件中添加如下配置:
    1. [backcolor=transparent]<receiver
    2. [backcolor=transparent]    [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.iid.FirebaseInstanceIdReceiver"
    3. [backcolor=transparent]    [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"true"
    4. [backcolor=transparent]    [backcolor=transparent]android:permission[backcolor=transparent]=[backcolor=transparent]"com.google.android.c2dm.permission.SEND"[backcolor=transparent] [backcolor=transparent]>
    5. [backcolor=transparent]    [backcolor=transparent]<intent-filter>
    6. [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]/>
    7. [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]/>
    8. [backcolor=transparent]        [backcolor=transparent]<category[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.taobao.taobao"[backcolor=transparent] [backcolor=transparent]/>
    9. [backcolor=transparent]    [backcolor=transparent]</intent-filter>
    10. [backcolor=transparent]</receiver>
    11. [backcolor=transparent]<receiver
    12. [backcolor=transparent]    [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
    13. [backcolor=transparent]    [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"false"[backcolor=transparent] [backcolor=transparent]/>
    14. [backcolor=transparent]<service
    15. [backcolor=transparent]    [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.alibaba.sdk.android.push.AgooFirebaseInstanceIDService"
    16. [backcolor=transparent]    [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"true"[backcolor=transparent] [backcolor=transparent]>
    17. [backcolor=transparent]    [backcolor=transparent]<intent-filter[backcolor=transparent] [backcolor=transparent]android:priority[backcolor=transparent]=[backcolor=transparent]"-500"[backcolor=transparent] [backcolor=transparent]>
    18. [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]/>
    19. [backcolor=transparent]    [backcolor=transparent]</intent-filter>
    20. [backcolor=transparent]</service>
    21. [backcolor=transparent]<service
    22. [backcolor=transparent]    [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.alibaba.sdk.android.push.AgooFirebaseMessagingService"
    23. [backcolor=transparent]    [backcolor=transparent]android:exported[backcolor=transparent]=[backcolor=transparent]"true"[backcolor=transparent] [backcolor=transparent]>
    24. [backcolor=transparent]    [backcolor=transparent]<intent-filter[backcolor=transparent] [backcolor=transparent]android:priority[backcolor=transparent]=[backcolor=transparent]"-500"[backcolor=transparent] [backcolor=transparent]>
    25. [backcolor=transparent]        [backcolor=transparent]<action[backcolor=transparent] [backcolor=transparent]android:name[backcolor=transparent]=[backcolor=transparent]"com.google.firebase.MESSAGING_EVENT"[backcolor=transparent] [backcolor=transparent]/>
    26. [backcolor=transparent]    [backcolor=transparent]</intent-filter>
    27. [backcolor=transparent]</service>


    4. Proguard配置

    • 如果集成推送SDK的工程开启代码混淆,在Proguard配置的基础上,需要添加以下辅助通道的Proguard配置。
    1. [backcolor=transparent]# 小米通道
    2. [backcolor=transparent]-[backcolor=transparent]keep [backcolor=transparent]class[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]xiaomi[backcolor=transparent].**[backcolor=transparent] [backcolor=transparent]{*;}
    3. [backcolor=transparent]-[backcolor=transparent]dontwarn com[backcolor=transparent].[backcolor=transparent]xiaomi[backcolor=transparent].**
    4. [backcolor=transparent]# 华为通道
    5. [backcolor=transparent]-[backcolor=transparent]keep [backcolor=transparent]class[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]huawei[backcolor=transparent].**[backcolor=transparent] [backcolor=transparent]{*;}
    6. [backcolor=transparent]-[backcolor=transparent]dontwarn com[backcolor=transparent].[backcolor=transparent]huawei[backcolor=transparent].**
    7. [backcolor=transparent]# GCM/FCM通道
    8. [backcolor=transparent]-[backcolor=transparent]keep [backcolor=transparent]class[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]google[backcolor=transparent].[backcolor=transparent]firebase[backcolor=transparent].**{*;}
    9. [backcolor=transparent]-[backcolor=transparent] dontwarn com[backcolor=transparent].[backcolor=transparent]google[backcolor=transparent].[backcolor=transparent]firebase[backcolor=transparent].**


    5. 在应用中初始化辅助通道


    将以下代码加入你application.onCreate()方法中初始通道。 注意:辅助通道注册务必在Application中执行且放在推送SDK初始化代码之后,否则可能导致辅助通道注册失败
    1. [backcolor=transparent]// 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
    2. [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]);
    3. [backcolor=transparent]// 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。
    4. [backcolor=transparent]HuaWeiRegister[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent]([backcolor=transparent]applicationContext[backcolor=transparent]);
    5. [backcolor=transparent]//GCM/FCM辅助通道注册
    6. [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为步骤获得的参数

    本方法会自动判断是否支持小米系统推送,如不支持会跳过注册。

展开
收起
猫饭先生 2017-10-23 11:02:10 3138 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载
内容驱动游戏分发 立即下载
“静态调用链路发现”应用场景分析及实践探索 立即下载