1.接入sdk(因为作者本人是接入的sdk)
在Project根目录build.gradle项目文件中进以下配置:
a.在repositories{}代码段中配置maven仓库地址。
allprojects {
repositories {
jcenter()
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
// 配置HMS Core SDK的Maven仓地址。
maven {
url 'https://developer.huawei.com/repo/'
}
}
}
b.在android{}代码段中配置应用包名和NDK。
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //包名
......
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'x86'
}
......
}
......
}
错误处理:如果在添加以上abFilter配置后Android Studio出现以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
则在Project根目录的gradle.properties文件中添加:
android.useDeprecatedNdk=true
c.在dependencies{}代码段中添加SDK依赖。
implementation 'com.aliyun.ams:alicloud-android-push:3.x.x'
implementation 'com.aliyun.ams:alicloud-android-push:3.2.5'
//辅助通道基础依赖
implementation 'com.aliyun.ams:alicloud-android-third-push:3.2.5'
//华为依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-huawei:3.2.5'
//小米依赖
implementation 'com.aliyun.ams:alicloud-android-third-push-xiaomi:3.2.5'
//公共依赖库
implementation 'com.aliyun.ams:alicloud-android-utdid:2.5.1-proguard'
implementation 'com.aliyun.ams:alicloud-android-utils:1.1.6.4'
implementation 'com.aliyun.ams:alicloud-android-ut:5.4.4'
implementation 'com.aliyun.ams:alicloud-android-beacon:1.0.4.3'
2.AndroidManifest配置
在AndroidManifest文件中设置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>
<!-- 阿里云推送相关权限 -->
<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.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.REORDER_TASKS"/>
<!-- 接收推送主要权限 -->
4.消息接收Receiver配置
创建消息接收Receiver,继承自com.alibaba.sdk.android.push.MessageReceiver,并在对应回调中添加业务处理逻辑,可参考以下代码:
public class MyMessageReceiver extends MessageReceiver {
// 消息接收部分的LOG_TAG
public static final String REC_TAG = "receiver";
@Override
public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
// TODO 处理推送通知
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>
5.SDK初始化配置
首先通过PushServiceFactory获取到CloudPushService,然后调用register()初始化并注册推送通道,并确保在Application上下文中进行初始化工作。
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);
}
});
}
}
6.Android 8.0以上收不到通知解决:
参考以下代码,在客户端创建自己的NotificationChannel。具体调用位置为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);
}
7.辅助通道集成
a.小米:上方sdk添加依赖处已集成,只需把下方代码写到Application
// 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
MiPushRegister.register(applicationContext, "小米AppID", "小米AppKey");
b.华为:需要现在清单文件里进行配置
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="appid=xxxxxx" />
将以下代码加入你application.onCreate()方法中初始化通道。
// 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。
HuaWeiRegister.register(application);
8.用户收到离线通知后,当点击通知跳珠页面时,需要服务端进行配置跳转的页面
客户端只需在该界面写如下代码:
//离线推送接受的消息进行处理
(new PopupNotifyClick(new PopupNotifyClickListener() {
public void onSysNoticeOpened(String title, String summary, Map<String, String> extMap) {
setSlide(false);
Log.d("tag", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extMap);
}
})).onCreate(this, this.getIntent());