Android 阿里云推送集成指南

一、注册阿里云开发者账号,创建应用获取Appkey以及AppSecrent

二、在AndroidMainifest添加权限,还有一些其他权限(基本上你自己的项目已经写了,我就不贴了)









android:allowBackup="true"在Application加上这个属性。

三、在build.gradle中引入阿里云推送库(根据官方文档的引入就一个的会有问题,亲测这种是最保险的)

implementation 'com.aliyun.ams:alicloud-android-push:3.1.4@aar'
implementation 'com.aliyun.ams:alicloud-android-utils:1.1.3'
implementation 'com.aliyun.ams:alicloud-android-beacon:1.0.1'
implementation 'com.aliyun.ams:alicloud-android-ut:5.4.0'

设置ndk支持so包,abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'其实国内手机99%以上都是arm的,

就支持arm就可以了,这样也能减小apk体积,因为so的体积确实不小,每个项目都适用。

在gradle.properties加入android.useDeprecatedNdk=true

四、在AndroidMainifest中加入以下,阿里云的配置

 
 



 
 
 


 
 
 


 
 
 




 
 
 


 
 
 
 
 
 
 
 
 
 
 
 
 


 
 
 
 
 
 


 
 
 


 
 
 


 
 
 
 
 
 
 


 
 
 






 
 
 
 
 





 
 
 
 
 
 
 
 
 
 

五、在Application中初始化阿里云推送

/**
 * 初始化云推送通道
 * @param applicationContext
 */
private void initCloudChannel(Context applicationContext) {
    createNotificationChannel();
 PushServiceFactory.init(applicationContext);
 CloudPushService pushService = PushServiceFactory.getCloudPushService();
 pushService.register(applicationContext, new CommonCallback() {
        @Override
 public void onSuccess(String response) {
            String deviceId = PushServiceFactory.getCloudPushService().getDeviceId();
 Log.e(TAG,deviceId);
 Log.d(TAG, "init cloudchannel success");
 }
        @Override
 public void onFailed(String errorCode, String errorMessage) {
            Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
 }
    });

}
private void createNotificationChannel(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 // 通知渠道的id
 String id = "1";//这个是与后台约定好的,要不收不到,该方法主要是适配Android 8.0以上,避免接收不到通知
 // 用户可以看到的通知渠道的名字.
 CharSequence name = "氢氪管家";
 // 用户可以看到的通知渠道的描述
 String 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);
 }
}

六、自定义MyMessageReceiver

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 extraMap) {//当通知准确到达用户的时候触发
        ToastManage.s(context,title+"推送");
 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());
 ToastManage.s(context,"消息:"+cPushMessage.getTitle());
 }
    @Override
 public void onNotificationOpened(Context context, String title, String summary, String extraMap) {//当通知展开的时候触发的操作
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
 ToastManage.s(context,"打开了通知:"+title);
 }
    @Override
 protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {//当通知被点击了触发的操作,并且没有配置跳转路径
        ToastManage.s(context,"onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
 Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
 }
    @Override
 protected void onNotificationReceivedInApp(Context context, String title, String summary, Map extraMap, int openType, String openActivity, String openUrl) {//当用户使用时接收到通知
        ToastManage.s(context,"onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + 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) {//通知被移除
        ToastManage.s(context,"通知被移除了");
 Log.e("MyMessageReceiver", "onNotificationRemoved");
 }
}

你可能感兴趣的:(android,学习资源)