Skip to content

Commit

Permalink
add more fields to notif processing
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Jul 5, 2024
1 parent 3ed8af2 commit e4644fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Notification
import android.app.NotificationChannel
import android.content.ComponentName
import android.content.Context
import android.icu.text.UnicodeSet
import android.os.Build
import android.os.UserHandle
import android.service.notification.NotificationListenerService
Expand Down Expand Up @@ -85,20 +86,18 @@ class NotificationListener : NotificationListenerService() {
// Do not notify for media notifications
return
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
val channels = getNotificationChannels(sbn.packageName, sbn.user)
channels?.forEach {
notificationBridge.updateChannel(it.id, sbn.packageName, false, (it.name
?: it.id).toString(), it.description ?: "")
}
} catch (e: Exception) {
Timber.w(e, "Failed to get notif channels from ${sbn.packageName}")
try {
val channels = getNotificationChannels(sbn.packageName, sbn.user)
channels?.forEach {
notificationBridge.updateChannel(it.id, sbn.packageName, false, (it.name
?: it.id).toString(), it.description ?: "")
}
} catch (e: Exception) {
Timber.w(e, "Failed to get notif channels from ${sbn.packageName}")
}
if (NotificationCompat.getLocalOnly(sbn.notification)) return // ignore local notifications TODO: respect user preference
if (sbn.notification.flags and Notification.FLAG_ONGOING_EVENT != 0) return // ignore ongoing notifications
//if (sbn.notification.group != null && !NotificationCompat.isGroupSummary(sbn.notification)) return
if (sbn.notification.group != null && !NotificationCompat.isGroupSummary(sbn.notification)) return
if (mutedPackages.contains(sbn.packageName)) return // ignore muted packages


Expand All @@ -109,30 +108,49 @@ class NotificationListener : NotificationListenerService() {
}
}

var tagId: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
tagId = sbn.notification.channelId
}
val tagId = sbn.notification.channelId
val title = sbn.notification.extras.getString(Notification.EXTRA_TITLE)
?: sbn.notification.extras.getString(Notification.EXTRA_CONVERSATION_TITLE)
?: ""

val text = sbn.notification.extras.getString(Notification.EXTRA_TEXT)
var text = sbn.notification.extras.getString(Notification.EXTRA_TEXT)
?: sbn.notification.extras.getString(Notification.EXTRA_BIG_TEXT)
?: ""

// If the text is empty, try to get it from the text lines
if (text.isBlank()) {
val textLines = sbn.notification.extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES)
textLines?.let {
text = textLines.joinToString("\n")
}
}
// If the text is still empty, try to get it from the info text
if (text.isBlank()) {
val infoText = sbn.notification.extras.getString(Notification.EXTRA_INFO_TEXT)
infoText?.let {
text = it
}
}
// All else fails, try to get it from the ticker text
if (text.isBlank()) {
val tickerText = sbn.notification.tickerText
tickerText?.let {
text = it.toString()
}
}

text = text.replace('\u2009', ' ') // Replace thin space with normal space (watch doesn't support it)

val actions = sbn.notification.actions?.map {
NotificationAction(it.title.toString(), !it.remoteInputs.isNullOrEmpty())
} ?: listOf()

var messages: List<NotificationMessage>? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val messagesArr = sbn.notification.extras.getParcelableArray(Notification.EXTRA_MESSAGES)
if (messagesArr != null) {
val msgstyle = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(sbn.notification)
messages = msgstyle?.messages?.map {
NotificationMessage(it.person?.name.toString(), it.text.toString(), it.timestamp)
}
val messagesArr = sbn.notification.extras.getParcelableArray(Notification.EXTRA_MESSAGES)
if (messagesArr != null) {
val msgstyle = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(sbn.notification)
messages = msgstyle?.messages?.map {
NotificationMessage(it.person?.name.toString(), it.text.toString(), it.timestamp)
}
}

Expand Down
16 changes: 7 additions & 9 deletions lib/background/notification/notification_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class NotificationManager {
TimelineAttribute subtitle = TimelineAttribute.subtitle(notif.title!.trim());
TimelineAttribute content = TimelineAttribute.body(notif.text!.trim());

if ((notif.messagesJson?.isNotEmpty ?? false) && jsonDecode(notif.messagesJson!).isNotEmpty) {
if (jsonDecode(notif.messagesJson ?? "[]").isNotEmpty) {
List<Map<String, dynamic>> messages = List<Map<String, dynamic>>.from(jsonDecode(notif.messagesJson!));
content = TimelineAttribute.body(NotificationMessage.fromJson(messages.last).text!.trim());
}
Expand All @@ -110,14 +110,12 @@ class NotificationManager {
//TODO: change to use preferences datasource
List<String>? disabledActionPkgs = (await _preferencesFuture).getStringList(disabledActionPackagesKey);
if (disabledActionPkgs == null || !disabledActionPkgs.contains(notif.packageId)) {
List<Map<String, dynamic>> notifActions = List<Map<String, dynamic>>.from(jsonDecode(notif.actionsJson!));
if (notifActions != null) {
for (int i=0; i<notifActions.length; i++) {
NotificationAction action = NotificationAction.fromJson(notifActions[i]);
actions.add(TimelineAction((META_ACTION_LENGTH)+i, action.isResponse! ? actionTypeResponse : actionTypeGeneric, [
TimelineAttribute.title(action.title)
]));
}
List<Map<String, dynamic> > notifActions = List<Map<String, dynamic>>.from(jsonDecode(notif.actionsJson!));
for (int i=0; i<notifActions.length; i++) {
NotificationAction action = NotificationAction.fromJson(notifActions[i]);
actions.add(TimelineAction((META_ACTION_LENGTH)+i, action.isResponse! ? actionTypeResponse : actionTypeGeneric, [
TimelineAttribute.title(action.title)
]));
}
}
attributes.add(subtitle);
Expand Down

0 comments on commit e4644fa

Please sign in to comment.