Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
Simple-Sms
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhangchengbo
Simple-Sms
Commits
790fdab4
Commit
790fdab4
authored
Aug 27, 2025
by
zhangchengbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:取消通知栏-自定义消息推送,采用系统自带消息推送。(解决推送消息重复展示2条)
parent
f41ba949
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
65 deletions
+62
-65
SmsNotificationListener.kt
...tlin/com/secspace/sms/services/SmsNotificationListener.kt
+62
-65
No files found.
app/src/main/kotlin/com/secspace/sms/services/SmsNotificationListener.kt
View file @
790fdab4
...
...
@@ -3,6 +3,7 @@ package com.secspace.sms.services
import
android.app.Notification
import
android.app.NotificationChannel
import
android.app.NotificationManager
import
android.content.Context
import
android.os.Build
import
android.service.notification.NotificationListenerService
import
android.service.notification.StatusBarNotification
...
...
@@ -28,74 +29,70 @@ class SmsNotificationListener : NotificationListenerService() {
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
override
fun
onNotificationPosted
(
sbn
:
StatusBarNotification
)
{
super
.
onNotificationPosted
(
sbn
)
if
(
isShowNotification
)
return
// 可见的通知中存在系统发出的关于顶层包名的通知时,取消显示
val
notification
:
Notification
?
=
sbn
.
notification
// if (notification?.publicVersion == null) return // publicVersion 获取为null 1.有可能该通知没有公共版本 2.浏览器不支持或实现有限 3.未满足权限或场景要求
// 检查通知是否来自短信应用
try
{
val
packageName
=
sbn
.
packageName
Log
.
d
(
TAG
,
"SmsNotificationListener 短信通知:packageName=$packageName"
)
if
(
isSmsPackage
(
packageName
))
{
//移出系统通知栏消息
// cancelNotification(sbn.key)
// 获取通知内容
val
title
=
getTitle
(
notification
)
val
content
=
getContent
(
notification
)
Log
.
d
(
TAG
,
"短信通知:标题=$title, 内容=$content"
)
maskPhoneResult
=
title
if
(
PhoneFromUtil
.
isNumeric
(
title
))
{
if
(
content
.
contains
(
"\u200B"
))
{
if
(
title
.
contains
(
"+86"
))
{
title
.
replace
(
"+86"
,
""
)
}
PhoneUtils
.
phoneNumberFormat
(
title
)
{
itNumber
->
maskPhoneResult
=
itNumber
}
Log
.
e
(
TAG
,
"maskPhoneResult:$maskPhoneResult"
)
}
}
Log
.
d
(
TAG
,
"短信通知:脱敏后标题=$maskPhoneResult, 内容=$content"
)
val
notificationManager
=
notificationManager
val
isHighPriority
=
mContent
.
powerManager
.
isInteractive
val
channelId
=
if
(
isHighPriority
)
Constant
.
HIGH_PRIORITY
else
Constant
.
LOW_PRIORITY
if
(
isOreoPlus
())
{
val
importance
=
NotificationManager
.
IMPORTANCE_DEFAULT
val
name
=
if
(
isHighPriority
)
Constant
.
HIGH_CHANNEL_PRIORITY
else
Constant
.
LOW_CHANNEL_PRIORITY
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
)
.
setDefaults
(
Notification
.
DEFAULT_SOUND
)
.
setContentTitle
(
maskPhoneResult
)
.
setContentText
(
content
)
.
setSound
(
null
)
.
setAutoCancel
(
true
)
.
setChannelId
(
channelId
)
val
buildNotification
=
builder
.
build
()
notificationManager
.
notify
(
CALL_NOTIFICATION_ID
,
buildNotification
)
Log
.
d
(
TAG
,
"短信通知:notificationManager 发送推送消息"
)
isShowNotification
=
true
}
/* if(isShowNotification) return
// 可见的通知中存在系统发出的关于顶层包名的通知时,取消显示
val notification: Notification? = sbn.notification
// if (notification?.publicVersion == null) return // publicVersion 获取为null 1.有可能该通知没有公共版本 2.浏览器不支持或实现有限 3.未满足权限或场景要求
try {
val packageName = sbn.packageName
Log.d(TAG, "SmsNotificationListener 短信通知:packageName=$packageName")
if (isSmsPackage(packageName)) {
// 获取通知内容
val title = getTitle(notification)
val content = getContent(notification)
Log.d(TAG, "短信通知:标题=$title, 内容=$content")
maskPhoneResult = title
if (PhoneFromUtil.isNumeric(title)) {
if (content.contains("\u200B")) {
if (title.contains("+86")) {
title.replace("+86", "")
}
PhoneUtils.phoneNumberFormat(title) { itNumber ->
maskPhoneResult = itNumber
}
Log.e(TAG, "maskPhoneResult:$maskPhoneResult")
}
}
Log.d(TAG, "短信通知:脱敏后标题=$maskPhoneResult, 内容=$content")
val notificationManager = notificationManager
val isHighPriority = mContent.powerManager.isInteractive
val channelId = if (isHighPriority) Constant.HIGH_PRIORITY else Constant.LOW_PRIORITY
if (isOreoPlus()) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val name = if (isHighPriority) Constant.HIGH_CHANNEL_PRIORITY else Constant.LOW_CHANNEL_PRIORITY
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)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentTitle(maskPhoneResult)
.setContentText(content)
.setSound(null)
.setAutoCancel(true)
.setChannelId(channelId)
val buildNotification = builder.build()
notificationManager.notify(CALL_NOTIFICATION_ID, buildNotification)
Log.d(TAG, "短信通知:notificationManager 发送推送消息")
isShowNotification = true
}
} catch (e: Exception) {
Log.e(TAG, "NotificationListener e: ${e.localizedMessage}")
cancelAllNotifications()
}
}
*/
}
override
fun
onNotificationRemoved
(
sbn
:
StatusBarNotification
?)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment