show a notification at receiving an SMS

This commit is contained in:
tibbi
2020-04-04 21:53:58 +02:00
parent 29c0f928ca
commit 1fe2f10d53
3 changed files with 75 additions and 2 deletions

View File

@ -160,7 +160,7 @@ fun Context.getNameFromPhoneNumber(number: String): Int? {
return null return null
} }
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long) { fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, threadID: Long) {
val uri = Telephony.Sms.CONTENT_URI val uri = Telephony.Sms.CONTENT_URI
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(Telephony.Sms.ADDRESS, address) put(Telephony.Sms.ADDRESS, address)
@ -168,6 +168,7 @@ fun Context.insertNewSMS(address: String, subject: String, body: String, date: L
put(Telephony.Sms.BODY, body) put(Telephony.Sms.BODY, body)
put(Telephony.Sms.DATE, date) put(Telephony.Sms.DATE, date)
put(Telephony.Sms.READ, 0) put(Telephony.Sms.READ, 0)
put(Telephony.Sms.THREAD_ID, threadID)
put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX) put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX)
} }

View File

@ -1,14 +1,31 @@
package com.simplemobiletools.smsmessenger.receivers package com.simplemobiletools.smsmessenger.receivers
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.RingtoneManager
import android.provider.Telephony import android.provider.Telephony
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.extensions.insertNewSMS import com.simplemobiletools.smsmessenger.extensions.insertNewSMS
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
import com.simplemobiletools.smsmessenger.helpers.THREAD_NUMBER
import com.simplemobiletools.smsmessenger.models.Events import com.simplemobiletools.smsmessenger.models.Events
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
class SmsReceiver : BroadcastReceiver() { class SmsReceiver : BroadcastReceiver() {
@SuppressLint("NewApi")
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)
messages.forEach { messages.forEach {
@ -16,9 +33,61 @@ class SmsReceiver : BroadcastReceiver() {
val subject = it.pseudoSubject val subject = it.pseudoSubject
val body = it.messageBody val body = it.messageBody
val date = it.timestampMillis val date = it.timestampMillis
context.insertNewSMS(address, subject, body, date) val threadID = if (isMarshmallowPlus()) {
Telephony.Threads.getOrCreateThreadId(context, address)
} else {
0
}
context.insertNewSMS(address, subject, body, date, threadID)
showNotification(context, address, body, threadID.toInt())
} }
EventBus.getDefault().post(Events.RefreshMessages()) EventBus.getDefault().post(Events.RefreshMessages())
} }
@SuppressLint("NewApi")
private fun showNotification(context: Context, address: String, body: String, threadID: Int) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val channelId = "simple_sms_messenger"
if (isOreoPlus()) {
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
.build()
val name = context.getString(R.string.channel_received_sms)
val importance = NotificationManager.IMPORTANCE_HIGH
NotificationChannel(channelId, name, importance).apply {
setBypassDnd(false)
enableLights(true)
setSound(soundUri, audioAttributes)
notificationManager.createNotificationChannel(this)
}
}
val intent = Intent(context, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, threadID)
putExtra(THREAD_NAME, address)
putExtra(THREAD_NUMBER, address)
}
val pendingIntent = PendingIntent.getActivity(context, threadID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val builder = NotificationCompat.Builder(context, channelId)
.setContentTitle(address)
.setContentText(body)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setCategory(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setSound(soundUri, AudioManager.STREAM_NOTIFICATION)
.setChannelId(channelId)
notificationManager.notify(threadID, builder.build())
}
} }

View File

@ -3,4 +3,7 @@
<string name="app_launcher_name">SMS Messenger</string> <string name="app_launcher_name">SMS Messenger</string>
<string name="type_a_message">Type a message…</string> <string name="type_a_message">Type a message…</string>
<string name="message_not_sent">Message has not been sent.</string> <string name="message_not_sent">Message has not been sent.</string>
<!-- Notifications -->
<string name="channel_received_sms">Received SMS</string>
</resources> </resources>