Reputation: 128
I programmed for a while now in flutter and I'm encountering an issue when handling notifications coming from the server through Firebase.
The problem is that if I receive a notification while thee app is closed or in background and I don't click it, no code is fired to interpret the data incoming. On the other hand when it is in foreground or it is clicked, everything works fine. Obviously this is not a Firebase problem.
So how do I handle notification data when it is not clicked?
For example: I receive a chat message from another user while I'm on the web. If I don't click the notification to open the chat (discard it or just ignore and manually open the app) the message won't be saved in the local database.
The onResume and onLaunch functions are called only when the user clicks the notifications.
I thought about checking at app resume if there are unread notifications, but I don't have a clue about how to do this.
Any help will be appreciated. Thank you!
Upvotes: 3
Views: 3648
Reputation: 599101
There are two types of messages in Firebase Cloud Messaging:
- Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
- Data messages, which are handled by the client app.
When your app is not active, notifications messages are displayed by the system. If the user doesn't click the notification, your app has no way of knowing that the message was ever received.
If your use-case requires that your app knows of such messages, you should use a data message. There are always delivered to your app. That also means that your app gets to decide whether to display a notification to the user.
A third option is to send a combined notification/data message. You do this by having both a notification
and a data
property in the message payload that you send to FCM.
Upvotes: 5