Flutter - myBackgroundMessageHandler never called in push notification - flutter

I am using firebase_messaging library with firebase for push notification in flutter. The documentation says that myBackgroundMessageHandler is called when the app is in background. But it has not happened.
I would like to format the title and the body of the notification before it is shown. I am getting notifications when the app is in the background but that particular method is not called. Currently it just shows the exact text sent by backend in title and body.
Is there something I have to do to enable(?) this method? I have it in my fcm.configure method:
_fcm.configure(
onBackgroundMessage: myBackgroundMessageHandler,
)
static Future<Map<String, dynamic>> myBackgroundMessageHandler(Map<dynamic, dynamic> message) async {
//some code here
return message;
}
Thanks in advance!

I had the same problem.
onBackgroundMessage apparently gets triggered only for data-messages.
See the difference here:
https://firebase.google.com/docs/cloud-messaging/concept-options
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
These are handled by the FCM SDK automatically.
Data messages, which
are handled by the client app.
In my case I had to remove the notification (subject, body etc), but also styling like bigpicture completely. Try sending only a data object.

Related

How to wait for a Flutter app to open before navigating to a particular page by clicking a scheduled notification?

I'm working on a Flutter project where I had to set a scheduled local notification that would fire everyday at 10 am. And, the user should be taken to a certain page of the app upon clicking the notification.
I've completed this part and the functionality works fine when the app is open and the user clicks on the notification.
But the problem arises when the app is closed and the notification is fired and the user clicks on the notification. It doesn't take the user to that certain page. I tried to modify the click handling function by making it an async function. But it doesn't work accordingly.
Can someone suggest me what to do now?
Create a notification handler.
Initialize your notification channel with high importance
Just before your app runs, before runApp(yourApp), create call a foregroundMessageHandler that you have initialized before the void main(){}
the foregroundMessageHandler should be a *TopMost initialization.
in the foregroundMessageHandler function, define the navigation behaviour
I don't know which library you are using for local notifcations. but if you are using flutter_local_notifications library check the link: https://pub.dev/packages/flutter_local_notifications
, you can check the data recived in selectNotification method and redirect the user to required page
void selectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
await Navigator.push(
context,
MaterialPageRoute<void>(builder: (context) => SecondScreen(payload)),
);
}
other libraries should have same functionality.

Flutter push notifications app background : firebase_messaging

I want to display a push notification when my app is in the background.
I am using the flutter_local_notifications package and the firebase_messaging package.
Push notifications are working great with firebase_messaging and when my app is the background.
However, the below method :
// The following handler is called, when App is in the background.
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
is not called if a RemoteNotification object is passed through the RemoteMessage :
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('message from background handler');
print("Handling a background message: ${message.messageId}");
// If this is null, then this handler method is called
RemoteNotification remoteNotification = message.notification;
showNotificationFromData(message.data);
}
Therefore, i have to pass the message.data object which is a Map<String, dynamic>.
My problem is that i don't receive push notification anymore when this firebaseMessagingBackgroundHandler handler method is called.
So i have been trying to use the flutter_local_notification package to display a push notification but as it is said, it's "local" so it's working fine in the Foreground but apparently not in the background (the same code, with the same data is not showing in the background as a push notification).
Question :
I need to call the firebaseMessagingBackgroundHandler handler to handle events in my app. So i can do to still have push notifications when my app is in the background please ?
Thanks
Okay i have found the solution. It was to set the content_available value to True in the server-side code.
To replace the firebase_messaging.Notification item, we need to override to the android and apns configs.
This looks like this in python :
apnsConfig = messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
content_available=True,
)
),
)
I am now receiving push notifications and call the background handler inside the dart code.
I know this bit too late but anyone trying to get custom notification using firebase then here is the solution
For Android (Flutter)
On Android, notification messages are sent to Notification Channels which are used to control how a notification is delivered. The default FCM channel used is hidden from users, however provides a "default" importance level. Heads up notifications require a "max" importance level.
This means that we need to first create a new channel with a maximum importance level & then assign incoming FCM notifications to this channel. we can take advantage of the flutter_local_notifications package
Add the flutter_local_notifications package to your local project.
Create a new AndroidNotificationChannel instance:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
(keep in mind this is example code but you can custimize notification through notification channel code)
Create the channel on the device:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
Once created, we can now update FCM to use our own channel rather
than the default FCM one. To do this, open the
android/app/src/main/AndroidManifest.xml file for your
FlutterProject project. Add the following meta-data schema within
the application component:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
now whenever you send notification specify your channel id and your app automatically show your custom notification (through api or firebase console)
{
"message":{
"token":"token_1",
"android_channel_id":"high_importance_channel" //specify channel id here
"data":{},
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message!",
}
}
}
as for ios they don't support notification channel i don't have necessary resources and info how things works if you guys know comment below will be much appriciated

Flutter firebase messaging v9.0.0 - Not firing events

I have an app that was using firebase messaging 7.0.3 and all worked perfectly. But when I migrated to firebase messaging 9.0.0, the push notifications are not being handled.
I know that the app is correctly linked to firebase and cloud messaging because in background I see the push notification comming, the problem is when I click that notification the app don't handle this event. Also, when the app is in foreground, the event of receiving the notification is not being triggered.
Specifically, the functions FirebaseMessaging.onMessage and FirebaseMessaging.onMessageOpenedApp are not working. My code is:
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("notification: message");
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("notification: resume");
});
The prints are never called, and if I put some more code inside isn't executed too.
I also call FirebaseMessaging.getToken and I can get the token, plus when the app is in background I get the push notification, so is not a link problem with firebase. In the logs, when I receive the push I can see a message saying: Broadcast received for message. So I assumed that the push is arriving in all the occasions, and I am doing something wrong in the code.
I tested all cases in Android and iOS physical devices.
Someone know why this occurs?
I came across this exact same issue after migrating and upgrading to 9.0.0.
There is a minor issue with 9.0.0 plugin.
The fix can be found here:
https://github.com/FirebaseExtended/flutterfire/issues/4949
To cut a long story short, if you navigate to the definition of the factory
RemoteMessage.fromMap() Using Cmd+Click or Ctrl+Click while hovering over the RemoteMessage class with the mouse), in the return statement, change contentAvailable: map['contentAvailable'], to contentAvailable: map['contentAvailable']??false.
Notifications are now working for me again now.
This should work for you until the plugin is fixed.

Is there any way to check whether user has recieved the notification recieved from firebase messaging

I am building a one to one chat app using flutter at client side and firebase as my backend. I use firebase messaging for sending notification to the reciever . I want to show single tick for sent double ticks for delivered and blue ticks for seen same as whatsapp.Is there any way that it could be achieved with firebase and flutter
You can do something like this:
Once the message is sent to firebase, you set the single tick.
When the message is received, set the double tick
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
// here you set the double tick
},
);
In our Widget showing the message you set the blue ticks

FCM: How to bring the app from background to foreground?

I have an app in which I am using FCM (Firebase Cloud Messaging).
As given in the README, I've managed to get it all working and the event handlers for onResume, onMessage, onLaunch are all firing properly.
The onBackgroundMessage which I think is supposed to run whenever the notifications appear while the app is inactive, needs a top-level dart function.
I need this function to render a particular widget, say, IncomingCall. But this being a top-level function, the best I could think of, is to invoke the main():
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
main();
}
which then calls runApp(IncomingCall()) but even this is not working.
So how does one render a widget in Dart when the app is inactive?