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
Related
I am currently trying to make a personal location application between 2 devices on Android.
The concept is simple: I install my application on my phone as well as on that of my wife and each can geolocate the other.
(This application is strictly personal)
To achieve this, I thought of using sending notifications by FCM.
Telephone A sends a request to telephone B which listens via a service for the reception of a message.
When phone B receives the request, it returns the GPS coordinates via FCM so that phone A displays them on a MAP.
(I also have the possibility to store the coordinates in a database instead of sending back an FCM message)
But FCM's documentation says:
"When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification."
Of course, this reduces the scope since it forces the user of the phone receiving the notification to click on it to activate the actions of the service.
Can FCM still meet my needs through another channel?
Are there other options to send a "request" to another phone?
(I know that this kind of application exists on the PlayStore, but I want to try to make mine :-))
The key word in that section from the documentation you quote is notification messages. Firebase Cloud Messaging supports two types of messages:
Notification messages, which are display by the system when the app is inactive - and delivered to your application code when the app is active.
Data messages, which are always delivered to your application code.
It sounds like you'll want to use data messages for this use-case
I'm currently implementing a Chat application using Firebase Cloud Messaging to send push notifications. Using the notification field in the API call, Firebase displays them automatically without having to manually create a service and listen for the messages.
The push notification is pretty generic, just says "You have a new message!", so it doesn't make sense to keep adding new pushes every new message, I need firebase to not show a new push if the message body contents the same of a previous one.
Is it posible to do without implementing the service and handling Notification show manually? Found no references of this use case in the documentation.
If your application is in Foreground then you receive the notification in onReceive of your FirebaseMessagingService. Else notification is delivered to system notification tray. Since you don't know the id of the notification so you may not retrieve it. It is also possible that your application is not running at the time you receive the notification. So logically it is NOT possible even by implementing the service. Well you can solve the problem by another approach. You can use Firebase Database in conjuction with Cloud Functions. Just have a look on developer guide of cloud functions and you will find that they can help yourself achieve you what you want. As a solution skeleton : Post messages to Firebase Database and Send notification using cloud functions. When the recipient reads the message update the database to reflect message has been read like having a variable seen. Design your cloud function such that it reads the value of seen value of last message and sends notification only if it was true. Hope this helps.
For manual handling you have to use the service. Go for handleIntent(intent) of FirebaseMessagingService.
This method gets called when application is in foreground, background and killed state. To avoid the duplication, do not call super.handleIntent(intent). This will prevent the automatic push notification when app in BG or killed state.
This worked for me.
I am using fcm push notification for my android app. I was able to display push notification on system tray when app is not launched. When I tap on the notification it opens the app launcher by default and I start an activity A from there. But the issue is, if I put the app to background and click on the app icon it again opens the app launcher rather than opening existing Activity A.
If the app process is killed, start the launcher activity. If the app is in the background, you can pass an intent to the notification which starts a DummyActivity that has no code on it, and immediatelly calls finish() on its onCreate() method. This will bring your app to the foreground.
Several things are not clear in your question. For example: How you send messages (from developer console or through rest api post requests to firebase backend)? What is your desired behaviour for app when push messages come? I will try to give you general answer that probably helps you to address issue and understand how to implement desired behaviour.
In any case, there are two types of Firebase push messages:
data messages
notification messages
more details about it check on Notification & data messages page
If you want to send additional details to activity that you are starting (something similar to bundle extras), you should use data messages and handle those in your service that extends FirebaseMessagingService by overriding onMessageReceived(RemoteMessage remoteMessage) method. This method is preferable for me because it is much more flexible. You can define all the details about showing notification based on received firebase message, including if notifications are bundled, what happens in details when user click notification and almost everything related to it.
If you don't need to start certain activity with some parameters, than you can use push messages and just define click_action. This method allows you to add define title, text and sound of notification (beside some other details) but it is not as flexible as if you send data messages
Here you can find detailed overview of possible parameters that you can use for different type of messages
Hope this helps
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.
Is it possible/is there a reliable way to get a call back when a notification is received on the android platform? i.e. I want to record when the notification is received (not when it is later opened).
No, It's your app which receives the message so if you need things logged, then log it. Simply handle it as you like once you receive it. Note that if you use FCM with Notification message message type, then you will not be directly notified about message arrival when app is in background (notification will be automatically posted by Firebase - your app will get message payload once use taps the notification). In such case consider switching to Data messages so you will get it no matter your app is in foreground or not.
See docs: https://firebase.google.com/docs/cloud-messaging/downstream