阿里云移动推送快速入门
在Project根目录下build.gradle文件中配置maven库URL:
buildscript { repositories { jcenter() // 阿里云推送 maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases/' } } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' // 阿里云推送 // 添加emas-services插件 classpath 'com.aliyun.ams:emas-services:1.0.1' } } allprojects { repositories { jcenter() maven { url "https://jitpack.io" } // 阿里云推送 maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases/' } } }
在对应的module下的build.gradle文件中添加对应依赖
apply plugin: 'com.android.application' // 阿里云推送 apply plugin: 'com.aliyun.ams.emas-services' defaultConfig { applicationId "com.**.**" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0.0" multiDexEnabled true // 阿里云推送 ndk { //选择要添加的对应cpu类型的.so库。 abiFilters 'armeabi', 'x86' } } // 阿里云推送 compile 'com.aliyun.ams:alicloud-android-push:3.1.4' compile 'com.aliyun.ams:alicloud-android-third-push:3.0.6@aar' compile('com.google.firebase:firebase-messaging:9.6.1')
控制台下载 aliyun-emas-services.json文件,并将json文件放到应用根目录下(/app/)
修改应用目录下json文件(<project>/<app-module>/ aliyun-emas-services.json),将不需要使用的产品service对应的status状态置为0( status=0表示对应不使用对应产品,status=1表示使用对应产品 )
Application中onCreate()方法中初始化
/** * 初始化阿里云推送通道 */ private void initPushService() { PushServiceFactory.init(this); CloudPushService pushService = PushServiceFactory.getCloudPushService(); pushService.register(this, new CommonCallback() { @Override public void onSuccess(String response) { Logger.t("BaseAppPush").i("init cloud channel success"); } @Override public void onFailed(String errorCode, String errorMessage) { Logger.t("BaseAppPush").i("init cloud channel -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage); } }); // 华为系统推送 HuaWeiRegister.register(this); // 小米系统推送 MiPushRegister.register(this, "XIAOMI_ID", "XIAOMI_KEY"); // OPPO系统推送 OppoRegister.register(this, "appKey", "appSecret"); }
配置Receiver
/** * 功能:阿里云推送Receiver */ public class AliPushReceiver extends MessageReceiver { // 消息接收部分的LOG_TAG public static final String REC_TAG = "AliPushReceiver"; /** * 收到通知 * <p> * 只对正在对话的界面进行“通知清理”处理,其他的操作有RabbitMQ处理 */ @Override public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) { Logger.t(REC_TAG).e("Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap); ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1); boolean isKidney = tasks.get(0).topActivity.getPackageName().contains(context.getPackageName()); if (isKidney) { clearAllNotification(context); } } /** * 点击通知栏通知 */ @Override public void onNotificationOpened(Context context, String title, String summary, String extraMap) { clearAllNotification(context); Logger.t(REC_TAG).e("onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap); } /** * 通知栏Removed */ @Override protected void onNotificationRemoved(Context context, String messageId) { Logger.t(REC_TAG).e("onNotificationRemoved, messageId: " + messageId); } private void clearAllNotification(Context context) { PushServiceFactory.init(context.getApplicationContext()); CloudPushService pushService = PushServiceFactory.getCloudPushService(); pushService.clearNotifications(); } }
将该receiver添加到AndroidManifest.xml中
添加application属性 <application android:allowBackup="false" tools:replace="android:allowBackup"> <!--开始配置阿里云推送--> <receiver android:name=".base.AliPushReceiver" android:exported="false"> <!-- 为保证receiver安全,建议设置不可导出,如需对其他应用开放可通过android:permission进行限制 --> <intent-filter> <action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED"/> </intent-filter> <intent-filter> <action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED"/> </intent-filter> <intent-filter> <action android:name="com.alibaba.sdk.android.push.RECEIVE"/> </intent-filter> </receiver> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> <action android:name="com.google.android.c2dm.intent.REGISTRATION"/> <category android:name="com.taobao.taobao"/> </intent-filter> </receiver> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false"/> <service android:name="com.alibaba.sdk.android.push.AgooFirebaseInstanceIDService" android:exported="true"> <intent-filter android:priority="-500"> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> <service android:name="com.alibaba.sdk.android.push.AgooFirebaseMessagingService" android:exported="true"> <intent-filter android:priority="-500"> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <!--结束配置阿里云推送-->
混淆设置Proguard
#阿里云推送 -keepclasseswithmembernames class ** { native <methods>; } -keepattributes Signature -keep class sun.misc.Unsafe { *; } -keep class com.taobao.** {*;} -keep class com.alibaba.** {*;} -keep class com.alipay.** {*;} -keep class com.ut.** {*;} -keep class com.ta.** {*;} -keep class anet.**{*;} -keep class anetwork.**{*;} -keep class org.android.spdy.**{*;} -keep class org.android.agoo.**{*;} -keep class android.os.**{*;} -dontwarn com.taobao.** -dontwarn com.alibaba.** -dontwarn com.alipay.** -dontwarn anet.** -dontwarn org.android.spdy.** -dontwarn org.android.agoo.** -dontwarn anetwork.** -dontwarn com.ut.** -dontwarn com.ta.** # 小米通道 -keep class com.xiaomi.** {*;} -dontwarn com.xiaomi.** # 华为通道 -keep class com.huawei.** {*;} -dontwarn com.huawei.** # GCM/FCM通道 -keep class com.google.firebase.**{*;} -dontwarn com.google.firebase.** # OPPO通道 -keep public class * extends android.app.Service注意:如果是针对单个用户推送,必须用到DeviceID,登录成功后上传DeviceID;退出登录时解绑该DeviceID;
PushServiceFactory.init(this); CloudPushService pushService = PushServiceFactory.getCloudPushService(); String deviceId=pushService.getDeviceId();