I am using Firebase Cloud Messaging (FCM) to send push notifications in my Flutter app. Notifications work fine when the app is in the foreground and background. However, on my Oppo device, notifications do not appear when the app is in a terminated state (closed).
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Main function
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
// Initialize local notifications
await initializeLocalNotifications();
// Get Firebase Messaging instance
FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
// Get FCM Token
firebaseMessaging.getToken().then((token) {
print("FCM Token: $token");
});
// Handle notification taps
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("User tapped on the notification!");
});
// Handle incoming messages while the app is in the foreground
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("Received message: ${message.notification?.body}");
// Call function to show notification
showLocalNotification(
message.notification?.title ?? 'Notification',
message.notification?.body ?? '',
);
});
runApp(const Work360App());
}
Future<void> initializeLocalNotifications() async {
const initializationSettingsAndroid =
AndroidInitializationSettings('ic_launcher');
const initializationSettingsIOS = DarwinInitializationSettings();
const initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
if (Platform.isAndroid) {
const androidNotificationChannel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.max,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidNotificationChannel);
} else if (Platform.isIOS) {
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(alert: true, badge: true, sound: true);
}
}
Future<void> showLocalNotification(String title, String body) async {
const androidNotificationDetails = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
channelDescription: 'This channel is used for important notifications.',
importance: Importance.max,
priority: Priority.high,
);
const iOSNotificationDetails = DarwinNotificationDetails();
const notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: iOSNotificationDetails,
);
await flutterLocalNotificationsPlugin.show(0, title, body, notificationDetails);
}
What I have tried:
Ensured that Firebase is initialized correctly in the main function.
Checked notification permissions for the app in the phone’s settings.
Verified that notifications work fine in both foreground and background states.
Added the necessary notification channel for Android as shown in the code.
Checked for any app-specific battery optimization settings that might be affecting notification delivery.
What works:
Notifications are received when the app is in the foreground and background on the Oppo device.
3
Make sure you add FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
this before app run. Also, you need to make sure this _firebaseMessagingBackgroundHandler
must be a top level function with @pragma('vm:entry-point’)
this annotation, as this would like to invoke app in termination state.
For more info – refer official documentation or you can also see the example listed here https://pub.dev/packages/firebase_messaging/example
1