How to read the push Notification data from system tray.? - android

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.

Related

Is there a way to get notification data if user opens application from launcher without clicking notification?

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.

Push Notifications to Android App

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.

Which event is called when app is in background mode?

According to this, when the message is both a notification and data message and the app is in the background, the message is received as follows:
Notification: system tray
Data: in extras of the intent.
Which intent is it refering to?
From the Firebase Notification Console I sent a message with Custom data, ie with some Key/Value. Is this considered a combined Notification and Data message? My onMessageReceived event does not fire when the app is in the background. Only the notification shows in the Notification bar at the top.
Which intent is it refering to?
The intent that starts the activity. See my answer here.
From the Firebase Notification Console I sent a message with Custom data, ie with some Key/Value. Is this considered a combined Notification and Data message?
Yes. See my answer here.

Firebase Pull notification

I'm working with Firebase Cloud Messaging. I have a couple of question I was not able to understand from the documentation:
Android: Lets suppose the app is closed (not backgrounded: closed). If I send a notification with also the data payload, this data payload is passed to the activity through the Intent Extra
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.
What if the user does not tap on the notification? Is the data payload lost? Is there a way to retrieve it?
iOS/Android. Lets suppose the user disabled the notification and I sent a notification to the client: is there a way to retrieve (pull) the notification at the application start?
Thanks very much
If the app is closed (not in background) the onMessageReceived method is called when the notification is received and there you can retrieve the data payload with remoteMessage.getData().get("key_for_parameter"); where key_for_parameter is the name of the parameter that you send in the notification. This method is called even if the application is closed. However, take into account that the "onMessageReceived" is called only if you omit the "notification" param in the notification (look this post)
So once you have the params in the onMessageReceived you can look for an strategy to use them in your application like storing in the DB and you are not going to lose the data if the user does not click on the notification.
if your app in background, Firebase will not trigger onMessageReceived(). Why.....? I have no idea. In this situation, I do not see any point in implementing FirebaseMessagingService.
According to docs, if you want to process background message arrival, you have to send 'click_action' with your message. But it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own "console" in order to enable marketing people to use it. So, this makes Firebase console also quite useless!
There is really good, promising, idea behind this new tool, but executed badly.
I suppose we will have to wait for new versions and improvements/fixes

Get data payload of android notification on dismiss

I'm using Firebase Cloud Messaging to send notifications with data payload to my app.
When i send a notification, if my app is running (foreground) i get the data overriding the onMessageReceived() from FirebaseMessagingService class.
If my app is not running the notification is sent to the "system tray" and when the user click's in notification my app start and i can get the data with the getExtras of the intent.
But, how could i get the data if the user dismiss the notification in "system tray"?
I need to write some background service that "listen" to the notifications to get this?
Seems like you are sending notifications from the Firebase console. These messages are always notification messages. If a notification message has an accompanying data payload then that data is only available to your application if the user taps the notification. If the user never taps the notification then that data will not be available to your app. So you should not use notification messages to send app critical data to your application.
If you send data messages they are always handled by onMessageReceived callback. At that point you can either silently handle the message or display your own notification. So use data messages if you want to be sure that your application has an opportunity to handle the data in the message.
So basically you want to store that data ?
if you want to store that data then write your code in OnReceive() method and write Database insertion code in it.
and put one flag in it like "Dismissed=true"
and if user open your notification then make it false and you can get your result.
There is no specific method to detect whether your app's notification is dismissed or not. So you have to manually maintain that data.

Categories

Resources