How to hide a new message notification when user is actively chatting with the sender in flutter - flutter

I am implementing a chat feature in my flutter app. I use FCM to send a notification when a new message comes. I propagate the FCM message to flutter local notification when the notification came while the app is in the foreground. My problem is I don't want to show the local notification when the user is actively chatting with the sender.
I have firebase messaging configuration in app.dart and I have another stateful widget to show the message details and chat. I will get fromUid from notification data. I have also open chat message UID's in the message details widget. I am trying to compare these two UIDs values from notification and message detail widget and if the UIds are the same don't notify.
I try using change notifier and RouteObserver but it's not working fine. I will love any help and recommendations.

I have implemented a similar feature in my app. This is what I did.
I am using flutter and hive for local storage.
Do this when entering chat view
Hive.box("settings").put("currentChatId", xxx)
And on popping the page, set it to null again
On receiving notifications, before showing local notification you can just use .get("currentChatId")!= null && id==notificationId to check if it should show it or not.

Related

How to setup push notification with supabase in flutter?

I had just replaced my app with supabase from firebase in flutter app.
Firebase has firebase_messaging that provide push notification to device token.
Is there any facilities to create function in supabase and send notification to selected user with device token while changing in database.
If yes than please suggest some function else suggest some method to send notification.
Supabase does not provide a mechanism to send push notifications at this point, so you can use FCM or other push notification sending services to send them out to the users.
If you are going with FCM, you can setup your own backend using Firebase cloud functions or other backend service that you prefer to send the notifications from.
Typically, you would want to send push notifications to your users when there is some kind of change in the database, like when there was a new message sent or someone liked a post. You can listen to these events using Supabase database webhooks. With database webhooks, you can send your backend a request whenever you receive certain data change happening in your database.

Where to place alertbox in flutter?

I have a flutter app that uses flask as backend and when ever some data changes in the backend, I want to send an Alert Dialog to user. Although I know how to use Alert Box, I'm confused where the code for it should be placed as I'm using 3 screens and navigating then via PageView.
My use case is that I have a PIR sensor equipped onto a raspberry pi and when ever motion is detected, motion_status value turns to true and sends to flutter app via flask app.
I'm confused about where i need to place the Alert Dialog widget so that when ever movement happens, no matter what screen i'm in i should get alert box .
So when an update happen in the backend, you have couple different ways to get the app notified about the change just happened,
1- first you create a stream in the flutter app calling the server to check if any change happened, that stream reoccurs every few minutes or seconds, but that's not a good solution because there will be a lot of calls to the server and the info won't be instant.
2- use a web socket system
Best solution
3- use FCM Firebase cloud messaging system, whenever a change happened in the backend you write a code that calls Firebase account associated with your flutter app and your Firebase account will send a notification with data load to your app, when your app receives that real time notification you can then show to the user the alert dialog

how to send local notifications when app is terminated in Flutter

I want to implement local notifications in my chat app, that whenever User A send message to User B then I want to send notifications,, it is possible to send local notification when the app is terminated,, As you know guys firebase functions are paid so I am trying to work with local notifications...
Thanks in advance

Flutter and Onesignal, hide notification

I'm having a chat in my app with notifications about new messages being shown and I'm using OneSignal for this.
However if the chat is with the person and the notification is about the chat open at that moment, I'd like it to hide the notification (not completely mute though as I'm using it for the chat messages being added into the conversation as well).
Is this doable or do I have to do for example save the open chat into the database, then check whatkind of notification is to be sent based to the open chat? (which would of course cause many kinds of issues if the user just simply closes the app)
Should I somehow edit the state of:
OneSignal.shared.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
}
Or is there another trick into this to achieve it?
I know this is an old question but if you do this: -
OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.none);
after you've initialized OneSignal it should do what you want

Send Notification as message with Firebase Cloud Messaging in Swift

I'm trying to create a Chat App. When the app enters background and if he receives a message, then a notification must appear.
What I understand is that FIRMessaging.sendMessage() sends something to the cloud. But how the other device is gonna receive it ? It cant be continually looking for messages that are targetted to him...
I would like to understand how to use this fonction and what are the parameters to pass in it. So far and with the current documentation, I couldnt do it.
[[FIRMessaging message]sendMessage:(nonnull NSDictionary *)message
to:(nonnull NSString *)receiver
withMessageID:(nonnull NSString *)messageID
timeToLive:(int64_t)ttl;
What should I put in the receiver ? It's userId in firebase ? or what ?
In the exemples they talk about SENDER_ID + "#gcm.googleapis.com"
which confuses me cause it looks like it's either my Id or the Id of the firebase App..
I cant even figure out if i'm trying to send an upstream or downstream message (dont understand)
Well all I can say is that I really get my head around it...
https://firebase.google.com/docs/cloud-messaging/upstream#handle_upstream_message_callbacks_1
Thanks!
Firebase Cloud Messaging and Firebase Notifications (which is built on Firebase Cloud Messaging) handle this automatically for you. From the documentation of Firebase Notifications:
When your app is in the background on a user's device, notifications are delivered to the system tray. When a user taps on the notification, the app launcher opens your app. If you want, you can also add client message handling to receive notifications in your app when it is already in the foreground on the user's device.