Get the notification title for Android… here is a solution to the problem.
Get the notification title for Android
How do I get the notification title for a notification?
Here is my code :
– From the 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 right title. However, if my app sends 2 notifications, I’ll get the title of the second notification.
How do I get the right notification title?
Solution
By extending the NotificationListenerService
and using its onNotificationPosted
method in our class, we will be able to get the notification title, text, and package name. Using the notification package, we can get its app icon, app name, and much 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);
}
}
}