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.
2
Answers
1. Battery Optimization Settings:
Go to Settings > Battery > Battery Optimization.
Find your app and ensure that it is not optimized. This will allow the app to receive notifications even when it’s terminated.
2. Auto-Start Permissions:
Oppo devices often require apps to be allowed to start automatically.
Go to Settings > App Management > Auto-Start Management.
Find your app and enable auto-start.
3. Background Permissions:
Ensure that your app has permission to run in the background.
Go to Settings > Privacy > Background Running Apps.
Ensure that your app is allowed to run in the background.
4. Push Notification Settings:
On some Oppo devices, there might be specific settings for push notifications.
Go to Settings > Notifications & Status Bar > Manage Notifications.
Ensure that notifications are enabled for your app.
5. Check for Additional Device-Specific Settings:
Some Oppo devices have additional security or battery settings that could block notifications. Make sure to review the device’s settings to ensure nothing is blocking the app from running when terminated.
6. FCM Implementation:
Consider adding a custom data message alongside the notification. This ensures that your app receives the data, which can be processed in the background even when the app is terminated.
Testing:
After making these changes, restart your device and test to see if notifications are received when the app is terminated. If they are still not working, consider using a different testing device or emulator to ensure that the issue is device-specific.
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