Reputation: 3730
I have built a todo list app where users can schedule notifications based on certain dates and hours, the notifications show up without problems when the app is in the foreground but when the app is in the background or the app is closed, they do not show up, and I cannot understand why, any reason why, Thank you.
late FlutterLocalNotificationsPlugin _localNotificationsPlugin;
List<Task> _list = [];
late MoorController moorController;
@override
void initState() {
super.initState();
moorController = Get.put(MoorController());
createNotification();
//showNotification();
}
void createNotification(){
_localNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid = const AndroidInitializationSettings("@mipmap/ic_launcher");
var initializationSettingsIOs = const IOSInitializationSettings();
var initSettings = InitializationSettings(android: initializationSettingsAndroid,iOS: initializationSettingsIOs);
_localNotificationsPlugin.initialize(initSettings);
}
void showNotification() async {
var android = const AndroidNotificationDetails(
Utils.CHANNEL_ID,
Utils.CHANNEL_NAME,
channelDescription: Utils.DESCRIPTION,
priority: Priority.high,
importance: Importance.max);
var iOS = const IOSNotificationDetails();
var platform = NotificationDetails(android: android,iOS: iOS);
for (var element in moorController.tasks) {
if(element.date == getCurrentDate() && element.time == getCurrentTime()){
await _localNotificationsPlugin.schedule(
0,element.date,element.time,DateTime.now(),platform, payload : 'Welcome to todo app',androidAllowWhileIdle: true);
//0, element.date,element.time,platform, payload: 'Simple Payload'
}
}
}
Upvotes: 1
Views: 9006
Reputation: 810
I believe you need an instance of the TZDateTime
from the timezone
package in order to schedule a notification.
This is a snippet from the documentation:
Starting in version 2.0 of the plugin, scheduling notifications now requires developers to specify a date and time relative to a specific time zone. This is to solve issues with daylight savings that existed in the schedule method that is now deprecated. A new zonedSchedule
method is provided that expects an instance TZDateTime
class provided by the timezone
package. As the flutter_local_notifications
plugin already depends on the timezone package, it's not necessary for developers to add the timezone
package as a direct dependency. In other words, the timezone
package will be a transitive dependency after you add the flutter_local_notifications
plugin as a dependency in your application.
So now in your code, import the package
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
Initialize the timezone database:
tz.initializeTimeZones();
Once the time zone database has been initialised, developers may optionally want to set a default local location/time zone
tz.setLocalLocation(tz.getLocation(timeZoneName));
Now you should be able to call it like this:
await _localNotificationsPlugin.zonedSchedule(
0,
'title',
'body',
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
platform,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
More info can be found in the docs here
Let us know if this helps!
Upvotes: 4
Reputation: 3730
Did you add these permissions in your AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Inside the application section:
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"><intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action></intent-filter></receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
Upvotes: 2
Reputation: 2235
You should check if your payload contains the click_action
with FLUTTER_NOTIFICATION_CLICK
:
{
"notification": {
"body": "this is a body",
"title": "this is a title",
},
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"sound": "default",
"status": "done",
"screen": "screenA",
},
"to": "<FCM TOKEN>"
}'
Upvotes: 0