Commit 790fdab4 by zhangchengbo

fix:取消通知栏-自定义消息推送,采用系统自带消息推送。(解决推送消息重复展示2条)

parent f41ba949
...@@ -3,6 +3,7 @@ package com.secspace.sms.services ...@@ -3,6 +3,7 @@ package com.secspace.sms.services
import android.app.Notification import android.app.Notification
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.content.Context
import android.os.Build import android.os.Build
import android.service.notification.NotificationListenerService import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification import android.service.notification.StatusBarNotification
...@@ -28,74 +29,70 @@ class SmsNotificationListener : NotificationListenerService() { ...@@ -28,74 +29,70 @@ class SmsNotificationListener : NotificationListenerService() {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
override fun onNotificationPosted(sbn: StatusBarNotification) { override fun onNotificationPosted(sbn: StatusBarNotification) {
super.onNotificationPosted(sbn) super.onNotificationPosted(sbn)
if(isShowNotification) return /* if(isShowNotification) return
// 可见的通知中存在系统发出的关于顶层包名的通知时,取消显示 // 可见的通知中存在系统发出的关于顶层包名的通知时,取消显示
val notification: Notification? = sbn.notification val notification: Notification? = sbn.notification
// if (notification?.publicVersion == null) return // publicVersion 获取为null 1.有可能该通知没有公共版本 2.浏览器不支持或实现有限 3.未满足权限或场景要求 // if (notification?.publicVersion == null) return // publicVersion 获取为null 1.有可能该通知没有公共版本 2.浏览器不支持或实现有限 3.未满足权限或场景要求
try {
// 检查通知是否来自短信应用 val packageName = sbn.packageName
try { Log.d(TAG, "SmsNotificationListener 短信通知:packageName=$packageName")
val packageName = sbn.packageName if (isSmsPackage(packageName)) {
Log.d(TAG, "SmsNotificationListener 短信通知:packageName=$packageName") // 获取通知内容
if (isSmsPackage(packageName)) { val title = getTitle(notification)
//移出系统通知栏消息 val content = getContent(notification)
// cancelNotification(sbn.key)
// 获取通知内容 Log.d(TAG, "短信通知:标题=$title, 内容=$content")
val title = getTitle(notification) maskPhoneResult = title
val content = getContent(notification) if (PhoneFromUtil.isNumeric(title)) {
if (content.contains("\u200B")) {
Log.d(TAG, "短信通知:标题=$title, 内容=$content") if (title.contains("+86")) {
maskPhoneResult = title title.replace("+86", "")
if (PhoneFromUtil.isNumeric(title)) { }
if (content.contains("\u200B")) { PhoneUtils.phoneNumberFormat(title) { itNumber ->
if (title.contains("+86")) { maskPhoneResult = itNumber
title.replace("+86", "") }
} Log.e(TAG, "maskPhoneResult:$maskPhoneResult")
PhoneUtils.phoneNumberFormat(title) { itNumber -> }
maskPhoneResult = itNumber }
}
Log.e(TAG, "maskPhoneResult:$maskPhoneResult") Log.d(TAG, "短信通知:脱敏后标题=$maskPhoneResult, 内容=$content")
}
} val notificationManager = notificationManager
val isHighPriority = mContent.powerManager.isInteractive
Log.d(TAG, "短信通知:脱敏后标题=$maskPhoneResult, 内容=$content") val channelId = if (isHighPriority) Constant.HIGH_PRIORITY else Constant.LOW_PRIORITY
val notificationManager = notificationManager if (isOreoPlus()) {
val isHighPriority = mContent.powerManager.isInteractive val importance = NotificationManager.IMPORTANCE_DEFAULT
val channelId = if (isHighPriority) Constant.HIGH_PRIORITY else Constant.LOW_PRIORITY val name = if (isHighPriority) Constant.HIGH_CHANNEL_PRIORITY else Constant.LOW_CHANNEL_PRIORITY
if (isOreoPlus()) { NotificationChannel(channelId, name, importance).apply {
val importance = NotificationManager.IMPORTANCE_DEFAULT setSound(null, null)
val name = if (isHighPriority) Constant.HIGH_CHANNEL_PRIORITY else Constant.LOW_CHANNEL_PRIORITY notificationManager.createNotificationChannel(this)
}
NotificationChannel(channelId, name, importance).apply { }
setSound(null, null)
notificationManager.createNotificationChannel(this) val builder = NotificationCompat.Builder(mContent, channelId)
} .setSmallIcon(R.mipmap.ic_launcher)
} .setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(Notification.CATEGORY_MESSAGE)
val builder = NotificationCompat.Builder(mContent, channelId) .setDefaults(Notification.DEFAULT_SOUND)
.setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(maskPhoneResult)
.setPriority(NotificationCompat.PRIORITY_LOW) .setContentText(content)
.setCategory(Notification.CATEGORY_MESSAGE) .setSound(null)
.setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true)
.setContentTitle(maskPhoneResult) .setChannelId(channelId)
.setContentText(content)
.setSound(null) val buildNotification = builder.build()
.setAutoCancel(true) notificationManager.notify(CALL_NOTIFICATION_ID, buildNotification)
.setChannelId(channelId)
Log.d(TAG, "短信通知:notificationManager 发送推送消息")
val buildNotification = builder.build() isShowNotification = true
notificationManager.notify(CALL_NOTIFICATION_ID, buildNotification)
}
Log.d(TAG, "短信通知:notificationManager 发送推送消息")
isShowNotification = true
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "NotificationListener e: ${e.localizedMessage}") Log.e(TAG, "NotificationListener e: ${e.localizedMessage}")
cancelAllNotifications() cancelAllNotifications()
} }*/
} }
override fun onNotificationRemoved(sbn: StatusBarNotification?) { override fun onNotificationRemoved(sbn: StatusBarNotification?) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment