Flutter中flutter_local_notifications的zonedSchedule通知在安卓模拟器不触发
Flutter Android应用定时通知不触发问题排查
我在Android API33模拟器上运行Flutter应用,使用flutter_local_notifications包。普通通知(「Show plain notification with payload」功能)可正常显示,但点击「Schedule notification to appear in 5 seconds」触发的定时通知无任何反应,且无报错信息。
已调整的定时通知代码
为避免额外权限,我将androidScheduleMode改为inexact模式,定时通知代码如下:
Future<void> _zonedScheduleNotification() async { final nowDttm = tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)); print('Scheduled: $nowDttm'); await flutterLocalNotificationsPlugin.zonedSchedule( 0, 'scheduled title', 'scheduled body', nowDttm, const NotificationDetails( android: AndroidNotificationDetails( 'your channel id', 'your channel name', channelDescription: 'your channel description')), androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle, //changed from exact uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime); }
正常工作的普通通知代码
普通通知可正常显示,其实现代码如下:
Future<void> _showNotification() async { const AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails('your channel id', 'your channel name', channelDescription: 'your channel description', importance: Importance.max, priority: Priority.high, ticker: 'ticker'); const NotificationDetails notificationDetails = NotificationDetails(android: androidNotificationDetails); await flutterLocalNotificationsPlugin.show( id++, 'plain title', 'plain body', notificationDetails, payload: 'item x'); }
已确认的配置项
- 应用已获取通知权限(普通通知正常显示可佐证)
- 时区初始化正确,代码如下:
tz.initializeTimeZones(); final String timeZoneName = await FlutterTimezone.getLocalTimezone(); tz.setLocalLocation(tz.getLocation(timeZoneName));
AndroidManifest.xml配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <application android:label="notifications" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:taskAffinity="" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the user while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> </manifest>
请问我遗漏了什么配置?该如何排查此定时通知不触发的问题?
内容的提问来源于stack exchange,提问作者under
相关产品推荐
相关产品推荐

