开发者社区> 问答> 正文

移动推送Android SDK:在Activity中初始化推送SDK,无法接收到推送通知为什么?



1.问题


在Activity中执行了推送的初始化逻辑,SDK的接入按照集成文档操作确认无误,为什么推送消息接收不到,或者有时能接收到有时接收不到?相关初始化代码如下:

  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]MyActivity[backcolor=transparent] [backcolor=transparent]extends[backcolor=transparent] [backcolor=transparent]Activity[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]   [backcolor=transparent]@override
  3. [backcolor=transparent]   [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onCreate[backcolor=transparent]([backcolor=transparent]Bundle[backcolor=transparent] savedInstanceState[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  4. [backcolor=transparent]       [backcolor=transparent]super[backcolor=transparent].[backcolor=transparent]onCreate[backcolor=transparent]([backcolor=transparent]savedInstanceState[backcolor=transparent]);
  5. [backcolor=transparent]       initCloudChannel[backcolor=transparent]([backcolor=transparent]getApplicationContext[backcolor=transparent]());
  6. [backcolor=transparent]       [backcolor=transparent]......
  7. [backcolor=transparent]   [backcolor=transparent]}
  8. [backcolor=transparent]   [backcolor=transparent]......
  9. [backcolor=transparent]}


2.问题背景,原因


推送SDK为了提高推送到达率做了进程保活,除了应用进程之外,还有辅助进程channel。在诸如应用退到后台,设备锁屏或者应用杀死等场景都是通过channel进程接收推送消息的。而channel进程的正常工作是依赖于channel的成功初始化。如果您在Activity中执行了推送的初始化逻辑将导致channel进程无法完成初始化操作。channel进程无法正常工作将严重影响推送到达率。

3.解决方案


将推送初始化逻辑移至Application中,并保证主进程和channel进程均可以执行相关代码:
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]MyApplication[backcolor=transparent] [backcolor=transparent]extends[backcolor=transparent] [backcolor=transparent]Application[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]   [backcolor=transparent]@override
  3. [backcolor=transparent]   [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onCreate[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
  4. [backcolor=transparent]       [backcolor=transparent]super[backcolor=transparent].[backcolor=transparent]onCreate[backcolor=transparent]();
  5. [backcolor=transparent]       [backcolor=transparent]if[backcolor=transparent](如果是主进程或[backcolor=transparent]channel[backcolor=transparent]进程)[backcolor=transparent] [backcolor=transparent]{
  6. [backcolor=transparent]           initCloudChannel[backcolor=transparent]([backcolor=transparent]this[backcolor=transparent]);
  7. [backcolor=transparent]       [backcolor=transparent]}
  8. [backcolor=transparent]   [backcolor=transparent]}
  9. [backcolor=transparent]   [backcolor=transparent]......
  10. [backcolor=transparent]}

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

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载