Skip to content

Instantly share code, notes, and snippets.

@Danil0v3s
Created October 7, 2021 20:20
Show Gist options
  • Save Danil0v3s/fa7327afbadf42a7d9537faad1f6b830 to your computer and use it in GitHub Desktop.
Save Danil0v3s/fa7327afbadf42a7d9537faad1f6b830 to your computer and use it in GitHub Desktop.
val notificationListenerString = Settings.Secure.getString(this.contentResolver, "enabled_notification_listeners")
if (!(notificationListenerString != null && notificationListenerString.contains(packageName))) {
val requestIntent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
requestIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(requestIntent)
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.firstsoft.historiconotificacoes.notificationrecorder">
<application>
<service
android:name="br.com.firstsoft.historiconotificacoes.notificationrecorder.src.service.NotificationRecorderService"
android:exported="true"
android:label="Notification Recorder Service"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
package br.com.firstsoft.historiconotificacoes.notificationrecorder.src.service
import android.app.Notification.EXTRA_BIG_TEXT
import android.app.Notification.EXTRA_TEXT
import android.app.Notification.EXTRA_TITLE
import android.content.pm.ApplicationInfo
import android.os.Bundle
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import br.com.firstsoft.historiconotificacoes.domain.src.model.AppNotification
import br.com.firstsoft.historiconotificacoes.domain.src.usecase.notification.SaveNotificationUseCase
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject
@AndroidEntryPoint
class NotificationRecorderService : NotificationListenerService() {
@Inject
lateinit var saveNotificationUseCase: SaveNotificationUseCase
override fun onNotificationPosted(sbn: StatusBarNotification) {
super.onNotificationPosted(sbn)
val notification = AppNotification(
id = sbn.id,
appName = getApplicationName(sbn.packageName),
title = sbn.notification.extras.get(EXTRA_TITLE)?.toString() ?: "",
message = getNotificationText(sbn.notification.extras),
packageName = sbn.packageName,
createdTime = sbn.postTime,
extras = getAvailableKeysAndValues(sbn.notification.extras, sbn.packageName)
)
CoroutineScope(Dispatchers.IO).launch {
saveNotificationUseCase(SaveNotificationUseCase.Input(notification))
}
}
private fun getAvailableKeysAndValues(
extras: Bundle?,
packageName: String
): Map<String, String> {
if (extras == null) {
return emptyMap()
}
val map = mutableMapOf<String, String>()
extras.keySet().forEach { key ->
val value = extras[key]
if (value != null) {
when (value.javaClass) {
java.lang.String::class,
java.lang.Boolean::class,
java.lang.Integer::class -> {
map[key] = value.toString()
}
}
}
}
return map
}
private fun getApplicationName(packageName: String): String {
val ai: ApplicationInfo? = try {
packageManager.getApplicationInfo(packageName, 0)
} catch (e: Exception) {
null
}
return (if (ai != null) packageManager.getApplicationLabel(ai) else "(unknown)") as String
}
private fun getNotificationText(bundle: Bundle): String {
val title = (bundle.get(EXTRA_TEXT) ?: bundle.get(EXTRA_BIG_TEXT)) ?: ""
return title.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment