I'd tried NotificationListenerService provided by Android to listen to notifications by using it's OnNotificationPosted() method. That's all okay. How to detect a change in that notification?
For example: In WhatsApp, if a message is sent and then deleted, It removes the existing notification and sends a new notification as "This message was deleted".
So how to detect that which notification gets changed or updated?
Are you receiving the "changed" notification in onNotificationPosted()? As far as I understand, the notification is replaced instead of changed. So the changes may register as a newly arrived notification.
Update: it turns out "updating" the notification is indeed pushing a new notification but with the same id. So simply track the id of the notification to see which notification was updated.
In your notification listener onNotificationPosted()
public void onNotificationPosted(StatusBarNotification sbn)
{
//this is the notification id
int id = sbn.getId();
...
}
Related
I have implemented Firebase notification in my Android application. When my app is running, notification is displayed with my custom layout, but when application is not running, the notification is displayed with the default layout. How can I change the notification layout to my layout when application is not running. Also, I store shared preferences to let user toggle notifications. But when app is not running the notification is displayed anyways. How can achieve that?
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(SettingsFragment.getReceiceNotification()){ //if user wants to receive notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.push_notification_layout);
remoteViews.setImageViewResource(R.id.push_notif_icon,R.mipmap.ic_bird_black);
Intent intent = new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContent(remoteViews);
notificationBuilder.setContentTitle("Radyo Türkkuşu");
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentIntent(pendingIntent);
remoteViews.setTextViewText(R.id.push_title, "Radyo Türkkuşu");
remoteViews.setTextViewText(R.id.push_context, remoteMessage.getNotification().getBody());
//notificationBuilder.setLights (ContextCompat.getColor(MainActivity.context, R.color.pushColor), 5000, 5000);
notificationManager.notify(0,notificationBuilder.build());
}
}
Your problem is you using it with notification tray.
See this link
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
If you using {data:"something"}(data-message) with {notification:"something"}(display-message) while your app is in background the data payload will delivered to extras of the intent but not to the onMessageReceived() method.I assume you implement your code for showing notification, so when your app is in foreground onMessageReceived() is trigger and it display the desire notification you want but when it is not onMessageReceived() no get trigger instead android system will handle it with your notification payload. You just have remove {notification:"something"}(display-message/notification tray) from your server side code to always ensure onMessageReceived().
For anyone keep mention onMessageReceived() will always trigger no matter wether it is not foreground or background please visit this link
There are two types of FCM messages
Notification message
Data message
Messages which get sent from Firebase console are Notification message. In order to get message in onMessageReceived() use Data Message. Use below code at server side to send Data Notification message
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
Reference https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Your code should never be triggered when your app is closed or running in the background. See this:Firebase Notification
What you need to do is check how the app start up, tap the notification or tap the launcher icon. To do this, some data should be add into the notification then retrieve them in the first start activity in your app. If you can retrieve them successfully, it means your app is launched by tapping the notification, then you can do what you want to do.
I am doing Push notification message in android devices and follows GCM documentation for my reference and I have scenario that in the notification itself I have to show buttons and user clicked it will trigger the respective actions.
From the GCM documentation, in the notification payload we can add click_action to trigger the action when user touched notification...
How to show buttons (Like Accept/Reject ) in the notification message?
You can use .addAction in Notification.Builder.
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_accept, "Accept", prevPendingIntent) // #0
.addAction(R.drawable.ic_reject, "Reject", pausePendingIntent) // #1
// Apply the media style template
EDIT 1
Refer this link.
There is no way to add action buttons to a notification message. This may be a feature added later on but currently it does not exist.
Notification messages allow you to create a very specific type of notification, if you want to have a very custom notification then you should use data messages (not notification messages) and then when the message is received use the Notification.Builder to generate your custom notification with as many features as are available :)
How can I remove all notifications in the notification tray that were sent by my server when the user clicks on one notifiaction?
I have a chatting-app and the user gets a notification for each message (if the app is not in foreground). When the user clicks on one of those notifications the app will be brought to foreground / will be started. After that has happened, I want all other notifications in the notification bar to disappear as well.
No, you dont have to save the ids of all notifications, you can simply call:
NotificationManager nManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
nManager.cancelAll();
Use the following code to cancel a Notification:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.
I'm developing a messaging app that potentially creates multiple notifications. I want it such that these notifications can be updated and deleted when stuff happens.
For example, if I receive a message from A, the notification should say "You have 1 message from A". If another message arrives, it should say "You have 2 messages from A".
Then, if I receive a message from B, it should be a consolidated notification that simply says "You have messages" rather than a separate notification from each sender.
Also, when I click on the notification, it should cancel, and if I click on the user A chat window, any notification from user A should also cancel.
Right now I've implemented the code below for generating notifications:
PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder noti = new NotificationCompat.Builder(
context).setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(notificationMessage)
.setSmallIcon(R.drawable.ic_stat_notify)
setContentIntent(pendingIntent)
setAutoCancel(true)
setWhen(System.currentTimeMillis())
setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify(notifyId, noti.build());
This creates the notification just fine, and cancels it when the user clicks on it. However, I'm unsure how to update notifications, consolidate them, and cancel the appropriate ones. Does anyone know?
Thanks.
You need to use the same Notification ID all the time
public void notify(int id, Notification notification)
id - Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
Just post a new notification with the same ID as the old one, that will replace the old notification with your new one.
I am currently making an application that uses Notifications.
I was able to display the notifications, and remove them from the notification list when the user taps them. However, I would also like my notifications to disappear if the user sees them but does not act on them.
For instance, the user displays the notification list, then taps on a notification that is not mine, or just closes the notification list. During those cases, I am trying to make my notifications get canceled and not displayed the next time the user displays the notification list even if he did not do anything to my notification.
Is this possible?
Thanks! :D
(edit: if you are wondering why I thought of this, a very simplified explanation would be: think of the notification as a toast instead; a toast that has a longer existence, and makes sure that the user actually saw it before disappearing.)
you may remove a notification by providing its id to the cancel method:
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(your_notification);
however, i wouldn't recommend you to do it as it may confuse the user
It is very easy, there is a cancel method for notifications:
public void cancel (int id)
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancel (String tag, int id)
Cancel a previously shown notification. If it's transient, the view will be hidden. If it's persistent, it will be removed from the status bar.
public void cancelAll ()
Cancel all previously shown notifications. See cancel(int) for the detailed behavior.
Details are here:
http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
Update
If you want to cancel your notification when user sees that notification, there is a flag for that, FLAG_AUTO_CANCEL:
Notification notification1 = new Notification(R.drawable.icon, "test",
System.currentTimeMillis());
notification1.flags |= Notification.FLAG_AUTO_CANCEL;