移动开发平台 mPaaS 接入第三方推送渠道

By | 2021年4月23日

为提升推送的到达率,mPaaS 集成了华为、小米、OPPO 和 vivo 等厂商的推送功能。采用 小米通知栏消息华为通知消息OPPO 通知栏消息vivo 通知栏消息 实现推送,在进程挂起时,依然可以发送通知,用户点击通知栏可以激活进程。

说明:接入厂商自有的推送渠道后,能够帮助应用获得稳定的推送性能,因此建议您将第三方推送渠道接入您的应用。

本文档分为 客户端接入第三方推送渠道接入 MpaaSNcActivity 两部分,引导开发者接入三方渠道的推送功能。在 客户端接入第三方推送渠道 中,分别介绍了接入华为、小米、OPPO 和 vivo 四家厂商的推送服务所需要进行的客户端配置。

客户端接入第三方推送渠道

接入华为渠道

注册华为推送

登录华为开发官网,注册账号并且开启推送服务。详情请参见 华为推送开启步骤

客户端接入华为推送

MPS 支持接入华为 HMS2 和 HMS5,但在接入华为推送组件过程中,您只能选择其中一个版本进行接入。下面分别对华为 HMS2 和 HMS5 这两个版本的接入方式进行说明。

接入华为推送 – HMS2.x 版本
  1. 接入 MPS 依赖。第三方渠道的接入与 MPS 自建通道的接入完全相同,更多信息请参见 添加 SDK
  2. 在 IDE 插件中添加 推送 – 华为2 组件。
  3. 配置 AndroidManifest.xml

    如果是基于 mPaaS 框架的工程,请在 Portal 工程的 AndroidManifest.xml 配置文件中进行配置。

        
    1. <activity
    2. android:name="com.huawei.hms.activity.BridgeActivity"
    3. android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
    4. android:excludeFromRecents="true"
    5. android:exported="false"
    6. android:hardwareAccelerated="true"
    7. android:theme="@android:style/Theme.Translucent">
    8. <meta-data
    9. android:name="hwc-theme"
    10. android:value="androidhwext:style/Theme.Emui.Translucent" />
    11. </activity>
    12. <!--为了防止低版本 dex 崩溃,动态开启 provider,enabled 设置为 false-->
    13. <provider
    14. android:name="com.huawei.hms.update.provider.UpdateProvider"
    15. android:authorities="${applicationId}.hms.update.provider"
    16. android:exported="false"
    17. android:enabled="false"
    18. android:grantUriPermissions="true">
    19. </provider>
    20. <!-- value 的值“appid”用实际申请的应用 ID 替换,来源于开发者联盟网站应用的服务详情。注意,value 中的斜杠(\)及空格要保留。-->
    21. <meta-data
    22. android:name="com.huawei.hms.client.appid"
    23. android:value="\ your huawei appId" />
    24. <receiver
    25. android:name="com.huawei.hms.support.api.push.PushEventReceiver"
    26. >
    27. <intent-filter>
    28. <!-- 接收通道发来的通知栏消息,兼容老版本PUSH -->
    29. <action android:name="com.huawei.intent.action.PUSH" />
    30. </intent-filter>
    31. </receiver>
    32. <receiver
    33. android:name="com.alipay.pushsdk.thirdparty.huawei.HuaweiPushReceiver"
    34. android:process=":push">
    35. <intent-filter>
    36. <!-- 必须,用于接收 TOKEN -->
    37. <action android:name="com.huawei.android.push.intent.REGISTRATION" />
    38. <!-- 必须,用于接收消息 -->
    39. <action android:name="com.huawei.android.push.intent.RECEIVE" />
    40. <!-- 可选,用于点击通知栏或通知栏上的按钮后触发 onEvent 回调 -->
    41. <action android:name="com.huawei.android.push.intent.CLICK" />
    42. <!-- 可选,查看PUSH通道是否连接,不查看则不需要 -->
    43. <action android:name="com.huawei.intent.action.PUSH_STATE" />
    44. </intent-filter>
    45. </receiver>
  4. 接入 MpaaSNcActivity
接入华为推送 – HMS5.x 版本
  1. 接入 MPS 依赖。第三方渠道的接入与 MPS 自建通道的接入完全相同,更多信息请参见 添加 SDK
  2. 在 IDE 插件中添加 推送 – HMS5 组件。
  3. 在华为应用服务控制台下载配置文件 agconnect-services.json 并放置到应用主工程的 assets 目录下。
  4. 在项目根目录下的 build.gradle 文件中配置 HMS Core SDK 的 Maven 仓地址。
        
    1. allprojects {
    2. repositories {
    3. // 其他repo已省略
    4. maven {url 'https://developer.huawei.com/repo/'}
    5. }
    6. }
  5. 在主工程的 build.gradle 文件中添加 HMS Core SDK 依赖。

        
    1. dependencies {
    2. implementation 'com.huawei.hms:push:5.0.2.300'
    3. // 华为HMS Core SDK版本会经常更新,最新版本号可以参考 https://developer.huawei.com/consumer/cn/doc/HMSCore-Guides-V5/version-change-history-0000001050040072-V5
    4. }
  6. 接入 MpaaSNcActivity
  7. 如果您是从 2.x 版本升级到 5.x 版本,请卸载已安装的 推送 – 华为2 组件,并移除相应的 AndroidManifest.xml 文件中的华为推送配置。

应用角标处理

mPaaS 自 10.1.60.13 基线版本起支持应用角标处理。当服务端推送携带角标信息的消息到客户端时,SDK 提供接口处理应用图标上的角标信息。

MPPush 类提供以下三个处理应用角标的接口:

  • 设置是否允许点击通知栏消息时自动清除应用角标消息。enable 为 true 时表示允许,false 表示不允许。
        
    1. void setBadgeAutoClearEnabled(Context context, boolean enabled)
  • 设置角标展示所在的应用图标对应的 Activity 名称。必须设置,否则无法清除角标信息。
        
    1. void setBadgeActivityClassName(Context context, String name)
  • 清除应用角标信息。SDK 不能处理角标信息的情况下可以使用此方法主动清除角标信息,例如用户主动点击应用图标进入应用时清除角标信息。
        
    1. void clearBadges(Context context)

接入小米渠道

注册小米推送

参考以下小米官方文档,完成小米推送注册:

客户端接入小米推送

  1. 接入 MPS 依赖。第三方渠道的接入与 MPS 自建通道的接入完全相同,更多信息请参见 添加 SDK
  2. 在 IDE 插件中添加 推送 – 小米 组件。
  3. 配置 AndroidManifest.xml

    如果是基于 mPaaS 框架的工程,请在 Portal 工程的 AndroidManifest.xml 配置文件中进行配置。

        
    1. <!--由于涉及到签名,权限放在 Portal 中-->
    2. <permission
    3. android:name="${applicationId}.permission.MIPUSH_RECEIVE"
    4. android:protectionLevel="signature"/>
    5. <uses-permission android:name="${applicationId}.permission.MIPUSH_RECEIVE"/>
    6. <!-- value 中的斜杠(\)及空格要保留 -->
    7. <meta-data
    8. android:name="xiaomi_appid"
    9. android:value="\ 2xxxxxxxxxx" />
    10. <!-- value 中的斜杠(\)及空格要保留 -->
    11. <meta-data
    12. android:name="xiaomi_appkey"
    13. android:value="\ 5xxxxxxxxxxxx" />
    14. <service android:name="com.xiaomi.push.service.XMJobService"
    15. android:enabled="true"
    16. android:exported="false"
    17. android:permission="android.permission.BIND_JOB_SERVICE"
    18. android:process=":push" />
    19. <service
    20. android:name="com.xiaomi.push.service.XMPushService"
    21. android:enabled="true"
    22. android:process=":push"/>
    23. <service
    24. android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
    25. android:enabled="true"
    26. android:exported="true"
    27. android:process=":push"/>
    28. <service
    29. android:name="com.xiaomi.mipush.sdk.MessageHandleService"
    30. android:enabled="true"
    31. android:process=":push"/>
    32. <receiver
    33. android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
    34. android:exported="true"
    35. android:process=":push">
    36. <intent-filter>
    37. <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    38. <category android:name="android.intent.category.DEFAULT"/>
    39. </intent-filter>
    40. </receiver>
    41. <receiver
    42. android:name="com.xiaomi.push.service.receivers.PingReceiver"
    43. android:exported="false"
    44. android:process=":push">
    45. <intent-filter>
    46. <action android:name="com.xiaomi.push.PING_TIMER"/>
    47. </intent-filter>
    48. </receiver>
    49. <receiver
    50. android:name="com.alipay.pushsdk.thirdparty.xiaomi.XiaoMiMsgReceiver"
    51. android:exported="true"
    52. android:process=":push">
    53. <intent-filter>
    54. <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE"/>
    55. </intent-filter>
    56. <intent-filter>
    57. <action android:name="com.xiaomi.mipush.ERROR"/>
    58. </intent-filter>
    59. <intent-filter>
    60. <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED"/>
    61. </intent-filter>
    62. </receiver>
  4. 接入 MpaaSNcActivity

接入 OPPO 渠道

说明:仅 10.1.60 及以上基线版本支持接入 OPPO 推送渠道。如果您使用了较早版本基线提供的
AliPushInterface 类进行初始化,请替换成使用
MPPush 类。

注册 OPPO 推送

参考 OPPO推送平台使用指南OPPO 开放平台 注册账号并申请接入推送服务。

客户端接入 OPPO 推送

  1. mPaaS 不提供 OPPO Push SDK 的库,请前往 OPPO SDK 文档 下载 SDK 并集成到主工程中。
    说明:当前适配的版本为 2.0.2。
  2. 在 IDE 插件中添加 推送 – OPPO 组件。
  3. 配置 AndroidManifest.xml

    如果是基于 mPaaS 框架的工程,请在 Portal 工程的 AndroidManifest.xml 配置文件中进行配置。

        
    1. <uses-permission android:name="com.coloros.mcs.permission.RECIEVE_MCS_MESSAGE" />
    2. <uses-permission android:name="com.heytap.mcs.permission.RECIEVE_MCS_MESSAGE"/>
    3. <application>
    4. <service
    5. android:name="com.heytap.mcssdk.PushService"
    6. android:permission="com.coloros.mcs.permission.SEND_MCS_MESSAGE"
    7. android:process=":push">
    8. <intent-filter>
    9. <action android:name="com.coloros.mcs.action.RECEIVE_MCS_MESSAGE"/>
    10. </intent-filter>
    11. </service>
    12. <service
    13. android:name="com.heytap.mcssdk.AppPushService"
    14. android:permission="com.heytap.mcs.permission.SEND_MCS_MESSAGE"
    15. android:process=":push">
    16. <intent-filter>
    17. <action android:name="com.heytap.mcs.action.RECEIVE_MCS_MESSAGE"/>
    18. </intent-filter>
    19. </service>
    20. <meta-data
    21. android:name="com.oppo.push.app_key"
    22. android:value="OPPO开放平台获取"
    23. />
    24. <meta-data
    25. android:name="com.oppo.push.app_secret"
    26. android:value="OPPO开放平台获取"
    27. />
    28. <meta-data
    29. android:name="mpaas.notification.channel.default.name"
    30. android:value="默认"
    31. />
    32. <meta-data
    33. android:name="mpaas.notification.channel.default.description"
    34. android:value="默认消息通知"
    35. />
    36. </application>
  4. 接入 MpaaSNcActivity

接入 vivo 渠道

说明:仅 10.1.60 及以上基线版本支持接入 vivo 推送渠道。

注册 vivo 推送

参考 vivo 推送平台使用指南,在 vivo 开放平台 上注册账号,并申请接入推送服务。

客户端接入 vivo 推送

  1. mPaaS 不提供 vivo Push SDK 的库,请前往 vivo SDK 文档 下载 SDK 并集成到主工程中。
  2. 在 IDE 插件中添加 推送 – vivo 组件。
  3. 配置 AndroidManifest.xml

    如果是基于 mPaaS 框架的工程,请在 Portal 工程的 AndroidManifest.xml 配置文件中进行配置。

        
    1. <application>
    2. <service
    3. android:name="com.vivo.push.sdk.service.CommandClientService"
    4. android:process=":push"
    5. android:exported="true" />
    6. <activity
    7. android:name="com.vivo.push.sdk.LinkProxyClientActivity"
    8. android:exported="false"
    9. android:process=":push"
    10. android:screenOrientation="portrait"
    11. android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    12. <meta-data
    13. android:name="com.vivo.push.api_key"
    14. android:value="VIVO开放平台提供" />
    15. <meta-data
    16. android:name="com.vivo.push.app_id"
    17. android:value="VIVO开放平台提供" />
    18. <receiver
    19. android:name="com.alipay.pushsdk.thirdparty.vivo.PushMessageReceiver"
    20. android:process=":push" >
    21. <intent-filter>
    22. <!-- 接收push消息 -->
    23. <action android:name="com.vivo.pushclient.action.RECEIVE" />
    24. </intent-filter>
    25. </receiver>
    26. </application>
  4. 接入 MpaaSNcActivity

接入 MpaaSNcActivity

在不接入 MpaaSNcActivity 的情况下,第三方渠道消息将无法跳转。

  
  1. <activity
  2. android:name="com.alipay.pushsdk.thirdparty.MPaaSNcActivity"
  3. android:exported="true"
  4. android:theme="@android:style/Theme.Translucent">
  5. <intent-filter>
  6. <action android:name="android.intent.action.VIEW" />
  7. <category android:name="android.intent.category.DEFAULT" />
  8. </intent-filter>
  9. </activity>

后续操作

在完成上述客户端接入第三方推送渠道的配置后,需要在消息推送控制台上配置对应三方渠道的相关参数。更多信息,参见 控制台渠道配置

请关注公众号获取更多资料

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注