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.