Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notified when a notification is notified [duplicate]

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?

like image 874
Kishore Avatar asked Jul 11 '12 09:07

Kishore


People also ask

Why do I get two of the same notifications?

Android devices with 2 copies of the app installed on the device can also receive duplicate notifications. This could happen if you have a production and staging / dev app installed at the same time with different Android package names. This would result in two different player_ids and two different push tokens.

Why do I get 2 notifications for Instagram messages?

If you've added multiple Instagram accounts, you may get push notifications from any account that has them turned on. This depends on when you last logged in and the number of devices that are logged in to an account.


2 Answers

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.

like image 131
OriolJ Avatar answered Oct 25 '22 13:10

OriolJ


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.!!! :)

like image 26
Kishore Avatar answered Oct 25 '22 13:10

Kishore