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.
Related
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 ?
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.
I need to use firebase_messaging to send message from my backend to my flutter apk.
When initialize firebase, these are the 4 keys essential in the FirebaseOptions
apiKey : Is this the Server Key found in FCM?
appId : Where can I find this key?
messagingSenderId : Sender ID FCM
projectId : Where can I find this?
Besides, in order for me to send message to a single user. I need to tell Firebase who am I right? May I know which ID shall I use?
Thx
When you register with firebase you obtain the push notification token. This token is used to send a notification to the device the app is installed on.
Assuming you already initialized firebase app in flutter, and you added the google-services.json in your app. You can obtain your push notification token in the following way:
messaging = FirebaseMessaging.instance;
messaging.getToken().then((value){
print(value); //this is the push notification token
});
You can also test that you are able to receive the notification on the app from the firebase console. You can send a notification to the app under Compose notification section in the firebase console. It only requires the notification token you obtained from the app.
I am getting push notification from a non-firebase source. I see the push popup and when selected opens my app. However, I need get the payload from the message.
Firebase messaging handler only get me the payload from push message originate via firebase. Is there another plugin that will show me the message payload from non-firebase push.
Thanks
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.