Android SDK 3.0配置如何实现?(4)-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

Android SDK 3.0配置如何实现?(4)

2017-10-23 10:56:36 1232 0


3.6. 在应用中注册和启动移动推送

  • 首先通过PushServiceFactory获取到CloudPushService,然后调用register()初始化并注册云推送通道,并确保Application上下文中进行初始化工作。
  • 请参照以下代码段进行初始化:
  1. [backcolor=transparent]import[backcolor=transparent] android[backcolor=transparent].[backcolor=transparent]app[backcolor=transparent].[backcolor=transparent]Application[backcolor=transparent];
  2. [backcolor=transparent]import[backcolor=transparent] android[backcolor=transparent].[backcolor=transparent]content[backcolor=transparent].[backcolor=transparent]Context[backcolor=transparent];
  3. [backcolor=transparent]import[backcolor=transparent] android[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Log[backcolor=transparent];
  4. [backcolor=transparent]import[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]sdk[backcolor=transparent].[backcolor=transparent]android[backcolor=transparent].[backcolor=transparent]callback[backcolor=transparent].[backcolor=transparent]InitResultCallback[backcolor=transparent];
  5. [backcolor=transparent]import[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]sdk[backcolor=transparent].[backcolor=transparent]android[backcolor=transparent].[backcolor=transparent]push[backcolor=transparent].[backcolor=transparent]CloudPushService[backcolor=transparent];
  6. [backcolor=transparent]import[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]sdk[backcolor=transparent].[backcolor=transparent]android[backcolor=transparent].[backcolor=transparent]push[backcolor=transparent].[backcolor=transparent]CommonCallback[backcolor=transparent];
  7. [backcolor=transparent]import[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]sdk[backcolor=transparent].[backcolor=transparent]android[backcolor=transparent].[backcolor=transparent]push[backcolor=transparent].[backcolor=transparent]noonesdk[backcolor=transparent].[backcolor=transparent]PushServiceFactory[backcolor=transparent];
  8. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]MainApplication[backcolor=transparent] [backcolor=transparent]extends[backcolor=transparent] [backcolor=transparent]Application[backcolor=transparent] [backcolor=transparent]{
  9. [backcolor=transparent]    [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] TAG [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"Init"[backcolor=transparent];
  10. [backcolor=transparent]    [backcolor=transparent]@Override
  11. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onCreate[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
  12. [backcolor=transparent]        [backcolor=transparent]super[backcolor=transparent].[backcolor=transparent]onCreate[backcolor=transparent]();
  13. [backcolor=transparent]        initCloudChannel[backcolor=transparent]([backcolor=transparent]this[backcolor=transparent]);
  14. [backcolor=transparent]    [backcolor=transparent]}
  15. [backcolor=transparent]    [backcolor=transparent]/**
  16. [backcolor=transparent]     * 初始化云推送通道
  17. [backcolor=transparent]     * @param applicationContext
  18. [backcolor=transparent]     */
  19. [backcolor=transparent]    [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] initCloudChannel[backcolor=transparent]([backcolor=transparent]Context[backcolor=transparent] applicationContext[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  20. [backcolor=transparent]        [backcolor=transparent]PushServiceFactory[backcolor=transparent].[backcolor=transparent]init[backcolor=transparent]([backcolor=transparent]applicationContext[backcolor=transparent]);
  21. [backcolor=transparent]        [backcolor=transparent]CloudPushService[backcolor=transparent] pushService [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]PushServiceFactory[backcolor=transparent].[backcolor=transparent]getCloudPushService[backcolor=transparent]();
  22. [backcolor=transparent]        pushService[backcolor=transparent].[backcolor=transparent]register[backcolor=transparent]([backcolor=transparent]applicationContext[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]CommonCallback[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
  23. [backcolor=transparent]            [backcolor=transparent]@Override
  24. [backcolor=transparent]            [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onSuccess[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] response[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  25. [backcolor=transparent]                [backcolor=transparent]Log[backcolor=transparent].[backcolor=transparent]d[backcolor=transparent]([backcolor=transparent]TAG[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"init cloudchannel success"[backcolor=transparent]);
  26. [backcolor=transparent]            [backcolor=transparent]}
  27. [backcolor=transparent]            [backcolor=transparent]@Override
  28. [backcolor=transparent]            [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onFailed[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] errorCode[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] errorMessage[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  29. [backcolor=transparent]                [backcolor=transparent]Log[backcolor=transparent].[backcolor=transparent]d[backcolor=transparent]([backcolor=transparent]TAG[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"init cloudchannel failed -- errorcode:"[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] errorCode [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]" -- errorMessage:"[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] errorMessage[backcolor=transparent]);
  30. [backcolor=transparent]            [backcolor=transparent]}
  31. [backcolor=transparent]        [backcolor=transparent]});
  32. [backcolor=transparent]    [backcolor=transparent]}
  33. [backcolor=transparent]}

【注意】:
  • 移动推送的初始化必须在Application中,不能放到Activity中执行。移动推送在初始化过程中将启动后台进程channel,必须保证应用进程和channel进程都执行到推送初始化代码。
  • 如果设备成功注册,将回调callback.onSuccess()方法。
  • 但如果注册服务器连接失败,则调用callback.onFailed方法,并且自动进行重新注册,直到onSuccess为止。(重试规则会由网络切换等时间自动触发。)
  • 请在网络通畅的情况下进行相关的初始化调试,如果网络不通,或者App信息配置错误,在onFailed方法中,会有相应的错误码返回,可参考错误处理

启动正常确认方法:
  • 回调方法callback.onSuccess()被调用。以上文接入代码为例,logcat将会打印以下日志:
  1. [backcolor=transparent]11[backcolor=transparent]-[backcolor=transparent]24[backcolor=transparent] [backcolor=transparent]12[backcolor=transparent]:[backcolor=transparent]55[backcolor=transparent]:[backcolor=transparent]51.096[backcolor=transparent]  [backcolor=transparent]15235[backcolor=transparent]-[backcolor=transparent]15535[backcolor=transparent]/[backcolor=transparent]com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]xxxx D[backcolor=transparent]/[backcolor=transparent]YourApp[backcolor=transparent][backcolor=transparent] init cloudchannel success
  • 确认cloudchannel初始化正常,在logcat日志中:输入awcn关键字:
  1. [backcolor=transparent]11[backcolor=transparent]-[backcolor=transparent]24[backcolor=transparent] [backcolor=transparent]12[backcolor=transparent]:[backcolor=transparent]53[backcolor=transparent]:[backcolor=transparent]51.036[backcolor=transparent]  [backcolor=transparent]15235[backcolor=transparent]-[backcolor=transparent]15556[backcolor=transparent]/[backcolor=transparent]com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]xxxx E[backcolor=transparent]/[backcolor=transparent]awcn[backcolor=transparent][backcolor=transparent] [backcolor=transparent]|[[backcolor=transparent]seq[backcolor=transparent]:[backcolor=transparent]AWCN1_1[backcolor=transparent]][backcolor=transparent] AUTH httpStatusCode[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]200
  2. [backcolor=transparent]11[backcolor=transparent]-[backcolor=transparent]24[backcolor=transparent] [backcolor=transparent]12[backcolor=transparent]:[backcolor=transparent]53[backcolor=transparent]:[backcolor=transparent]51.036[backcolor=transparent]  [backcolor=transparent]15235[backcolor=transparent]-[backcolor=transparent]15556[backcolor=transparent]/[backcolor=transparent]com[backcolor=transparent].[backcolor=transparent]alibaba[backcolor=transparent].[backcolor=transparent]xxxx E[backcolor=transparent]/[backcolor=transparent]awcn[backcolor=transparent][backcolor=transparent] [backcolor=transparent]|[[backcolor=transparent]seq[backcolor=transparent]:[backcolor=transparent]AWCN1_1[backcolor=transparent]][backcolor=transparent] status[backcolor=transparent]:[backcolor=transparent]AUTH_SUCC
取消 提交回答
全部回答(0)
相关问答

1

回答

安卓端oss的sdk

2018-03-22 10:10:13 613浏览量 回答数 1

1

回答

安卓sdk 签名公开的访问URL

2018-04-04 14:40:12 783浏览量 回答数 1

1

回答

安卓sdk 签名私有资源的指定有效时长的访问URL

2018-02-22 07:22:37 796浏览量 回答数 1

1

回答

短视频上传SDK ,安卓没法指定转码模板

2018-09-06 17:26:01 1124浏览量 回答数 1

0

回答

安卓sdk日志上报重复

2019-01-15 22:07:41 576浏览量 回答数 0

1

回答

请问微信分享sdk分享二维码图片到安卓微信,没有长按识别二维码选项

2016-03-09 18:36:56 3614浏览量 回答数 1

1

回答

修改了Android项目的最小SDK版本之后出现很多stysle文件找不到-安卓报错

2020-05-29 09:55:45 286浏览量 回答数 1

1

回答

解决android 6.0(api 23) SDK,不再提供org.apache.http-安卓报错

2020-05-29 19:54:25 265浏览量 回答数 1

1

回答

android 三个配置放在一起代表什么意思-安卓报错

2020-05-31 15:05:16 288浏览量 回答数 1

1

回答

android 三个配置放在一起代表什么意思 - 安卓报错

2020-06-02 20:41:24 342浏览量 回答数 1
+关注
文章
问答
问答排行榜
最热
最新
相关电子书
更多
Android热修复技术原理详解与升级探索
立即下载
《深入探索Android热修复技术原理》
立即下载
Android插件化:从入门到放弃
立即下载