Reputation: 565
In my Flutter project, I want to be able to request some data from the smartphone of the user. Now, since I use Firebase, I tried out the firebase_messaging plugin. At this point, it enables me to send a notification message to the device and once I click on it, the callback is triggered that does the required logic and sends the data to firebase.
However, my problem with this approach is that the user really needs to click on the notification in order to trigger the callback. It looks like the firebase_messaging plugin only supports notification-messages and not pure data ones. So I am kind of restricted to only executing my code once a notification is clicked on.
My question is, is there a way to trigger my code in the Flutter app without the notification?
I would love to be able to send some data requests to the device and receive some response back (or at least trigger some code). Maybe there is another plugin or some hacks somebody is aware of?
Thank you in advance.
Upvotes: 3
Views: 3683
Reputation: 3175
Firebase Messaging plugin supports both Notification and Data Messages. However, the delivery and processing of the messages is not the same for both types.
Data messages are what you need to use if you want the app to process something without a notification appearing for the user to act on. So, for example, if you are using cloud functions to send your FCM messages you simply omit the 'notification' block of the payload and only send the data block eg.
const payload = {
// notification: {
// title_loc_key: "notification_title_string",
// body_loc_key: "notification_message_chat_string",
// badge: "1",
// sound: "default",
// },
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
rcpntId: recipientId,
sndrId: senderId,
// sound: "default",
type: "chat",
}
The problem is that it appears your app will only be able to process that Data message if it is active ie. in the foreground.
The documentation says this:
However, I have only been able to make it work with onMessage when the app is in the foreground not when in background, which makes it fairly useless, and as you see app terminated is not supported on Android. I have not tested it on iOS yet - I am sure there will be more issues there :-)
Upvotes: 4
Reputation: 343
Firebase has OnMessageReceived method that run every time a notification is received. you can send a message to app with out notification and just put json data then parse it on that method. if need more details you can this good article.
Upvotes: -1