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.
Asked
Active
Viewed 8.7k times
45
-
1You can have a look at the AccessibilityService, see http://developer.android.com/training/accessibility/service.html#create or http://stackoverflow.com/questions/14540394/listen-to-incoming-whatsapp-messages-notifications – Robe Elckers May 26 '13 at 09:30
3 Answers
28
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/

Monban
- 527
- 4
- 12
-
Note: This works but does not work for the text field of the Stacked Notifications. – user1406716 Mar 31 '15 at 07:47
-
4This example is not working for me i have a permission for notification but still its not showing any list – Bhanu Sharma Jul 07 '16 at 06:46
-
Say, is it also possible this way to convert all notifications to be ones without heads-up notification – android developer Sep 21 '16 at 12:56
-
3is it possible to read already existing notification or just new that will come? – Ewoks Jan 27 '17 at 09:35
-
it is working in marshmallow but error in 8.0 with notificationChannel – Sunil Chaudhary Nov 14 '18 at 12:24
2
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*/
}
}
}