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
4c58e575
Commit
4c58e575
authored
Aug 29, 2025
by
zhangchengbo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:自定义[通知栏]短信消息推送
parent
86fd9d8d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
14 deletions
+64
-14
SmsReceiver.kt
...src/main/kotlin/com/secspace/sms/receivers/SmsReceiver.kt
+64
-14
No files found.
app/src/main/kotlin/com/secspace/sms/receivers/SmsReceiver.kt
View file @
4c58e575
package
com.secspace.sms.receivers
package
com.secspace.sms.receivers
import
android.annotation.SuppressLint
import
android.annotation.SuppressLint
import
android.app.Notification
import
android.app.NotificationChannel
import
android.app.NotificationManager
import
android.content.BroadcastReceiver
import
android.content.BroadcastReceiver
import
android.content.Context
import
android.content.Context
import
android.content.Intent
import
android.content.Intent
...
@@ -8,9 +11,13 @@ import android.database.Cursor
...
@@ -8,9 +11,13 @@ import android.database.Cursor
import
android.os.Handler
import
android.os.Handler
import
android.os.Looper
import
android.os.Looper
import
android.provider.Telephony
import
android.provider.Telephony
import
androidx.core.app.NotificationCompat
import
com.secspace.log.Log
import
com.secspace.log.Log
import
com.secspace.sms.App
import
com.secspace.sms.R
import
com.secspace.sms.extensions.*
import
com.secspace.sms.extensions.*
import
com.secspace.sms.helpers.Config
import
com.secspace.sms.helpers.Config
import
com.secspace.sms.helpers.Constant
import
com.secspace.sms.helpers.RecentsHelper
import
com.secspace.sms.helpers.RecentsHelper
import
com.secspace.sms.helpers.refreshMessages
import
com.secspace.sms.helpers.refreshMessages
import
com.secspace.sms.models.Conversation
import
com.secspace.sms.models.Conversation
...
@@ -21,19 +28,21 @@ import com.secspace.sms.util.AES
...
@@ -21,19 +28,21 @@ import com.secspace.sms.util.AES
import
com.secspace.sms.util.PhoneFromUtil
import
com.secspace.sms.util.PhoneFromUtil
import
com.secspace.sms.util.PhoneUtils
import
com.secspace.sms.util.PhoneUtils
import
com.secspace.sms.util.SmsCountUtil
import
com.secspace.sms.util.SmsCountUtil
import
com.simplemobiletools.commons.extensions.baseConfig
import
com.simplemobiletools.commons.extensions.*
import
com.simplemobiletools.commons.extensions.getMyContactsCursor
import
com.simplemobiletools.commons.extensions.hasPermission
import
com.simplemobiletools.commons.extensions.isNumberBlocked
import
com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import
com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import
com.simplemobiletools.commons.helpers.SimpleContactsHelper
import
com.simplemobiletools.commons.helpers.SimpleContactsHelper
import
com.simplemobiletools.commons.helpers.ensureBackgroundThread
import
com.simplemobiletools.commons.helpers.ensureBackgroundThread
import
com.simplemobiletools.commons.helpers.isOreoPlus
import
com.simplemobiletools.commons.models.PhoneNumber
import
com.simplemobiletools.commons.models.PhoneNumber
import
com.simplemobiletools.commons.models.SimpleContact
import
com.simplemobiletools.commons.models.SimpleContact
import
com.simplemobiletools.commons.util.GsonUtil
class
SmsReceiver
:
BroadcastReceiver
()
{
class
SmsReceiver
:
BroadcastReceiver
()
{
private
val
TAG
:
String
=
"SmsReceiver"
private
val
TAG
:
String
=
"SmsReceiver"
private
val
mContent
=
App
.
APP_CONTEXT
private
var
CALL_NOTIFICATION_ID
=
1
override
fun
onReceive
(
context
:
Context
,
intent
:
Intent
)
{
override
fun
onReceive
(
context
:
Context
,
intent
:
Intent
)
{
val
messages
=
Telephony
.
Sms
.
Intents
.
getMessagesFromIntent
(
intent
)
val
messages
=
Telephony
.
Sms
.
Intents
.
getMessagesFromIntent
(
intent
)
...
@@ -56,10 +65,11 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -56,10 +65,11 @@ class SmsReceiver : BroadcastReceiver() {
body
+=
it
.
messageBody
body
+=
it
.
messageBody
date
=
System
.
currentTimeMillis
()
date
=
System
.
currentTimeMillis
()
threadId
=
context
.
getThreadId
(
address
)
threadId
=
context
.
getThreadId
(
address
)
// android.util.Log.d(TAG, "onReceive: address = $address body = $body date = $date")
}
}
if
(
address
.
isNotEmpty
())
{
if
(
address
.
isNotEmpty
())
{
findConversationWithPhoneNumber
(
context
,
address
,
threadId
)
{
findConversationWithPhoneNumber
(
context
,
address
,
body
,
threadId
)
{
if
(
context
.
baseConfig
.
blockUnknownNumbers
)
{
if
(
context
.
baseConfig
.
blockUnknownNumbers
)
{
val
simpleContactsHelper
=
SimpleContactsHelper
(
context
)
val
simpleContactsHelper
=
SimpleContactsHelper
(
context
)
simpleContactsHelper
.
exists
(
address
,
privateCursor
)
{
exists
->
simpleContactsHelper
.
exists
(
address
,
privateCursor
)
{
exists
->
...
@@ -74,6 +84,8 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -74,6 +84,8 @@ class SmsReceiver : BroadcastReceiver() {
badgeTotalNumber
(
context
)
badgeTotalNumber
(
context
)
}
}
}
}
// android.util.Log.d(TAG, "onReceive: 短信手机号 = $it 短信内容 = $body")
sendNotification
(
it
,
body
)
}
}
}
}
}
}
...
@@ -84,20 +96,23 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -84,20 +96,23 @@ class SmsReceiver : BroadcastReceiver() {
* 1.先从短信DB库查找
* 1.先从短信DB库查找
* 2.未找到再从 通话记录中查找
* 2.未找到再从 通话记录中查找
*/
*/
private
fun
findConversationWithPhoneNumber
(
context
:
Context
,
phoneNumber
:
String
,
threadId
:
Long
,
confirmCallBack
:
(
String
)
->
Unit
)
{
private
fun
findConversationWithPhoneNumber
(
context
:
Context
,
phoneNumber
:
String
,
body
:
String
,
threadId
:
Long
,
confirmCallBack
:
(
String
)
->
Unit
)
{
var
lastNumber
=
""
var
lastNumber
=
""
// Log.d(TAG, "----SmsReceiver---- phoneNumber:${phoneNumber} threadId:
$threadId")
Log
.
d
(
TAG
,
"----SmsReceiver---- phoneNumber:${phoneNumber} body = $body threadId =
$threadId"
)
val
hasPerMissionResult
=
context
.
hasPermission
(
PERMISSION_READ_CALL_LOG
)
val
hasPerMissionResult
=
context
.
hasPermission
(
PERMISSION_READ_CALL_LOG
)
if
(
hasPerMissionResult
)
{
if
(
hasPerMissionResult
)
{
val
allConversations
=
context
.
conversationsDB
.
getNonArchived
()
as
ArrayList
<
Conversation
>
// Log.d(TAG, "----SmsReceiver----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
//1
//1
allConversations
.
forEach
{
val
allConversations
=
context
.
conversationsDB
.
getNonArchived
()
as
ArrayList
<
Conversation
>
if
(
it
.
title
.
trim
()
==
phoneNumber
&&
it
.
isSFNumber
)
{
Log
.
d
(
TAG
,
"----SmsReceiver----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}"
)
lastNumber
=
it
.
phoneNumber
if
(
allConversations
.
isNotEmpty
()){
return
@forEach
val
listConversation
=
allConversations
.
filter
{
Log
.
d
(
TAG
,
"----SmsReceiver---- 短信内容相等 = ${it.threadId == threadId && it.isSFNumber }"
)
it
.
threadId
==
threadId
&&
it
.
isSFNumber
}
android
.
util
.
Log
.
d
(
TAG
,
"findConversationWithPhoneNumber listConversation = $listConversation"
)
if
(
listConversation
.
isNotEmpty
())
{
lastNumber
=
listConversation
.
first
().
phoneNumber
}
}
}
}
...
@@ -284,6 +299,41 @@ class SmsReceiver : BroadcastReceiver() {
...
@@ -284,6 +299,41 @@ class SmsReceiver : BroadcastReceiver() {
Log
.
d
(
TAG
,
"badgeTotalNumber readTotalResult = $totalResult "
)
Log
.
d
(
TAG
,
"badgeTotalNumber readTotalResult = $totalResult "
)
}
}
/**
* 发送通知栏消息推送
*/
private
fun
sendNotification
(
maskPhoneResult
:
String
,
body
:
String
)
{
val
notificationManager
=
mContent
.
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
(
body
)
.
setSound
(
null
)
.
setAutoCancel
(
true
)
.
setChannelId
(
channelId
)
val
buildNotification
=
builder
.
build
()
notificationManager
.
notify
(
CALL_NOTIFICATION_ID
,
buildNotification
)
}
}
}
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