Reputation: 64844
How to read all the coming notifications in android. Is it possible to use the broadcast receiver to listen the incoming notifications and the ability to read the notifications information.
Upvotes: 46
Views: 93220
Reputation: 1995
Android provides android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
which you can use to get the notification information.
In your AndroidManifest.xml, declare a service like this:
<service android:name=".AppNotificationListenerService"
android:label="@string/service_name"
android:exported="false"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Create a class named AppNotificationListenerService
and extend it from NotificationListenerService
class AppNotificationListenerService: NotificationListenerService()
{
override fun onNotificationPosted(sbn: StatusBarNotification?)
{
super.onNotificationPosted(sbn)
// Query information from sbn using methods like:
// sbn?.notification.extras.get(Notification.EXTRA_TITLE)
// Some other keys include EXTRA_TEXT, EXTRA_SUB_TEXT
}
}
You also need to make sure that your application has permission to see all notifications. For that, you can use the below code:
val componentName = ComponentName(packageName, AppNotificationListenerService::class.java.getName())
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (!notificationManager.isNotificationListenerAccessGranted(componentName))
{
val notificationAccessSettings: Intent =
Intent(Intent(Settings.ACTION_NOTIFICATION_LISTENER_DETAIL_SETTINGS))
notificationAccessSettings.putExtra(
Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME,
componentName.flattenToString()
)
startActivity(notificationAccessSettings)
}
else
{
Log.d(LOGGER_TAG, "App has notification access")
}
Upvotes: 1
Reputation: 117
You need to do like this in onNotificationPosted in order to get all messages
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)){
Parcelable b[] = (Parcelable[]) extras.get(Notification.EXTRA_MESSAGES);
if(b != null){
for (Parcelable tmp : b){
Bundle msgBundle = (Bundle) tmp;
content = content + msgBundle.getString("text") + "\n";
/*Set<String> io = msgBundle.keySet(); // To get the keys available for this bundle*/
}
}
}
Upvotes: 2
Reputation: 546
First you must declare your intent to receive notifications in your manifest, so that you can get the android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
permission.
AndroidManifest.xml:
<service android:name=".NotificationListener"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Then create a NotificationListenerService
class and override the onNotificationPosted
function.
For more information, read the developer reference here: https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
Also look at this simple example application for implementation guidance: https://github.com/kpbird/NotificationListenerService-Example/
Upvotes: 29