Skip to content

Commit

Permalink
notif: Handle RemoveFcmMessage.
Browse files Browse the repository at this point in the history
This implementation has some problems.

* Group Notification doesn't get cancelled when its children are.
  It will appear as an empty notification if all notification
  within a group are cancelled and will be required to be manually
  removed.
* This will only happen in case of multiple groups, I have managed
  to cancel all notification if there are only group notifications
  remaining as active notifications.
* It would be nice to have more information in notification
  payload to remove notification, the implementation currently
  performs a linear search for every message to be removed.
  Consider using a better approach.
  • Loading branch information
AkashDhiman committed Aug 19, 2021
1 parent a91fdf4 commit 9de656f
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.service.notification.StatusBarNotification
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.NotificationCompat
Expand Down Expand Up @@ -71,7 +72,32 @@ internal fun onReceived(context: Context, mapData: Map<String, String>) {

if (fcmMessage is MessageFcmMessage) {
updateNotification(context, fcmMessage)
} // TODO handle case for RemoveFcmMessage
} else if (fcmMessage is RemoveFcmMessage) {
removeNotification(context, fcmMessage)
}
}

private fun removeNotification(context: Context, fcmMessage: RemoveFcmMessage) {
for (fcmMessageId in fcmMessage.messageIds) {
val activeNotifications = getActiveNotification(context) ?: return
for (statusBarNotification in activeNotifications) {
val notification = statusBarNotification.notification
val messageId = notification.extras.getInt("zulipMessageId")
if (fcmMessageId == messageId) {
NotificationManagerCompat.from(context).cancel(statusBarNotification.tag, statusBarNotification.id)
break
}
}
}
// TODO figure out a way to cancel individual group notifications
// misbehaves with multiple realm, shows empty notification.
// This cancels all group Notification when no other notification
// is present.
if (Build.VERSION.SDK_INT >= 24) {
if (getActiveNotification(context)?.all { statusBarNotification -> statusBarNotification.isGroup } == true) {
NotificationManagerCompat.from(context).cancelAll()
}
}
}

private fun createViewPendingIntent(fcmMessage: MessageFcmMessage, context: Context): PendingIntent {
Expand Down Expand Up @@ -119,6 +145,11 @@ private fun getActiveNotification(context: Context, conversationKey: String):
return Pair(null, null)
}


private fun getActiveNotification(context: Context): Array<StatusBarNotification>? =
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?)?.activeNotifications

// TODO: Add a Text saying n messages in m conversations. (this will
// TODO: Add a Text saying n messages in m conversations. (this will
// only be visible in API < 24)
private fun createSummaryNotification(
Expand Down Expand Up @@ -210,6 +241,10 @@ private fun updateNotification(
setGroup(groupKey)
setContentIntent(createViewPendingIntent(fcmMessage, context))
setNumber(messageCount)

val extraData = Bundle()
extraData.putInt("zulipMessageId", fcmMessage.zulipMessageId)
extras = extraData
}

val summaryNotification = createSummaryNotification(context, fcmMessage, groupKey)
Expand Down

0 comments on commit 9de656f

Please sign in to comment.