Skip to content

Instantly share code, notes, and snippets.

@taciomedeiros
Created August 16, 2020 12:11
Show Gist options
  • Save taciomedeiros/50472cf94c742befba720853e9d598b6 to your computer and use it in GitHub Desktop.
Save taciomedeiros/50472cf94c742befba720853e9d598b6 to your computer and use it in GitHub Desktop.
It is an example of how to use flutterLocalnotifications to schedule a lot of notifications with isolateHandler
final IsolateHandler isolateHandler = IsolateHandler();
void scheduleNotificationsIsolate(String _reminders) async {
await new Future.delayed(new Duration(milliseconds: 500));
// ... (describe settings)
flutterLocalNotificationsPlugin.initialize(
settings,
onSelectNotification: onSelectNotification,
);
await flutterLocalNotificationsPlugin.cancelAll();
List<dynamic> _remindersParsed = jsonDecode(_reminders);
for (// iterate over your entities to show the message) {
int generatedId = id ?? random.nextInt(1000000000);
await flutterLocalNotificationsPlugin.schedule(
generatedId,
title,
message,
scheduledNotificationDateTime,
platformSpecifics,
payload: payload,
);
}
killCurrentScheduleNotifications();
}
startScheduleNotifications(String _remindersAsString) {
killCurrentScheduleNotifications();
isolateHandler.spawn<String>(
entryPoint,
name: "scheduleNotifications",
onReceive: scheduleNotificationsIsolate,
onInitialized: () => isolateHandler.send(
_remindersAsString,
to: "scheduleNotifications",
),
);
}
void killCurrentScheduleNotifications() {
if (isolateHandler.isolates.containsKey('scheduleNotifications'))
isolateHandler.kill('scheduleNotifications');
}
void entryPoint(Map<String, dynamic> context) {
final messenger = HandledIsolate.initialize(context);
messenger.listen((message) {
messenger.send(message);
});
}
/// At my production code i create some abstractions above flutterLocalNotifications:
/// 1 - a class called NotificationManager is the object that i use in the application it is a singleton
/// and it creates a specific provider that i initialized just once int the application. (Because Isolate its a separeted
/// context i have to initialize it them to every time i call)
/// 2- an interface called NotificationProvider and your implementation where i put the common methods
/// 3 - The specific implementations NotificationProviderIOS and NotificationProviderAndroid that extends
/// NotificationProviderImpl where i put the configs necessary to create an specific platform notification.
/// An Isolate should be a simple function created directly inside a file, declared outside of a class.
/// In other plugins that i tested i remember to declare the methodChannel used by flutterLocalNotifications.
/// This is a thing that i didn't have to do with flutter isolate.
/// the parameter passed to an isolate should be compound only by primitives. I serialize it as string to decode as JSON.
// I realized tests with a few thousands of notifications and performance was good, the phone slows down a bit.
/// A good strategie is schedule the minimun that is needed(a week, a month) an create routines in your application to schedule
/// the next ones when user hits some action.
// I hope that this content should help. If you get in trouble with this feel free to contact me by e-mail.
@flipflop133
Copy link

Hey I just posted a question on Stack Overflow (https://stackoverflow.com/questions/69955481/creating-multiple-flutter-nofitications-with-flutter-local-notification) to which I think you could answer, would you mind sharing more code on how you managed to create scheduled notifications on a second thread with flutter_local_notification?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment