Open Flutter app based on notification data? - firebase

How can I open the flutter app based on notification data using FirebaseMessaging?
So, when I tap on a notification (received through Firebase Messaging), the message handler should evaluate the data and based on them open the app. At the moment it is asynchronous, meaning the app starts and the notification handler continues the work.
It is important to know, that Navigator.push can't be used, as there is an important app setup screen, which checks the auth state and so on at app start. How to solve this issue?

When you tap a notification from the device's notification centre the operating system will launch the app, provided of course you have set up Firebase messaging correctly as per instructions the onLaunch or onMessage functions will get called.
Now since firebase messaging configured pretty much as early as possible, typically inside the main entry of the flutter app you will need to store the notification somewhere first. This can be a global state object created before anything that uses it.
final notifications = <NotificationData>[]; // globally accessible (i.e. via Provider)
Then in the handler of the messages...
NotificationData onMessage(Map<String, dynamic> data) {
notifications.add(NotificationData.fromJson(data));
}
The flutter app will start and the MaterialApp will get built. At this point you can have a loading screen to check if the user is authenticated or any new messages in the notifications global state. If there are you can route to a page to display the notification, or get the user to sign in then route to the notification page.

Related

Firebase messaging background handler not called when app is terminated (Flutter)

I am trying to update my app's badge count number of my flutter app when receiving push notifications when the app is terminated.
The Firebase messaging background handler works fine if the app is in the background but doesn't work when the app is terminated.
I have read in the documentation :
On iOS, if the user swipes away the application from app Switcher, it must be manually reopened again for background messages to start working again.
Does it mean that on iOS there is no way to update the badge count using the firebase background handler (by the logic you implement in the handler of course, just need the handler to be called) ?
Any app do this today so i wonder why it would not be possible with Firebase Messaging.
I have actually found a solution, by using a service extension in swift and passing data as payload server side, using content-available: true in python.
Here is the documentation i have followed to implement the notification server side.
Also, there is an ongoing bug / feature request for flutter and Firebase regarding this case.

Listening for changes in Firestore database in and out of the App - Swift

I need to listen for update on the client side whenever something in the Firestore database changes.
I need some clarification about this topic:
Let's say that we want to notify the user whenever a new document is created in a collection:
We have 2 cases:
when the user is on the app
when the user is out of the app
for the second point I can create a cloud function firestore trigger that notifies the user through the push notification service
But on the first point I don't know which is the best approach.
(maybe setting up a snapshotListener? but how can I do that on the global scope of the app? Is it the right approach?)
Should you implement a FCM notification for the second scenario, the notification will be fired to your app no matter if it is on background or foreground, so you can use this approach for both situations actually.
You can use this solution to not fire a notification (on the frontend) and in case you don't want to fire that if the app is in foreground you can just keep the completionHandler() blank and that's it.

FCM method to determine if token/user has disabled push Notifciations

I know there are methods to check both iOS and Android to see if the user has locally disabled push notifications for an app, that is done within the app itself. But if a user disables push notifications for an app is this reported back to FCM? Does FCM know not to send a notification to that user and if so, is there anyway of pulling that info out of FCM.
The reason for this question is if the user hasn't used the app in a while and they disabled push notifications - without the user starting the app (thus getting updated tokens/checking for permissions) there is no way to know the user disabled push notifications - unless we can extract that from FCM.
And along these same lines, if a user has uninstalled the app, does FCM get updated that the token is now invalid due to app removal and removes it from its db - and if so, is there anyway to way to extract this from FCM?
In both cases, we need to either update our DB that the user has disabled push notifications (and possibly use alternate communication methods) or mark the user as 'uninstalled' and thus remove that user from any app communication method (fcm, sms, email).

Firebase messaging not triggering onResume / onLaunch properly

There are no problems with the code, because if I press on the notification it triggers onResume, however if the app it's killed or the user deletes the notification no callbacks are triggered.
Right now I'm saving notifications locally on an SQL db, so when a user logs in back it shows how many notifications he hasn't read. However, If I can't trigger no callback when he deletes the notification or when the app is killed (Actually when the user logs in back, after a couple of seconds onMessage is called with the notification sent while the app was killed, however this still bothers me 'cause I can't get user's attention with notifications) I can't save them locally and when he logs in back in the app, those notifications that he dismissed won't ever be back.
Is there a way to callback a function when the user dismisses notifications or when my app is killed?
Basically as you know, notifications are designed to be removed by users.
So if you want users to see all the notifications from your service, you should consider with another UX something like notifications tab (Facebook, Instagram has)
And, manage all notifications from the server, set flag if it is been activated or not.
As far as I know, there is no callback when the user dismissed the one.
I hope it can help you to decide your service design.

Enabling the notification feature for my firebase app

I have made an app with the use of firebase. It is basically a chat application in which you van send and receive the text and images. I want to add a functionality in it that whenever a user sends a msg, then another user should get a notification . When I try to send a notification through the firebase console, then it is working, but when a user messages through the app, then it is not showing any notification to another user. So, can anyone tell me that how can this functionality be achieved ?Also, provide some sample code to see how things are working
You need to use cloud functions for that:
https://firebase.google.com/docs/functions
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests
First, you can register the user to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("news");
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Then when the message is sent to the database, you can use onWrite() in cloud functions which is a database trigger to be able to send the notification.
https://firebase.google.com/docs/functions/database-events

Resources