Unable to send firebase FCM message to multiple device in flutter using topics - flutter

I'm trying to register my app on a topics called weather , then trying to send push notification from postman. I have tried below code in flutter end to register or subscribe user to a topics called weather.
void main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseMessaging.instance.subscribeToTopic('weather');
runApp(MyApp());
}
Then, after tried to send message using postman I'm getting message id but not getting any FCM message in my device.
subscribeToTopic is't not the way that I have tried to subscribe the device to topics ?

Related

I already get fcm key of ever users,How to send notification to another user with firebase cloud messaging?In dart

I have Fcm key
and working when i put fcm trial of cloud messageing in firebase
I tried but only got same device notification,
I am expecting that when I send a message to another user The other user need to get a notification while the app is not opened

flutter_local_notifications onNotificationReceived method - Flutter

Too long to read: The problem
Basically, I am looking for an onMessageReceived() callback but that works with flutter_local_notifications so I can handle and show the data to the user. This plugin only supports handling the onNotificationTap() action.
How am I expected to handle the message when the user receives it, for example, if they have Do Not Disturb on? Even if the local notification doesn't show, I need to show an Overlay at least, triggered by some onMessageReceived() function.
How can I update the notificationCount in my database when a local notification is received (scheduled)?
Description
In my project I am using:
Firebase Cloud Messaging (FCM)
flutter_local_notifications package.
When an event is scheduled in my app, the process is the following:
POST request to FCM with data only message.
My app receives the message through the onMessageReceived() callback.
Almost instantly I get 'Got a message whilst in the foreground!' message. This was triggered by the instant FCM data message.
The data inside the message triggers flutter_local_notifications to schedule a notification.
This scheduled local notification, received at a later date, cannot be handled (no OnMessage() function).
I don't schedule a notification directly on FCM because you can't do that from a post request (weird), but that would solve all my problems.
Problem
When the notification gets to the user's device, there is no way of handling the message (foreground or background)
I cannot display an Overlay with the notification, in case of the user being in the foreground
I cannot automatically update the notificationCount in my Firebase Realtime Database
Basically, I am looking for an onMessageReceived() callback but that works with flutter_local_notifications so I can handle and show the data to the user. This plugin only supports handling the onNotificationTap()` action.
Example of my process
This is what FCM has that flutter_local_notifications doesn't. Triggered when a notification is received by my app:
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('Got a message whilst in the foreground!');
[...]
if (scheduledDate != null) {
//using flutter_local_notifications
sendScheduledLocalNotification(itemTitle, 'Due Today', formattedDate);
//this notification, received at a later date, cannot be processed with this same function because it doesn't use FCM
}
//only shown with instant notifications (not scheduled)
if (notification != null) {
showOverlayNotification((context) {
return LocalNotificationOverlay(
title: notification.title!,
subtitle: notification.body!,
imageUrl: notification.imageUrl!,
);
}, duration: Duration(seconds: 3));
}
}
});
Yes, as far as I know we don't have the same stream option to listen to the notifications in the system tray for flutter_local_notifications as we have for FCM.
You can check this answer, It might help you.
I guess in the end you'd better do it in the back-end, and again this scheduled message being send/received via FCM.
I don't know about the mechanism that you're using and the function that sends the notification to user, but you can ignore sending notification to user if it has information about scheduling. Based on your code, scheduledDate and formattedDate are aware of it. So in your cloud function that sends the notifications you can instead of sending a notification, doing some task or triggering something(similar to the attached answer) and this will schedule sending that notification based on its time. Then in front-end for example you can update your databases every time you get a notification from FCM, because you didn't send them in the first place and you actually scheduled sending them in the planed time.

How to expire/dispose Notifications in Flutter App

I enabled notifications in my app and its working fine but how can I expire an old notification if a new one arrives or expire it after a certain duration?
The "flutter_local_notifications" plugin has a method to dismiss notifications. However, there is currently no method to get a list of the present notifications. You need to track them on your own (e.g. some persistent storage like SharedPreferences).
Retrieving pending notification requests :
final List<PendingNotificationRequest> pendingNotificationRequests =
await flutterLocalNotificationsPlugin.pendingNotificationRequests();
cancel the notification with id value of zero
await flutterLocalNotificationsPlugin.cancel(0);
Cancelling/deleting all notifications
await flutterLocalNotificationsPlugin.cancelAll();
you can achieve this with the flutter_local_notifications.

Flutter : Handle push notification message that come from non-firebase source

My app is setup to use firebase for push notification. I see the push and the data when I send a message thru firebase
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('Got a message whilst in the background!');
print('Message data: ${message.data}');
});
//trap when app is in foreground
FirebaseMessaging.onMessage.listen(
(RemoteMessage message) {
}
Now , another third party vendor(iterable) is sending push message. I see the popup but how do I get the data from the message. The firebase handler does not get triggered
Thanks
You can't get the message data from the FirebaseMessaging if you are not using Firebase.
Firebase messaging is exclusive to firebase, so you can't use it to get other third-party notifications. Your third-party notification provider will provide SDK, you need to integrate it into your app, in order to get the data from the message.

Cannot receive cloud function notification using Send to Topic Option

I am using Flutter and Dart to trigger cloud function push notification however, I cannot seem to receive any notification on my device. Can someone tell me what's wrong with my code?
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.myFunction = functions.database.instance("fm-90011")
.ref("orders/{userId}/{id}")
.onCreate((snapshot, context) => {
console.log(snapshot.val());
console.log("Inside push notification");
return admin.messaging().sendToTopic("orders",{notification:{title:"New Order",body:'Tap to view',clickAction:'FLUTTER_NOTIFICATION_CLICK'}});
});
Heres how the orders structure looks like in firebase realtime database.
In the cloud function log, I get the log messages as seen:
Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.
For you to send a notification to topics,the device must be subscribed to the topic.
here is how to subscribe a device to a topic
....
FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
//subscribe to a notification
_firebaseMessaging.subscribeToTopic("orders");
make sure user is subscribed to orders before adding order.