Fixed triggering of missed call notifications

This commit is contained in:
Juha Heinanen
2026-07-14 11:09:57 +03:00
parent 606874c070
commit 6a21627afe
2 changed files with 61 additions and 39 deletions
@@ -1406,44 +1406,8 @@ class BaresipService: Service() {
} }
ua.account.missedCalls = ua.account.missedCalls || missed ua.account.missedCalls = ua.account.missedCalls || missed
} }
if (missed) { if (missed)
val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT showMissedCallNotification(uap, call.peerUri, aor)
val caller = Utils.friendlyUri(this, call.peerUri, ua.account)
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "call missed").putExtra("uap", uap)
val pi = PendingIntent.getActivity(
this,
CALL_REQ_CODE,
intent,
piFlags
)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
nb.setSmallIcon(R.drawable.ic_notification_call_missed)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setContentIntent(pi)
.setCategory(Notification.CATEGORY_CALL)
.setAutoCancel(true)
var missedCalls = 0
for (notification in nm.activeNotifications)
if (notification.id == CALL_MISSED_NOTIFICATION_ID)
missedCalls++
if (missedCalls == 0) {
nb.setContentTitle(getString(R.string.missed_call_from))
nb.setContentText(caller)
}
else {
nb.setContentTitle(getString(R.string.missed_calls))
nb.setContentText(
String.format(
getString(R.string.missed_calls_count),
missedCalls + 1
)
)
}
nm.notify(CALL_MISSED_NOTIFICATION_ID, nb.build())
}
if (!Utils.isVisible()) if (!Utils.isVisible())
return return
} }
@@ -1607,6 +1571,14 @@ class BaresipService: Service() {
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
.addPerson(sender) .addPerson(sender)
var unreadMessages = 0
for (notification in nm.activeNotifications)
if (notification.id == MESSAGE_NOTIFICATION_ID) {
unreadMessages = notification.notification.number
break
}
nb.setNumber(unreadMessages + 1)
val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY) val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel(getString(R.string.reply)) .setLabel(getString(R.string.reply))
.build() .build()
@@ -1855,6 +1827,7 @@ class BaresipService: Service() {
) )
lowChannel.description = "Background status notifications" lowChannel.description = "Background status notifications"
lowChannel.enableVibration(false) lowChannel.enableVibration(false)
lowChannel.setShowBadge(false)
lowChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC lowChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
nm.createNotificationChannel(lowChannel) nm.createNotificationChannel(lowChannel)
val ringAttributes = AudioAttributes.Builder() val ringAttributes = AudioAttributes.Builder()
@@ -1869,6 +1842,7 @@ class BaresipService: Service() {
highChannel.description = "Incoming calls and important alerts" highChannel.description = "Incoming calls and important alerts"
highChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC highChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
highChannel.enableVibration(true) highChannel.enableVibration(true)
highChannel.setShowBadge(true)
highChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, ringAttributes) highChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, ringAttributes)
nm.createNotificationChannel(highChannel) nm.createNotificationChannel(highChannel)
val mediumChannel = NotificationChannel( val mediumChannel = NotificationChannel(
@@ -1879,6 +1853,7 @@ class BaresipService: Service() {
mediumChannel.description = "Incoming messages" mediumChannel.description = "Incoming messages"
mediumChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC mediumChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
mediumChannel.enableVibration(false) mediumChannel.enableVibration(false)
mediumChannel.setShowBadge(true)
mediumChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, ringAttributes) mediumChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, ringAttributes)
nm.createNotificationChannel(mediumChannel) nm.createNotificationChannel(mediumChannel)
} }
@@ -2273,8 +2248,10 @@ class BaresipService: Service() {
history.startTime = call.startTime history.startTime = call.startTime
history.rejected = call.rejected history.rejected = call.rejected
history.add() history.add()
if (call.dir == "in" && call.startTime == null && !call.rejected) if (call.dir == "in" && call.startTime == null && !call.rejected) {
call.ua.account.missedCalls = true call.ua.account.missedCalls = true
showMissedCallNotification(uap, call.peerUri, call.ua.account.aor)
}
} }
synchronized(calls) { synchronized(calls) {
calls.remove(call) calls.remove(call)
@@ -3090,6 +3067,50 @@ class BaresipService: Service() {
isServiceClean = true isServiceClean = true
} }
private fun showMissedCallNotification(uap: Long, peerUri: String, aor: String) {
val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
val caller = Utils.friendlyUri(this, peerUri, Account.ofAor(aor)!!)
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra("action", "call missed").putExtra("uap", uap)
val pi = PendingIntent.getActivity(
this,
CALL_REQ_CODE,
intent,
piFlags
)
val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID)
nb.setSmallIcon(R.drawable.ic_notification_call_missed)
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
.setContentIntent(pi)
.setCategory(NotificationCompat.CATEGORY_MISSED_CALL)
.setAutoCancel(true)
var missedCalls = 0
for (notification in nm.activeNotifications)
if (notification.id == CALL_MISSED_NOTIFICATION_ID) {
missedCalls = notification.notification.number
break
}
if (missedCalls == 0) {
nb.setContentTitle(getString(R.string.missed_call_from))
nb.setContentText(caller)
nb.setNumber(1)
} else {
nb.setContentTitle(getString(R.string.missed_calls))
nb.setContentText(
String.format(
getString(R.string.missed_calls_count),
missedCalls + 1
)
)
nb.setNumber(missedCalls + 1)
}
nm.notify(CALL_MISSED_NOTIFICATION_ID, nb.build())
}
private external fun baresipStart( private external fun baresipStart(
path: String, path: String,
addresses: String, addresses: String,
@@ -356,6 +356,7 @@ class MainActivity : ComponentActivity() {
override fun onNewIntent(intent: Intent) { override fun onNewIntent(intent: Intent) {
// Called when MainActivity already exists at the top of current task // Called when MainActivity already exists at the top of current task
super.onNewIntent(intent) super.onNewIntent(intent)
setIntent(intent)
this.setShowWhenLocked(true) this.setShowWhenLocked(true)
this.setTurnScreenOn(true) this.setTurnScreenOn(true)