获取创建通知的应用名称

get name of app which created the notification

我正在开发一个侦听通知的 android 应用程序。有什么方法可以获取创建通知的应用程序的名称。

您需要在收到通知服务的新通知时首先获取包名::

@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;
}

然后使用“pack”值获取应用名称:

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