Get Android Notification Title
How can I get the title of a notification notification?
Here's 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 you do this with just one notification, I get the correct title. However, if my app sends 2 notifications, I will get the header of the second notification.
How can I get the correct title of the notification?
source to share
The notification id
must be unique in your application.
If a notification with the same
id
has already been sent by your app and is not canceled yet, it will be replaced with updated information.
NotificationManager notiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(UNIQUE_ID, notification);
If you are using the method PendingIntent.getActivity()
, use different requestCode
for different notifications:
Intent resultIntent= new Intent(NotificationService.this, StartNAFromNS.class);
resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " "));
PendingIntent pI = PendingIntent.getActivity(mContext, REQUEST_CODE, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Hope this helps!
source to share
By extending NotificationListenerService
and using our method onNotificationPosted
in our class, we can get the header name, text and package name. Using a notification bundle, we get our app icon, app name, and 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);
}
}
}
source to share
This code works correctly for fcm. We can send message and header from console or fcm server. Notification received by registered mobile app.
@Override
public void onMessageReceived (String from, Bundle data) {
//Getting the message from the bundle
long dateTime = data.getLong("google.sent_time");
Bundle notificationBundle = data.getBundle("notification");
String message = notificationBundle.getString("body");
String title = notificationBundle.getString("title");
//Displaying a notiffication with the message
sendNotification(title, message);
}
// The next method generates a notification and displays the notification
private void sendNotification(String title, String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("message", message);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= 21)
noBuilder.setSmallIcon(R.mipmap.ic_launcher);
else
noBuilder.setSmallIcon(R.mipmap.ic_launcher_small);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
}
source to share