Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Notification Title Android

How could I get the Notification Title of the notification ?

Here's my code :

-From Notification Service :

resultIntent= new Intent(NotificationService.this, StartNAFromNS.class);
                        resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " "));

-From StartNAFromNS :

String text = this.getIntent().getStringExtra(Intent.EXTRA_TITLE);

When doing this with only 1 notification, I get the correct title. However, if my application sends 2 notifications, I will get the title of the second notification.

How could I get the proper notification title ?

like image 910
Jerome Dumilieu Avatar asked Dec 13 '22 23:12

Jerome Dumilieu


1 Answers

By extending NotificationListenerService and using its onNotificationPosted method in our class we will be able to get notification title, text and package name. Using notification package we get its app icon, app name and many more.

public class MyNotification extends NotificationListenerService {
    Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // We can read notification while posted.
    for (StatusBarNotification sbm : MyNotification.this.getActiveNotifications()) {
            String title = sbm.getNotification().extras.getString("android.title");
            String text = sbm.getNotification().extras.getString("android.text");
            String package_name = sbm.getPackageName();
        Log.v("Notification title is:", title);
        Log.v("Notification text is:", text);
        Log.v("Notification Package Name is:", package_name);
    }
    }
}
like image 76
Amit Darji Avatar answered Dec 25 '22 17:12

Amit Darji