全部产品
移动推送

Android SDK 3.0配置

更新时间:2017-09-12 10:48:49   分享:   

本集成文档适用于V3.0.0及以上版本。

Android参考Demo

新版Android SDK(v3.0.0及以上)支持两种集成方式

  • 1.Maven库快速集成
  • 2.手动集成

Maven库快速集成,该集成方案配置简单,不容易出问题,后续更新方便,所以强烈建议开发者采用该方案进行集成。

如果您坚持使用原有集成方案,请参考手动集成方案。


一、Maven库快速集成(远程同步)

  • 在Project根目录下build.gradle文件中配置maven库URL:
  1. allprojects {
  2. repositories {
  3. jcenter()
  4. maven {
  5. url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
  6. }
  7. }
  8. }
  • 在对应的module下的build.gradle文件中添加对应依赖
  1. android {
  2. ......
  3. defaultConfig {
  4. applicationId "com.xxx.xxx" //包名
  5. ......
  6. ndk {
  7. //选择要添加的对应cpu类型的.so库。
  8. abiFilters 'armeabi', 'x86'
  9. }
  10. ......
  11. }
  12. ......
  13. }
  14. dependencies {
  15. ......
  16. compile 'com.aliyun.ams:alicloud-android-push:3.0.11@aar'
  17. compile 'com.aliyun.ams:alicloud-android-utdid:1.1.5.3'
  18. compile 'com.aliyun.ams:alicloud-android-ut:5.1.0'
  19. ......
  20. }

注 : 如果在添加以上 abiFilter 配置之后android Studio出现以下提示:

  1. NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.

则在 Project 根目录的gradle.properties文件中添加:

  1. android.useDeprecatedNdk=true

二、手动集成

  • 下载SDK,其目录结构为:

    1. SDK
    2. |-- libs
    3. | |-- armeabi
    4. | | |--libcocklogic-1.1.3.so -网络连接库及幽灵进程的辅助lib
    5. | | |--libtnet-3.1.11.so
    6. | |-- arm64-v8a
    7. | | |--libcocklogic-1.1.3.so
    8. | | |--libtnet-3.1.11.so
    9. | |-- armeabi-v7a
    10. | | |--libcocklogic-1.1.3.so
    11. | | |--libtnet-3.1.11.so
    12. | |-- x86
    13. | | |--libcocklogic-1.1.3.so
    14. | | |--libtnet-3.1.11.so
    15. | |-- x86_64
    16. | | |--libcocklogic-1.1.3.so
    17. | | |--libtnet-3.1.11.so
    18. | |-- mips
    19. | | |--libtnet-3.1.11.so
    20. | |-- mips64
    21. | | |--libtnet-3.1.11.so
    22. | |--alicloud-android-push.jar -移动推送主功能包
    23. | |--alisdk-ut.jar -UT基础包
    24. | |--utdid4all.jar -设备Id生成包
  • 手动拷贝libs/armeabi,libs/x86,alisdk-ut.jar,utdid4all.jar,alicloud-android-push.jar到工程下libs目录

  • APP工程的build.gradle中配置jniLibs的目录
  1. android {
  2. ...
  3. sourceSets {
  4. main {
  5. jniLibs.srcDirs = ['libs']
  6. }
  7. }
  8. }

三、AndroidManifest配置

3.1 appKey, appSecret配置

  • 在AndroidManifest文件中设置appKey,appSecret:
  1. <application android:name="*****">
  2. <meta-data android:name="com.alibaba.app.appkey" android:value="*****"/> <!-- 请填写你自己的- appKey -->
  3. <meta-data android:name="com.alibaba.app.appsecret" android:value="****"/> <!-- 请填写你自己的appSecret -->
  4. </application>

com.alibaba.app.appkeycom.alibaba.app.appsecret为您App的对应信息,在推送控制台APP列表页的应用证书中获取。appkeyappsecret请务必写在application标签下,否则sdk会报找不到appkey错误。如果您是百川云推送用户,不能直接使用百川平台的appKey和appSecret,需要登录阿里云移动推送控制台,登录账号为您的百川平台账号,并使用阿里云平台的appKeyappSecret

3.2 Permission配置(Maven库快速集成可跳过)

将以下uses-permission片段拷贝进你manifest中的Permission申明区域中:

  1. <!-- 阿里云推送相关权限 -->
  2. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  3. <uses-permission android:name="android.permission.INTERNET" />
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  5. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  6. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  7. <uses-permission android:name="android.permission.WAKE_LOCK" />
  8. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  9. <uses-permission android:name="android.permission.RESTART_PACKAGES" />
  10. <uses-permission android:name="android.permission.GET_TASKS" />
  11. <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  12. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  13. <uses-permission android:name="android.permission.VIBRATE" />
  14. <uses-permission android:name="android.permission.REORDER_TASKS" />
  15. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  16. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  17. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

3.3 其他组件配置(Maven库快速集成可跳过)

将以下片段拷贝进你Manifest中的Application标签下

  1. <!-- Push SDK 相关组件,required-->
  2. <!-- 消息接收服务 -->
  3. <service
  4. android:name="com.alibaba.sdk.android.push.MsgService"
  5. android:exported="false">
  6. <intent-filter>
  7. <action android:name="com.alibaba.sdk.android.push.NOTIFY_ACTION" />
  8. </intent-filter>
  9. </service>
  10. <service android:name="com.alibaba.sdk.android.push.channel.CheckService"
  11. android:process=":channel">
  12. <intent-filter>
  13. <action android:name="com.alibaba.sdk.android.push.CHECK_SERVICE" />
  14. </intent-filter>
  15. </service>
  16. <service android:name="com.taobao.accs.ChannelService"
  17. android:exported="true" android:process=":channel">
  18. <intent-filter>
  19. <action android:name="com.taobao.accs.intent.action.SERVICE"/>
  20. </intent-filter>
  21. </service>
  22. <service
  23. android:name="com.taobao.accs.ChannelService$KernelService"
  24. android:exported="false"
  25. android:process=":channel" >
  26. </service>
  27. <service android:name="com.taobao.accs.data.MsgDistributeService"
  28. android:exported="true">
  29. <intent-filter>
  30. <action android:name="com.taobao.accs.intent.action.RECEIVE" />
  31. </intent-filter>
  32. </service>
  33. <receiver android:name="com.taobao.accs.EventReceiver"
  34. android:process=":channel">
  35. <intent-filter>
  36. <action android:name="android.intent.action.BOOT_COMPLETED"/>
  37. </intent-filter>
  38. <intent-filter>
  39. <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
  40. </intent-filter>
  41. <intent-filter>
  42. <action android:name="android.intent.action.PACKAGE_REMOVED"/>
  43. <data android:scheme="package"/>
  44. </intent-filter>
  45. <intent-filter>
  46. <action android:name="android.intent.action.USER_PRESENT"/>
  47. </intent-filter>
  48. </receiver>
  49. <receiver android:name="com.taobao.accs.ServiceReceiver"
  50. android:process=":channel">
  51. <intent-filter>
  52. <action android:name="com.taobao.accs.intent.action.COMMAND"/>
  53. </intent-filter>
  54. <intent-filter>
  55. <action android:name="com.taobao.accs.intent.action.START_FROM_AGOO"/>
  56. </intent-filter>
  57. </receiver>
  58. <service android:name="org.android.agoo.accs.AgooService"
  59. android:exported="true" >
  60. <intent-filter>
  61. <action android:name="com.taobao.accs.intent.action.RECEIVE" />
  62. </intent-filter>
  63. </service>
  64. <service android:name="com.alibaba.sdk.android.push.AliyunPushIntentService"
  65. android:exported="true"
  66. >
  67. <intent-filter>
  68. <action android:name="org.agoo.android.intent.action.RECEIVE" />
  69. </intent-filter>
  70. </service>
  71. <receiver
  72. android:name="com.taobao.agoo.AgooCommondReceiver"
  73. android:process=":channel"
  74. android:exported="true" >
  75. <intent-filter>
  76. <action android:name="${applicationId}.intent.action.COMMAND" />
  77. </intent-filter>
  78. <intent-filter>
  79. <action android:name="android.intent.action.PACKAGE_REMOVED" />
  80. <data android:scheme="package" />
  81. </intent-filter>
  82. </receiver>
  83. <service
  84. android:name="com.alibaba.sdk.android.push.channel.TaobaoRecvService"
  85. android:exported="true"
  86. android:process=":channel">
  87. <intent-filter>
  88. <action android:name="org.android.agoo.client.MessageReceiverService" />
  89. </intent-filter>
  90. </service>
  91. <!-- V3.0.12及以上版本需配置 -->
  92. <service
  93. android:name="com.taobao.accs.internal.AccsJobService"
  94. android:permission="android.permission.BIND_JOB_SERVICE"
  95. android:process=":channel"/>
  96. <!-- V3.0.7及以上版本需配置 -->
  97. <service android:name="com.alibaba.sdk.android.push.channel.KeepChannelService"
  98. android:permission="android.permission.BIND_JOB_SERVICE"
  99. android:process=":channel" />
  100. <receiver android:name="com.alibaba.sdk.android.push.SystemEventReceiver"
  101. android:process=":channel">
  102. <intent-filter>
  103. <action android:name="android.intent.action.MEDIA_MOUNTED"/>
  104. <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
  105. <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  106. </intent-filter>
  107. </receiver>
  108. <!-- V3.0.9及以上版本需配置 -->
  109. <activity
  110. android:name="com.alibaba.sdk.android.push.keeplive.PushExtActivity"
  111. android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
  112. android:excludeFromRecents="true"
  113. android:exported="false"
  114. android:finishOnTaskLaunch="false"
  115. android:launchMode="singleInstance"
  116. android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
  117. android:process=":channel"
  118. >
  119. </activity>

3.4 消息接收Receiver配置

创建消息接收Receiver,继承自com.alibaba.sdk.android.push.MessageReceiver,并在对应回调中添加业务处理逻辑,可参考以下代码:

  1. public class MyMessageReceiver extends MessageReceiver {
  2. // 消息接收部分的LOG_TAG
  3. public static final String REC_TAG = "receiver";
  4. @Override
  5. public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
  6. // TODO 处理推送通知
  7. Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
  8. }
  9. @Override
  10. public void onMessage(Context context, CPushMessage cPushMessage) {
  11. Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
  12. }
  13. @Override
  14. public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
  15. Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
  16. }
  17. @Override
  18. protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
  19. Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
  20. }
  21. @Override
  22. protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
  23. Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
  24. }
  25. @Override
  26. protected void onNotificationRemoved(Context context, String messageId) {
  27. Log.e("MyMessageReceiver", "onNotificationRemoved");
  28. }
  29. }

将该receiver添加到AndroidManifest.xml中

  1. <!-- 消息接收监听器 (用户可自主扩展) -->
  2. <receiver
  3. android:name=".MyMessageReceiver"
  4. android:exported="false"> <!-- 为保证receiver安全,建议设置不可导出,如需对其他应用开放可通过android:permission进行限制 -->
  5. <intent-filter>
  6. <action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
  7. </intent-filter>
  8. <intent-filter>
  9. <action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
  10. </intent-filter>
  11. <intent-filter>
  12. <action android:name="com.alibaba.sdk.android.push.RECEIVE" />
  13. </intent-filter>
  14. </receiver>
如果是从V2.3.7及以下版本升级到V3.0.0及以上版本的用户,需将<action android:name="org.agoo.android.intent.action.RECEIVE" />改为<action android:name="com.alibaba.sdk.android.push.RECEIVE" />,否则会接收不到推送。

3.5. Proguard配置

  1. -keepclasseswithmembernames class ** {
  2. native <methods>;
  3. }
  4. -keepattributes Signature
  5. -keep class sun.misc.Unsafe { *; }
  6. -keep class com.taobao.** {*;}
  7. -keep class com.alibaba.** {*;}
  8. -keep class com.alipay.** {*;}
  9. -keep class com.ut.** {*;}
  10. -keep class com.ta.** {*;}
  11. -keep class anet.**{*;}
  12. -keep class anetwork.**{*;}
  13. -keep class org.android.spdy.**{*;}
  14. -keep class org.android.agoo.**{*;}
  15. -keep class android.os.**{*;}
  16. -dontwarn com.taobao.**
  17. -dontwarn com.alibaba.**
  18. -dontwarn com.alipay.**
  19. -dontwarn anet.**
  20. -dontwarn org.android.spdy.**
  21. -dontwarn org.android.agoo.**
  22. -dontwarn anetwork.**
  23. -dontwarn com.ut.**
  24. -dontwarn com.ta.**

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

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

【注意】:

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

启动正常确认方法:

  • 回调方法callback.onSuccess()被调用。以上文接入代码为例,logcat将会打印以下日志:
  1. 11-24 12:55:51.096 15235-15535/com.alibaba.xxxx D/YourApp init cloudchannel success
  • 确认cloudchannel初始化正常,在logcat日志中:输入awcn关键字:
  1. 11-24 12:53:51.036 15235-15556/com.alibaba.xxxx E/awcn |[seq:AWCN1_1] AUTH httpStatusCode: 200
  2. 11-24 12:53:51.036 15235-15556/com.alibaba.xxxx E/awcn |[seq:AWCN1_1] status:AUTH_SUCC
本文导读目录
本文导读目录
以上内容是否对您有帮助?