I am using Flutter flutter_local_notifications
package in an app targeting Android.
I've set up the example app and am running it in Android emulator (API33).
When I use Show plain notification with payload
it works, but when I click Schedule notification to appear in 5 seconds
, nothing! The notification does not appear. There is no error either.
I've changed the androidScheduleMode
to use "inexact" mode so I don't need additional permissions:
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);
}
The notification that does work is using this method:
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');
}
App has notification permission as shown by the other type of notification working. Other's had problems with timezones but this is initialized correctly like so:
tz.initializeTimeZones();
final String timeZoneName = await FlutterTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(timeZoneName));
My AndroidManifest.xml
is below (as requested):
<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>
Full project is on GitHub.
What am I missing here? How do I troubleshoot?
As mentioned in my comment, you did not follow the integration guide. Part of it, besides adding the permissions and changing things in Gradle, is also to add receivers in your AndroidManifest.xml file so scheduled notifications work.
From the guide:
To schedule notifications the following changes are needed Specify the appropriate permissions between the tags. : this is required so the plugin can known when the device is rebooted. This is required so that the plugin can reschedule notifications upon a reboot If the app requires scheduling notifications with exact timings (aka exact alarms), there are two options since Android 14 brought about behavioural changes (see here for more details) specify and call the requestExactAlarmsPermission() exposed by the AndroidFlutterNotificationsPlugin class so that the user can grant the permission via the app or specify . Users will not be prompted to grant permission, however as per the official Android documentation on the USE_EXACT_ALARM permission (refer to here and here), this requires the app to target Android 13 (API level 33) or higher and could be subject to approval and auditing by the app store(s) used to publish theapp Specify the following between the tags so that the plugin can actually show the scheduled notification(s)
Add the following to your AndroidManifest.xml file and scheduled notifications should work:
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>