get name of app which created the notification

Viewed 183

I'm working on an android application that listens for notifications. Is there any way to get the name of the app which created the notification.

1 Answers

You need to get the package name first on receiving the new notification from the notification service::

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    String pack = sbn.getPackageName();
    String ticker = new String();
    if(sbn.getNotification().tickerText != null) {
        ticker = sbn.getNotification().tickerText.toString();
    }
    Bundle extras = sbn.getNotification().extras;

    String title = extras.getString("android.title");
    String text = extras.getCharSequence("android.text").toString();
    int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
    Bitmap id = sbn.getNotification().largeIcon;
}

And then use the "pack" value to get the app name:

final PackageManager pm = getApplicationContext().getPackageManager(); 
ApplicationInfo ai; 
try { 
     ai = pm.getApplicationInfo( pack, 0); 
} catch (final PackageManager.NameNotFoundException e) { 
     ai = null; 
} 
final String applicationName = (String) (ai != null ? 
pm.getApplicationLabel(ai) : "(unknown)");
Related