Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See notification of other app

What API I should use for my app to see if I've received a notification from example from facebook, and so write in one textbox "Facebook!"?

like image 274
malloc Avatar asked Apr 24 '14 21:04

malloc


People also ask

Can I see my notification history?

Swipe up from the homescreen and open Settings (the one with a gear icon). Scroll to Notifications. Open Advanced settings from the following menu. Select Notification history.


1 Answers

Your best bet is to use NotificationListenerService, which was added in API level 18.

Here's an example:

public class FacebookNotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        final String packageName = sbn.getPackageName();
        if (!TextUtils.isEmpty(packageName) && packageName.equals("com.facebook.katana")) {
            // Do something
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // Nothing to do
    }

}

In your AndroidManifest

<service
    android:name="your.path.to.FacebookNotificationListener"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

Also, your users will need to enable your app to listen for notifications to be posted under:

  • Settings --> Security --> Notification access

But you can direct your users straight there by using the following Intent:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
like image 169
adneal Avatar answered Oct 06 '22 02:10

adneal