阿里云移动推送学习笔记

文章目录

  • 阿里云移动推送学习笔记
    • 第一步:项目build.gradle文件配置
    • 第二步:Module中build.gradle文件配置
    • 第三步:build工程,然后到阿里云官网控制台添加应用,然后记录appKey 和appSecret
    • 第四步:AndroidManifest文件配置
    • 第五步:初始化阿里云推送
    • 第六步:自定义AliyunMessageIntentService服务
    • 附:通知设置工具类
    • 参考
第一步:项目build.gradle文件配置
buildscript {repositories {...... maven {url 'http://maven.aliyun.com/nexus/content/repositories/releases/'}}......
}allprojects {repositories {......maven {url 'http://maven.aliyun.com/nexus/content/repositories/releases/'}}
}
第二步:Module中build.gradle文件配置
defaultConfig {applicationId "com.quanyou.daggertest"......ndk {//选择要添加的对应cpu类型的.so库。abiFilters "armeabi", "armeabi-v7a", "x86", "mips"}
}dependencies {......//aliyun推送implementation 'com.aliyun.ams:alicloud-android-push:3.1.4@aar'implementation 'com.aliyun.ams:alicloud-android-utdid:1.1.5.3'implementation 'com.aliyun.ams:alicloud-android-ut:5.4.0'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-third-push:3.0.3@aar'
}
第三步:build工程,然后到阿里云官网控制台添加应用,然后记录appKey 和appSecret

阿里云官网地址:https://homenew.console.aliyun.com/

第四步:AndroidManifest文件配置
<!-- 阿里云推送相关权限 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<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.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS" />
<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" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><applicationandroid:name=".base.MyApplication"......><meta-data android:name="com.alibaba.app.appkey" android:value="*****"/> <!-- 请填写你自己的- appKey --><meta-data android:name="com.alibaba.app.appsecret" android:value="****"/> <!-- 请填写你自己的appSecret -->......
</application>
第五步:初始化阿里云推送

在自定义Application中声明如下方法并在onCreate方法中调用

fun initCloudChannel(applicationContext: Application) {PushServiceFactory.init(applicationContext)val pushService = PushServiceFactory.getCloudPushService()pushService.register(applicationContext, object : CommonCallback {override fun onSuccess(response: String) {//获取设备号val deviceId = pushService.deviceIdLogUtils.e("MyApplication", "init cloudchannel success   " + deviceId)}override fun onFailed(errorCode: String, errorMessage: String) {val packageName = applicationContext.getApplicationInfo().packageNameLogUtils.e("MyApplication", "当前packageName:$packageName -- init cloudchannel failed -- errorcode:$errorCode -- errorMessage:$errorMessage")}})//android 8.0支持if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {val mNotificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?// 通知渠道的idval id = "1"// 用户可以看到的通知渠道的名字.val name = "notification channel"// 用户可以看到的通知渠道的描述val description = "notification description"val importance = NotificationManager.IMPORTANCE_HIGHval mChannel = NotificationChannel(id, name, importance)// 配置通知渠道的属性mChannel.description = description// 设置通知出现时的闪灯(如果 android 设备支持的话)mChannel.enableLights(true)mChannel.lightColor = Color.RED// 设置通知出现时的震动(如果 android 设备支持的话)mChannel.enableVibration(true)mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)//最后在notificationmanager中创建该通知渠道mNotificationManager!!.createNotificationChannel(mChannel)}/*辅助通道设置*/// 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。MiPushRegister.register(applicationContext, "小米AppID", "小米AppKey");// 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。HuaWeiRegister.register(applicationContext);//GCM/FCM辅助通道注册GcmRegister.register(applicationContext, "sendId", applicationContext.applicationInfo.packageName); //sendId/applicationId为步骤获得的参数// OPPO通道注册OppoRegister.register(applicationContext, "appKey", "appSecret"); // appKey/appSecret在OPPO通道开发者平台获取
}
第六步:自定义AliyunMessageIntentService服务

自定义AliyunMessageIntentService服务用于接收通知

/*** Created by liyazhou on 17/8/22.* 为避免推送广播被系统拦截的小概率事件,我们推荐用户通过IntentService处理消息互调,接入步骤:* 1. 创建IntentService并继承AliyunMessageIntentService* 2. 覆写相关方法,并在Manifest的注册该Service* 3. 调用接口CloudPushService.setPushIntentService* 详细用户可参考:https://help.aliyun.com/document_detail/30066.html#h2-2-messagereceiver-aliyunmessageintentservice*/class MyMessageIntentService : AliyunMessageIntentService() {/*** 推送通知的回调方法*/override fun onNotification(context: Context, title: String, summary: String, extraMap: Map<String, String>) {LogUtils.e(REC_TAG, "收到一条推送通知 : $title, summary:$summary")}/*** 推送消息的回调方法*/override fun onMessage(context: Context, cPushMessage: CPushMessage) {LogUtils.e(REC_TAG, "收到一条推送消息 : " + cPushMessage.title + ", content:" + cPushMessage.content)}/*** 从通知栏打开通知的扩展处理*/override fun onNotificationOpened(context: Context, title: String, summary: String, extraMap: String) {LogUtils.e(REC_TAG, "onNotificationOpened :  : $title : $summary : $extraMap")}/*** 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened*/override fun onNotificationClickedWithNoAction(context: Context, title: String, summary: String, extraMap: String) {LogUtils.e(REC_TAG, "onNotificationClickedWithNoAction :  : $title : $summary : $extraMap")}/*** 通知删除回调*/override fun onNotificationRemoved(context: Context, messageId: String) {LogUtils.e(REC_TAG, "onNotificationRemoved : $messageId")}/*** 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html#h3-3-4-basiccustompushnotification-api*/override fun onNotificationReceivedInApp(context: Context, title: String, summary: String, extraMap: Map<String, String>, openType: Int, openActivity: String, openUrl: String) {LogUtils.e(REC_TAG, "onNotificationReceivedInApp :  : $title : $summary  $extraMap : $openType : $openActivity : $openUrl")}companion object {private val TAG = "MyMessageIntentService"}
}

,并在AndroidManifest文件中注册

<serviceandroid:name=".service.MyMessageIntentService"android:exported="false"><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>
</service>
附:通知设置工具类
object SettingNoticeUtils {/*** 获取推送服务*/fun getCloudPushService(): CloudPushService {return PushServiceFactory.getCloudPushService()}private fun getIdentifier(context: Context, resType: String, resName: String): Int {val identifier = context.resources.getIdentifier(resName, resType, context.packageName)return identifier}private fun getRawIdentifier(context: Context, resName: String): Int {val identifier = getIdentifier(context, "raw", resName)return identifier}private fun getDrawableIdentifier(context: Context, resName: String): Int {val identifier = getIdentifier(context, "drawable", resName)return identifier}private fun getRawPath(context: Context, identifier: Int): String {val packageName = context.packageNameval path = "android.resource://$packageName/$identifier"return path}fun drawable2Bitmap(drawable: Drawable): Bitmap? {return when (drawable) {is BitmapDrawable -> drawable.bitmapis NinePatchDrawable -> {val bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),if (drawable.getOpacity() != PixelFormat.OPAQUE)Bitmap.Config.ARGB_8888elseBitmap.Config.RGB_565)val canvas = Canvas(bitmap)drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight())drawable.draw(canvas)bitmap}else -> null}}/*** 设置通知声音*/fun setNotifSound(path: String) {getCloudPushService().setNotificationSoundFilePath(path)}fun setNotifSound(context: Context, resName: String) {val identifier = getRawIdentifier(context, resName)if (0 != identifier) {val path = getRawPath(context, identifier)getCloudPushService().setNotificationSoundFilePath(path)} else {LogUtils.e("SettingNoticeUtils", "未发现资源: $resName")}}/*** 设置通知图片(大图标)*/fun setNotifIcon(bitmap: Bitmap) {getCloudPushService().setNotificationLargeIcon(bitmap)}fun setNotifIcon(context: Context, @DrawableRes drawableResId: Int) {val bitmap = BitmapFactory.decodeResource(context.resources, drawableResId)if (null != bitmap) {getCloudPushService().setNotificationLargeIcon(bitmap)} else {LogUtils.e("SettingNoticeUtils", "图片资源不存在: $drawableResId")}}fun setNotifIcon(drawable: Drawable) {val bitmap = drawable2Bitmap(drawable)if (null != bitmap) {getCloudPushService().setNotificationLargeIcon(bitmap)} else {LogUtils.e("SettingNoticeUtils", "图片资源不存在")}}fun setNotifIcon(context: Context, resName: String) {val identifier = getDrawableIdentifier(context, resName)if (0 != identifier) {val drawable = context.resources.getDrawable(identifier)val bitmap = drawable2Bitmap(drawable)if (null != drawable && null != bitmap) {getCloudPushService().setNotificationLargeIcon(bitmap)} else {LogUtils.e("SettingNoticeUtils", "图片资源不存在: $resName")}} else {LogUtils.e("SettingNoticeUtils", "图片资源不存在: $resName")}}/*** 自定义状态栏通知图标(小图标)*/fun setNotifSmallIcon(context: Context, resName: String) {val identifier = getDrawableIdentifier(context, resName)if (0 != identifier) {getCloudPushService().setNotificationSmallIcon(identifier)} else {LogUtils.e("SettingNoticeUtils", "图片资源不存在: $resName")}}/***  设置基础自定义样式通知示例*  @param remindType 设置提醒方式(无提示,震动,声音,震动和声音),传参如下值:*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_SILENT]*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_VIBRATE]*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_SOUND]*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND]*/fun setBasicCusNotif(drawableResId: Int,remindType: Int,notifiId: Int,isBuildWhenAppInForeground: Boolean,isServerOptionFirst: Boolean,callback: Callback?) {val notification = BasicCustomPushNotification()//设置状态栏图标notification.statusBarDrawable = drawableResId//设置提醒方式为声音notification.remindType = remindType//设置当推送到达时如果应用处于前台不创建通知notification.isBuildWhenAppInForeground = isBuildWhenAppInForeground//设置服务端配置优先notification.isServerOptionFirst = isServerOptionFirst//注册该通知,并设置IDval isSuccess = CustomNotificationBuilder.getInstance().setCustomNotification(notifiId, notification)callback?.isSuccess(isSuccess)}fun setBasicCusNotif(drawableResId: Int,remindType: Int,notifiId: Int,isBuildWhenAppInForeground: Boolean,isServerOptionFirst: Boolean) {setBasicCusNotif(drawableResId, remindType, notifiId, isBuildWhenAppInForeground, isServerOptionFirst, null)}/***  设置高级自定义样式通知示例*  @param remindType 设置提醒方式(无提示,震动,声音,震动和声音),传参如下值:*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_SILENT]*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_VIBRATE]*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_SOUND]*  @sample [com.alibaba.sdk.android.push.notification.BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND]*/fun setAdvCusNotf(@LayoutRes layoutResId: Int,@DrawableRes iconResId: Int,@IdRes titleIdRes: Int,@IdRes contentIdRes: Int,isBuildWhenAppInForeground: Boolean,isServerOptionFirst: Boolean,remindType: Int,notifiId: Int,callback: Callback?) {//创建高级自定义样式通知,设置布局文件以及对应的控件IDval notification = AdvancedCustomPushNotification(layoutResId, iconResId, titleIdRes, contentIdRes)//设置服务端配置优先notification.isServerOptionFirst = isServerOptionFirst//设置提醒方式为声音notification.remindType = remindType//设置当推送到达时如果应用处于前台不创建通知notification.isBuildWhenAppInForeground = isBuildWhenAppInForegroundval isSuccess = CustomNotificationBuilder.getInstance().setCustomNotification(notifiId, notification)callback?.isSuccess(isSuccess)}fun setAdvCusNotf(@LayoutRes layoutResId: Int,@DrawableRes iconResId: Int,@IdRes titleIdRes: Int,@IdRes contentIdRes: Int,isBuildWhenAppInForeground: Boolean,isServerOptionFirst: Boolean,remindType: Int,notifiId: Int) {SettingNoticeUtils.setAdvCusNotf(layoutResId, iconResId, titleIdRes, contentIdRes, isBuildWhenAppInForeground, isServerOptionFirst, remindType, notifiId)}/*** 设置接收消息IntentService示例* 1. 设置后,所有推送相关互调全部通过对应IntentService透出*/fun setIntentService(clazz: Class<AliyunMessageIntentService>) {getCloudPushService().setPushIntentService(clazz)}/*** 设置接收消息BroadcastReceiver示例* 1. 系统默认通过广播方式发送给对应BroadcastReceiver* 2. 如果希望从IntentService改回BroadcastReceiver可参考该方法*/fun setBroadcastReceiver() {getCloudPushService().setPushIntentService(null)}interface Callback {fun isSuccess(isSuccess: Boolean)}
}
参考

阿里云移动推送开发文档 https://help.aliyun.com/product/30047.html?spm=a2c4g.750001.list.119.6b9e7b13hCCqDp

阿里云移动推送学习笔记相关推荐

  1. 解决.NET Core中MailKit无法使用阿里云邮件推送服务的问题

    在博问中(.net core怎么实现邮件发送)知道了MailKit无法使用阿里云邮件推送服务发送邮件的问题,自已实测也遇到同样的问题,而用自己搭建的邮件服务器没这个问题. 于是,向阿里云提交了工单.. ...

  2. 基于阿里云移动推送的移动应用推送模式最佳实践

    摘要: ### 一.概念 以下概念对应系统设计时的语义,对于如何合理使用移动推送有借鉴意义 #### 1.1 设备 安装并使用开发者移动应用的装置 #### 1.2 设备ID 阿里云移动推送为设备分配 ...

  3. 使用阿里云邮件推送服务架设自己邮件验证与推送体系

    提示:阅读本文需提前了解的相关知识 1.电子邮件协议(http://baike.baidu.com/view/2367542.htm) 2.阿里云邮件推送(https://www.aliyun.com ...

  4. 阿里云-邮件推送 配置 购买域名 配置域名

    阿里云-邮件推送 配置 购买域名 配置域名 1.邮件推送是什么: 2.为什么要用它 3.如何使用? 3.1 第一步:创建发信域名 第二步. 创建发信地址 第三步. 再创建一个模板,这个需要审核. 最后 ...

  5. Java实现邮箱发送(阿里云邮箱推送)

    Java mail邮箱发送 1. 邮箱信息实体类 2. jar包依赖 3. 发送邮箱实现 绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥- 阿里云邮件推送控 ...

  6. 友盟推送和阿里云移动推送使用注意事项、不同点比较

    友盟推送和阿里云移动推送,都属于阿里系的产品,在使用上很大一部分还是很相似的,阿里云将移动推送.移动热修复.移动测试.移动数据分析和移动用户反馈集成到了一块,所有的远程依赖也都是集成在一起了,然而友盟 ...

  7. YII2.0使用阿里云邮件推送实现邮件发送

    从YII中文网小马哥发布的教程http://www.yiichina.com/tutorial/320获取到163邮箱的配置方法 1.在配置文件main-local.php components=&g ...

  8. java项目——发邮件之阿里云邮箱推送服务(一)

    最近做项目的时候用到了阿里云的邮箱推送.首先站在我自己的理解的角度,为什么要用阿里云邮箱推送服务? 首先从万网说起. 中国万网是中国最大的互联网基础服务提供商之一,服务范围涵盖基础的域名服务.主机服务 ...

  9. 阿里云-邮件推送 java 代码 ,测试邮件推送服务,阿里云邮件推送,java

    阿里云-邮件推送Java 测试代码 1.阿里云-邮件推送 配置 购买域名 配置域名 2.创建 Access Key 2.1登录 Access Key 管理控制台. 2.2单击页面右上角按钮 创建 Ac ...

  10. 阿里云邮件推送,邮件模板加EmailCamel退订链接,加速模板通过审核!

    1. 什么是邮件退订链接? 在给客户发邮件或者群发邮件的时候,在邮件内容的底部加退订链接.客户如果以后不想接收您的邮件,可以退订.退订后,您就不要再给客户发邮件了.使用EmailCamel退订链接,可 ...

最新文章

  1. 裸眼3D将是未来移动端的显示的主要技术
  2. zigbee zstack 串口,按键,消息,定时器
  3. 巨蟒python全栈开发-第10天 函数进阶
  4. echarts的x轴数量固定_联轴器 多节膜片式夹紧螺丝固定型 DAAPC(对应LK5CWP)
  5. 如何使用Python操作MySQL数据库
  6. CTFHUB《Web-信息泄露-备份文件下载》网站源码,
  7. 蓝桥杯 ADV-78 算法提高 最长单词
  8. 找出所有子集的异或总和再求和
  9. 【分布式】Zookeeper序列化及通信协议
  10. 微信小程序斩获世界大奖后,中小企业怎样搭上这辆快速列车?
  11. SEO优化师王永仙说:网页title设置及描述设置和URL优化及网站地图优化(sitemap)...
  12. html5测试网速插件,js 检测客户端网速
  13. jQuery01(达)
  14. 大数据技术之Hive 第8章 函数 系统内置函数
  15. SVD分解和矩阵的Lipschitz条件等
  16. matlab 解函数方程,MATLAB程序设计教程(7)—MATLAB解方程与函数极值
  17. red had第二次学习整理
  18. 台式您想使用系统还原计算机吗,系统还原功能已关闭。如果继续安装,将不能使用系统还原随您的计算机运行的Windows操作系统提供的Windows - Microsoft Community...
  19. matlab中0.1见方,square,square怎么读?
  20. 怎么批量重命名文件夹123456

热门文章

  1. Spring Boot整合mybatis报错Invalid bound statement (not found)
  2. 【文献阅读】深度时空网络风力预测(Jiangyuan Li等人,ArXiv,2021)
  3. 如何手动彻底消除U盘使用痕迹
  4. 深度学习图像分割:U-Net 体系结构
  5. 板簧的弹性系数如何计算_一种板簧总成的刚度计算方法、系统及可读存储介质与流程...
  6. 维纳滤波器、卡尔曼系列滤波器以及自适应LMS、RLS滤波器matlab代码实现
  7. excel转word_excel怎么转word?常用方法合集,看你使用哪一种
  8. 【博弈论】【RQNOJ】取棋子游戏
  9. oracle数据对应函数,Oracle函数取得姓名对应的拼音
  10. 10款在线检查英语语法的网站