9

I want to read/access/log the notifications fired on notification bar by other applications.

I searched Intents and PendingIntents a lot, but couldn't get a solution.

Does my application need to be notified when any notification is fired?

Or Does android system provide something to read notifications by the user-level applications?

Amit Desale
  • 1,281
  • 3
  • 16
  • 43
Kishore
  • 952
  • 2
  • 11
  • 31

4 Answers4

7

Starting with api 18 (android 4.3) it's possible: http://developer.android.com/reference/android/service/notification/NotificationListenerService.html

Very easy to use, but the user have to give permission to the app manually.

OriolJ
  • 2,762
  • 1
  • 28
  • 22
5

Finally got the answer.!!! Using AccessibilityService

public class NotificationService extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    // TODO Auto-generated method stub.
//Code when the event is caught 
   }
@Override
public void onInterrupt() {
    // TODO Auto-generated method stub.

}

@Override
protected void onServiceConnected() {
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
      info.feedbackType = 1;
          info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
      info.notificationTimeout = 100; 
      setServiceInfo(info);
     }
}

And my Manifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.test.notify"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
     <service android:name=".NotificationService" android:enabled="true">
            <intent-filter >
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
           <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>
     </service>
   </application>
 </manifest>

Enjoy Coding.!!! :)

Kishore
  • 952
  • 2
  • 11
  • 31
  • 1
    The code is only to catch events related to notifications. On changing `info.eventTypes = ` we can catch different type of events. – Kishore Jul 13 '12 at 12:27
  • i am trying to get log in on service connected method, but it fails, does it require any permissions – Calvin Feb 21 '14 at 12:36
  • @Calvin : You need to turn the Accessibility service manually from Settings (Settings -> Acccessibility ) – Kushal Mar 16 '15 at 13:02
0

There is not any API for access any notification. At least it would be hole in security. Only thing you can is catching some events, that causes notifications (like sms).

Jin35
  • 8,602
  • 3
  • 32
  • 52
0

A Notification is raised by sending an Intent.

Intent intent = new Intent(this, NotificationReceiver.class);

The NotificationReceiver catches the Intent and pops up a Notification.

It is perfectly possible to catch the same Intent in one of your applications using a BroadCastReceiver and filter on the Intent you want.

DroidBender
  • 7,762
  • 4
  • 28
  • 37
  • There is no intent for "notification bar state change" or "notification fired". – Kishore Jul 11 '12 at 10:30
  • That is true, but the notifications are fired using Intents.. And Intents can be caught with a BroadCastReceiver. Afcourse, this all depends on what kind of Nofication you want to access. – DroidBender Jul 11 '12 at 11:15
  • Actually i want to log all the notifications. So i'm searching for a generic method to catch all the notifications. – Kishore Jul 12 '12 at 11:11
  • 2
    @MartijnVanMierloo : this is not true. notifications are added to the notificationManager, which uses a System service, not a broadcast. – njzk2 Jul 24 '13 at 09:05