praneeth
praneeth

Reputation: 43

get name of app which created the notification

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.

Upvotes: 2

Views: 988

Answers (1)

bre_dev
bre_dev

Reputation: 572

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)");

Upvotes: 1

Related Questions