'Flutter FCM data only payload notification is not received when app is terminated
I'm using firebase_messaging: ^11.2.10 and flutter_local_notifications: ^9.4.0 to show notifications. I am sending data only notifications without notification title or body. Most of the time it works, but when the app is terminated and not used for a longer period, notifications won't show until you wake or unlock the phone.
Here is the case:
I subscribe to the Firebase background messages.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(MessagingService.subscribeToFCMBackground);
await NotificationService().init();
runApp(const MyApp());
);
}
Inside my MessagingService I get the payload data and there it shows the notification based on the value under the "type" key. Looks like this:
static Future<void> subscribeToFCMBackground(RemoteMessage message) async {
await Firebase.initializeApp();
NotificationService().showNotificationByType(_getNotificationTypeFromMessage(message));
}
static NotificationType _getNotificationTypeFromMessage(final RemoteMessage message) {
final String notificationType = message.data['type'] as String;
return notificationTypeEnumMap[notificationType]!;
}
Inside the NotificationService I create notification channel, place it inside of NotificationDetails object and pass it to showNotification method. Here is an example of android channel:
final AndroidNotificationDetails _androidNotificationDetails = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Channel',
channelDescription: 'Very important notifications',
playSound: true,
sound: RawResourceAndroidNotificationSound(soundFileName),
priority: Priority.max,
importance: Importance.max,
);
...
final NotificationDetails platformChannelSpecifics = NotificationDetails(
android: _androidNotificationDetails,
iOS: _iOSNotificationDetails,
);
...
await flutterLocalNotificationsPlugin.show(
0,
'Notification title',
'Notification body',
platformChannelSpecifics,
);
Also I added this to the Android Manifest file:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
Do you guys have any idea how to solve this problem? I would really appreciate any advice or a solution. Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
