Android集成阿里云推送功能
官方集成参考网址:https://help.aliyun.com/document_detail/51056.html?spm=a2c4g.11186623.6.587.64ed7fa8NY9DN8
本文讲解Maven库快速集成,收到集成下载jar包导入方式请移步官方文档。
1.在Project根目录下build.gradle文件中配置maven库URL:
如果工程需要配置多个maven仓库地址,可以向如下格式配置:
allprojects {repositories {flatDir {dirs 'libs' // 支付宝 SDK AAR 包所需的配置}google()jcenter()mavenCentral()maven { url "https://jitpack.io" }maven { url "http://maven.aliyun.com/nexus/content/repositories/releases/" }} }
2.在对应的module下的build.gradle文件中添加对应依赖:
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //包名
......
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'x86'
}
......
}
......
}
dependencies {
......
compile 'com.aliyun.ams:alicloud-android-push:3.1.6@aar'
compile 'com.aliyun.ams:alicloud-android-utils:1.1.3'
compile 'com.aliyun.ams:alicloud-android-beacon:1.0.2'
compile 'com.aliyun.ams:alicloud-android-ut:5.4.0'
// 或
compile 'com.aliyun.ams:alicloud-android-push:3.1.6'
......
}
官方提示:如果在添加以上 abiFilter 配置之后android Studio出现以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
则在 Project 根目录的gradle.properties文件中添加:
android.useDeprecatedNdk=true
3.AndroidManifest配置
3.1 Permission配置
<!-- 阿里云推送相关权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3.2 appKey, appSecret配置
<application android:name="*****">
<meta-data android:name="com.alibaba.app.appkey" android:value="*****"/> <!-- 请填写你自己的- appKey -->
<meta-data android:name="com.alibaba.app.appsecret" android:value="****"/> <!-- 请填写你自己的appSecret -->
</application>
3.3 其他组件配置
<!-- Push SDK 相关组件,required-->
<!-- 消息接收服务 -->
<service
android:name="com.alibaba.sdk.android.push.MsgService"
android:exported="false">
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.NOTIFY_ACTION" />
</intent-filter>
</service>
<service android:name="com.alibaba.sdk.android.push.channel.CheckService"
android:process=":channel">
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.CHECK_SERVICE" />
</intent-filter>
</service>
<service android:name="com.taobao.accs.ChannelService"
android:exported="true" android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.SERVICE"/>
</intent-filter>
</service>
<service
android:name="com.taobao.accs.ChannelService$KernelService"
android:exported="false"
android:process=":channel" >
</service>
<service android:name="com.taobao.accs.data.MsgDistributeService"
android:exported="true">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.RECEIVE" />
</intent-filter>
</service>
<receiver android:name="com.taobao.accs.EventReceiver"
android:process=":channel">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
<receiver android:name="com.taobao.accs.ServiceReceiver"
android:process=":channel">
<intent-filter>
<action android:name="com.taobao.accs.intent.action.COMMAND"/>
</intent-filter>
<intent-filter>
<action android:name="com.taobao.accs.intent.action.START_FROM_AGOO"/>
</intent-filter>
</receiver>
<service android:name="org.android.agoo.accs.AgooService"
android:exported="true" >
<intent-filter>
<action android:name="com.taobao.accs.intent.action.RECEIVE" />
</intent-filter>
</service>
<service android:name="com.alibaba.sdk.android.push.AliyunPushIntentService"
android:exported="true"
>
<intent-filter>
<action android:name="org.agoo.android.intent.action.RECEIVE" />
</intent-filter>
</service>
<receiver
android:name="com.taobao.agoo.AgooCommondReceiver"
android:process=":channel"
android:exported="true" >
<intent-filter>
<action android:name="${applicationId}.intent.action.COMMAND" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service
android:name="com.alibaba.sdk.android.push.channel.TaobaoRecvService"
android:exported="true"
android:process=":channel">
<intent-filter>
<action android:name="org.android.agoo.client.MessageReceiverService" />
</intent-filter>
</service>
<!-- V3.0.12及以上版本需配置 -->
<service
android:name="com.taobao.accs.internal.AccsJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":channel"/>
<!-- V3.0.7及以上版本需配置 -->
<service android:name="com.alibaba.sdk.android.push.channel.KeepChannelService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":channel" />
<receiver android:name="com.alibaba.sdk.android.push.SystemEventReceiver"
android:process=":channel">
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
<!-- V3.0.9及以上版本需配置 -->
<activity
android:name="com.alibaba.sdk.android.push.keeplive.PushExtActivity"
android:configChanges="keyboardHidden|orientation|screenSize|navigation|keyboard"
android:excludeFromRecents="true"
android:exported="false"
android:finishOnTaskLaunch="false"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:process=":channel"
>
</activity>
3.4消息接收Receiver配置
创建消息接收Receiver,继承自com.alibaba.sdk.android.push.MessageReceiver,并在对应回调中添加业务处理逻辑,可参考以下代码:
public class MyMessageReceiver extends MessageReceiver {
public static final String REC_TAG = "receiver";
//当通知准确到达用户的时候触发
@Override
public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
}
//当消息准确到达用户的时候触发,只有用户在使用的时候才能接收到消息
@Override
public void onMessage(Context context, CPushMessage cPushMessage) {
Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
}
//当通知展开的时候触发的操作
@Override
public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
//当通知被点击了触发的操作,并且没有配置跳转路径
@Override
protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
}
//当用户使用时接收到通知
@Override
protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
}
//通知被移除
@Override
protected void onNotificationRemoved(Context context, String messageId) {
Log.e("MyMessageReceiver", "onNotificationRemoved");
}
}
将该receiver添加到AndroidManifest.xml中
<!-- 消息接收监听器 (用户可自主扩展) -->
<receiver
android:name="自己包名.MyMessageReceiver"
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>
3.5. 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.**{*;}
-keep class org.json.**{*;}
-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.**
3.6. 在应用中注册和启动移动推送
请参照以下代码段进行初始化:
import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
public class MainApplication extends Application {
private static final String TAG = "Init";
@Override
public void onCreate() {
super.onCreate();
initCloudChannel(this);
}
/**
* 初始化云推送通道
* @param applicationContext
*/
private void initCloudChannel(Context applicationContext) {
PushServiceFactory.init(applicationContext);
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.register(applicationContext, new CommonCallback() {
@Override
public void onSuccess(String response) {
Log.d(TAG, "init cloudchannel success");
}
@Override
public void onFailed(String errorCode, String errorMessage) {
Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
}
});
}
}
3.7. Android8.0及以上NotificationChannel机制,Android 8.0 以上设备通知接收不到
添加如下代码:
在Application的onCreate,云推初始化前后都可以
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 通知渠道的id
String id = "1";
// 用户可以看到的通知渠道的名字.
CharSequence name = "notification channel";
// 用户可以看到的通知渠道的描述
String description = "notification description";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// 配置通知渠道的属性
mChannel.setDescription(description);
// 设置通知出现时的闪灯(如果 android 设备支持的话)
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//最后在notificationmanager中创建该通知渠道
mNotificationManager.createNotificationChannel(mChannel);
}
自己的后台同时集成阿里云推送API,还是使用官方网站推送,都要在高级推送设置中添加上推送渠道id,否则android8.0以上版本收不到通知栏的提示。
3.8 推送指定给某一个人接收,可以使用高级推送设置账户id
在MainActivity的onCreate方法中:PushServiceFactory.getCloudPushService().bindAccount(Global.UInfo.getId(), new CommonCallback() {@Overridepublic void onSuccess(String s) {}@Overridepublic void onFailed(String s, String s1) {} });
Android集成阿里云推送功能相关推荐
- Android集成友盟推送功能
友盟是中国最大的移动开发者服务平台,为移动开发者提供免费的应用统计分析.社交分享.消息推送.自动更新.在线参数.移动推广效果分析.微社区等app开发和运营解决方案. 如何快速集成友盟推送功能: 1. ...
- ionic集成阿里云推送
阿里云推送是比较廉价,快速的推送管理平台,目前在ionic上有一个插件可以直接使用它,不需要官网介绍的那些复杂步骤.插件地址:https://github.com/llwwbb/cordova-plu ...
- Android集成阿里消息推送
1.阿里消息推送的官方文档地址 https://help.aliyun.com/document_detail/30054.html?spm=a2c4g.11174283.3.2.136c6d1669 ...
- react-native集成阿里云推送
1.使用组件 react-native-aliyun-push 2.安装及集成 *注意在之前必须要到阿里云注册移动推送服务并建好ios和android的app,里面的appkey和AppSecret到 ...
- Android集成阿里云消息推送的方法步骤
一 创建App应用 1.1 在控制台发(https://mhub.console.aliyun.com)的App列表页,点击页面产品列表中"添加产品"的图标即可创建一个新的产品(产 ...
- Android 集成阿里云移动推送
该博客不为学术讨论,仅是记录,方便以后用到快速集成开发,有兴趣者可以mark一下以备使用.当然也为推送的小白提供方便. 因为公司内部有第三方安全检测机构,所以用某光推送时,因为极光推送原理是会通过其他 ...
- Android之集成友盟推送功能
友盟是中国最大的移动开发者服务平台,为移动开发者提供免费的应用统计分析.社交分享.消息推送.自动更新.在线参数.移动推广效果分析.微社区等app开发和运营解决方案. 如何快速集成友盟推送功能: 1. ...
- Android 集成友盟推送方案(1)
我自己继承过FCM(国外app)百度云推送,阿里云推送,小米推送,华为推送,友盟推送,目前这几类推送方式.本文主讲友盟推送方式的集成步骤: 大家可以根据官网网址进行集成:官网如下: https://d ...
- Android集成阿里云旺即时通讯踩坑历程
下载云旺的demo,将demo中的OneSDK直接拷贝,作为Moudle进行依赖,具体操作就不说了,OneSDK是最新的,一定不要进行修改, 进行依赖后,可能会遇到buildToolsVersion ...
最新文章
- 50. Pow(x, n)
- Linux下tar命令简介
- 点积的那个公式:a dot b =||a||*||b||CosX.
- 【Java】Float计算不准确
- iOS中安全结束 子线程 的方法
- (八)数据结构之“树”
- mongodb 如何删除 字段值为 json对象中的某个字段值
- html鼠标悬停效果_【开发小技巧】023—如何使用HTML和CSS实现3D文字效果
- AutoCAD2007 快捷键介绍和线形设置
- 【VB底层开发经典入门】VB编写植物大战僵尸辅助开发视频教程
- 关于动态路由OSPF原理、报文
- 【沃顿商学院学习笔记】管理学——06腐败带来的间接成本和全球制裁状况Indirect Costs and Sanction
- CSS实现最简洁的四角边框
- 《Linux内核修炼之道》精华分享与讨论(7)——分析内核源码如何入手?(下)
- Revit—视图联动
- 2020年数学建模国赛C题完整代码下载链接处
- INT_MAX INT_MIN及其运算
- iphone怎么设置农历提醒事项
- html视频一直播放代码,通过HTML5调用播放视频的一些注意事项和代码方法
- 如何规避企业财务风险