Commit 4c58e575 by zhangchengbo

fix:自定义[通知栏]短信消息推送

parent 86fd9d8d
package com.secspace.sms.receivers
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
......@@ -8,9 +11,13 @@ import android.database.Cursor
import android.os.Handler
import android.os.Looper
import android.provider.Telephony
import androidx.core.app.NotificationCompat
import com.secspace.log.Log
import com.secspace.sms.App
import com.secspace.sms.R
import com.secspace.sms.extensions.*
import com.secspace.sms.helpers.Config
import com.secspace.sms.helpers.Constant
import com.secspace.sms.helpers.RecentsHelper
import com.secspace.sms.helpers.refreshMessages
import com.secspace.sms.models.Conversation
......@@ -21,19 +28,21 @@ import com.secspace.sms.util.AES
import com.secspace.sms.util.PhoneFromUtil
import com.secspace.sms.util.PhoneUtils
import com.secspace.sms.util.SmsCountUtil
import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.getMyContactsCursor
import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.extensions.isNumberBlocked
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.models.PhoneNumber
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.util.GsonUtil
class SmsReceiver : BroadcastReceiver() {
private val TAG: String = "SmsReceiver"
private val mContent = App.APP_CONTEXT
private var CALL_NOTIFICATION_ID = 1
override fun onReceive(context: Context, intent: Intent) {
val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
......@@ -56,10 +65,11 @@ class SmsReceiver : BroadcastReceiver() {
body += it.messageBody
date = System.currentTimeMillis()
threadId = context.getThreadId(address)
// android.util.Log.d(TAG, "onReceive: address = $address body = $body date = $date")
}
if (address.isNotEmpty()) {
findConversationWithPhoneNumber(context, address, threadId) {
findConversationWithPhoneNumber(context, address,body,threadId) {
if (context.baseConfig.blockUnknownNumbers) {
val simpleContactsHelper = SimpleContactsHelper(context)
simpleContactsHelper.exists(address, privateCursor) { exists ->
......@@ -74,6 +84,8 @@ class SmsReceiver : BroadcastReceiver() {
badgeTotalNumber(context)
}
}
// android.util.Log.d(TAG, "onReceive: 短信手机号 = $it 短信内容 = $body")
sendNotification(it, body)
}
}
}
......@@ -84,20 +96,23 @@ class SmsReceiver : BroadcastReceiver() {
* 1.先从短信DB库查找
* 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 = ""
// 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)
if (hasPerMissionResult) {
val allConversations = context.conversationsDB.getNonArchived() as ArrayList<Conversation>
// Log.d(TAG, "----SmsReceiver----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
//1
allConversations.forEach {
if (it.title.trim() == phoneNumber && it.isSFNumber) {
lastNumber = it.phoneNumber
return@forEach
val allConversations = context.conversationsDB.getNonArchived() as ArrayList<Conversation>
Log.d(TAG, "----SmsReceiver----handleMessage allConversations:${GsonUtil.parseListToJson(allConversations)}")
if(allConversations.isNotEmpty()){
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() {
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)
}
}
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