Flutter应用关闭时无法接收本地通知的问题解决咨询
Hey there, it sounds like you're hitting a common issue with Flutter local notifications—they work great in the foreground, but vanish once the app is closed. The root cause here is that when your app is in the background (or killed), the Dart runtime gets suspended by the system, so your Flutter-side logic can't fire notifications anymore. Let's fix this with proper Android native setup, building on the Receiver snippet you started:
1. Complete Your Broadcast Receiver Configuration in AndroidManifest.xml
Your Receiver code was cut off, so let's make sure it's fully set up to catch the necessary intents, even when the app isn't running. Here's the full configuration you need:
<receiver android:name=".notification.NotificationReceiver" android:exported="true"> <intent-filter> <!-- Match the custom actions you're using to trigger notifications --> <action android:name="io.flutter.app.notification.RECEIVE"/> <action android:name="io.flutter.app.notification.DELETE"/> <!-- Add this if you want notifications to resume after device reboot --> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
Don't forget to add the required permissions above the <application> tag:
<!-- Allow receiving boot completed broadcasts --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- For precise alarms (Android 12+) --> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> <uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
2. Replace Flutter Timer with Android Native Scheduling
If you're using Timer.periodic in Flutter to trigger the 1-minute checks, that won't work in the background—Android will pause or kill that timer. Instead, use WorkManager or AlarmManager to handle periodic tasks natively:
- WorkManager: Best for flexible, system-managed periodic tasks. Create a
Workerclass in Android that sends the notification, and schedule it to run every minute. The system will handle waking up your app even if it's closed. - AlarmManager: Use this for precise timing. Pair it with your
NotificationReceiver—set an alarm to fire every minute, and your receiver'sonReceive()method will trigger the notification.
3. Verify Notification Channel Setup (Android 8.0+)
Android Oreo and above require a notification channel for notifications to show up. Make sure you're creating one in your Flutter initialization code:
const AndroidNotificationChannel channel = AndroidNotificationChannel( 'your_unique_channel_id', 'Your Notification Channel', description: 'Channel for your periodic notifications', importance: Importance.high, // Critical for background notifications to appear ); await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
4. Check Device-Specific Restrictions
Many Android manufacturers (Xiaomi, Huawei, OPPO, etc.) have aggressive battery optimization that blocks background tasks. To ensure your notifications fire:
- Add your app to the battery optimization whitelist in Settings > Battery > Battery Optimization.
- Enable Auto-Start and Background Activity permissions for your app (look in Settings > Apps > Your App > Permissions).
内容的提问来源于stack exchange,提问作者camilleB

