Android 14 快速适配要点
zhuanlan.zhihu.com/p/628752923
Android 13 适配指南
Android 12 快速适配要点
zhuanlan.zhihu.com/p/440582410
PendingIntent 兼容性问题:
val pendingIntent = PendingIntent.getBroadcast(
Utils.getApp(),
0,
intentClick,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_UPDATE_CURRENT
)
如果单独用 FLAG_IMMUTABLE ,后面intent的值 怎么都不会变
Notification 设置了 setSilent(true) 后 优先级 IMPORTANCE_HIGH 就失效了,通知会没有下拉显示
object PayNotification {
private var notificationManager: NotificationManager? = null
//const val channelId = "22"
/**
* 显示通知栏
*/
@Synchronized
fun showNotification(context: Context, resp: SocketResponse,isSilent:Boolean = true) {
LogUtils.e("showNotification ------ resp : $resp")
val title = resp.payload?.title?:""
var content = resp.payload?.body?:""
var headUrl = ""
val targetId = resp.payload?.orderItemId?:""
//前台
if (AppUtils.isAppForeground()) {
val ac = ActivityUtils.getTopActivity()
if (ac is OrderDetailActivity) {//处于 同个订单,只提示语音
if (Objects.equals(targetId, ac.getOrderId())) {
initNotification(context, title, targetId, resp, isImportance = false,isSilent = isSilent)
return
}
}
}
initNotification(context, title, targetId, resp,isImportance = true, isSilent = isSilent)
}
@SuppressLint("LaunchActivityFromNotification")
private fun initNotification(
context: Context,
title: String,
targetId: String,
resp: SocketResponse,
isImportance: Boolean = true,
isSilent:Boolean = true
) {
LogUtils.e("initNotification ------ targetId $targetId resp : $resp")
val chatPartner = Person.Builder()
.setName(title)
.setKey(targetId)
.setImportant(isImportance)
.setIcon(
IconCompat.createFromIcon(
Icon.createWithResource(
Utils.getApp(),
R.drawable.login_icon
)
)
)
.build()
// 通知行为(点击后能进入应用界面)
val intentClick = initClickIntent(resp)
val pendingIntent = PendingIntent.getBroadcast(
Utils.getApp(),
0,
intentClick,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_UPDATE_CURRENT
)
//通知栏移除
/* val intent =
Intent("com.ym.chat.broadcast.NOTIFICATION_REMOVE").putExtra("targetId", targetId)
val removeIntent = PendingIntent.getBroadcast(
Utils.getApp(),
0,
intent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_IMMUTABLE else PendingIntent.FLAG_UPDATE_CURRENT
)*/
//添加消息
val msgStyle = NotificationCompat.MessagingStyle(chatPartner)
.setGroupConversation(true)
.addMessage(resp.payload?.body, System.currentTimeMillis(), chatPartner)
val channelId = "${System.currentTimeMillis()}"
val notification = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(msgStyle)
.setContentTitle(title)
.setContentText(resp.payload?.body)
//.setDeleteIntent(removeIntent)
.setCategory(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setPriority(if(isImportance) PRIORITY_MAX else PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
//.setSilent(isSilent)
.build()
show(targetId.hashCode(), notification, isImportance,channelId)
}
/**
* 点击通知栏监听
*/
private fun initClickIntent(resp: SocketResponse): Intent {
val intent = Intent(Utils.getApp(), PayNotificationClickReceiver::class.java)
intent.putExtra(OrderDetailActivity.ORDER_ID, resp.payload?.orderItemId)
intent.putExtra (OrderDetailActivity.IS_SELLER, Objects.equals(AppCache.getUserInfoEntity()?.id, resp.payload?.sellerId))
return intent
}
@Synchronized
private fun show(id: Int, notification: Notification, isImportance: Boolean, channelId: String) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
initNotificationManager(isImportance,channelId)
if (Build.BRAND.lowercase() == "xiaomi") {
//BadgeUtils.setCount(0, Utils.getApp(), notification)
notificationManager?.notify(id, notification)
} else {
notificationManager?.notify(id, notification)
//BadgeUtils.setCount(0, Utils.getApp(), notification)
}
}
@Synchronized
private fun initNotificationManager(isImportance: Boolean, channelId: String) {
synchronized(this) {
if(notificationManager == null){
notificationManager = Utils.getApp().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = Utils.getApp().getString(R.string.app_name)
val descriptionText = Utils.getApp().getString(R.string.app_name)
val importance = if (isImportance) IMPORTANCE_HIGH else IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, name, importance).apply {
description = descriptionText
}
channel.setShowBadge(false)
// Register the channel with the system
notificationManager?.createNotificationChannel(channel)
}
}
}
/**
* 根据targetId清空通知栏
*/
fun clearNotification(targetId: String) {
//移除通知栏消息数据
//ImCache.notifycationMsg[targetId]?.clear()
notificationManager?.cancel(targetId!!.hashCode())
}
}
Android 11 快速适配要点
zhuanlan.zhihu.com/p/275758740
cloud.tencent.com/developer/a…
Android 10 快速适配要点
cloud.tencent.com/developer/a…