I am sending notification and data payload from my server and generating a notification in Android System tray. but if I want to remove particular notification if user read that conversation through app and not via clicking system tray item then how to find that notificationId to write code like notificationManager.Cancel(notificationId) and not notificationManager.CancelAll()
Related
Is it possible to read the push notification information (Both notification {} and data{} from FCM), without tapping/clicking on the notifications. ?
As for the the Documentation, Notification come your app, either it is in foreground or background, But in these case of background, a 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.
But my requirement is to read the notification without clicking on it.
Is it possible..? if yes please give me some information / some links.
You can use FCM data message instead of notification, everytime you receive a new data message onMessageReceived will be called and you can choose either to show this notification or not, and will get all relevant data.
I have got notification from FCM. And i have opened the app from launcher without clicking the notification. So i dont get the notification data in activity. Is there a way to get notification data if user opens application from launcher without clicking notification?
It totally depends upon the app status to get notification data without clicking on Notification,
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background.
In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
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.
I shared this data just to inform you that the answer provided by #Sagar Zala will not work when your app is in Background
FCM onMessageReceived Documentation
You can not directly get notification data if you opens app from launcher icon.
But in this case you can store notification data in SQLite or Shared Preference while notification arrive and use this data when you opens app from launcher icon.
Note: Clear notification data once app opens.
Thanks.
I have implemented FCM push notifications in an android app.
While I am logged in to the app. I get the notification in the form that I am expecting as below.
When the app is in background I receive the json response instead as below.
Following is the code I have added in onMessageRecieved()
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))/*Notification with Image*/;
How can I get the notification in the same manner in both cases.
Any help will be appreciated thank you
After setting up FCM in Android application, you can use Firebase console to send notifications. When foregrounded application receives a notification, onMessageReceived method is invoked. You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device’s system tray and you can not handle notification with onMessageReceived method. When the user taps on a notification, it opens the app launcher by default. For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don’t want to show notification dialog to the user, you can’t do these when the application is backgrounded.
Message Types
With FCM you can send two types of messages to clients :
1- Notification message, sometimes thought of a “display message”
2- Data message, which are handled by the client application
According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. Data messages let developers send up to 4KB custom key-value pairs.
Solution:
If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method.
Here is the complete article.
That is so because push notifications handles in foreground and background in different ways.
From the documentation
Notification messages delivered when your app is in the background. In
this case, the notification is delivered to the device’s system tray.
A user tap on a notification opens the app launcher by default.
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.
Also you have to remember that there are 2 types of firebase pushes - Notification message and Data message.
Looks like you are using Notification messages, so they are working as expected.
Just use data type of messages and so you can always handle them as you need.
I have an android app connected to FCM(firebase cloud messaging) for sending notifications . However , when the app is in background , it doesn't apply
custom notifications. I think this is because it does not enter OnMessageReceived method . Whats the solution for this ? As i read somewhere it can be done using HTTP Api but i am not sure of how to go about it ? Please help.Thank you.
Whether the method onMessageReceived is called is dependent on the notification payload.
See 'Handling Messages' section in FCM docs here.
Handling messages
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
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.
In summary, if the app is in foreground, then onMessageReceived will always be called. If the app is in background, then onMessageReceived will only be called if the notification payload is data only.
So to answer your question, if you must have onMessageReceived every time regardless if the app is in foreground or background state, then set your notification payload to contain data only.
I am able to group notifications for my app when it is in foreground. Whenever the app goes in background it creates multiple notification. How can I fix this?
As google says in this site, when app is in background notifications are administrated by the system by default :
Notifications delivered when your app is in the background. In this
case, the notification is delivered to the device’s system tray. A
user tap on a notification opens the app launcher by default.
But if you want to work with the notifications by "yourself" you should send the 'data message' instead of the 'notification' as is said here and you can handle the notification.