Skip to content

Instantly share code, notes, and snippets.

@jesusrp98
Created April 16, 2019 19:06
Show Gist options
  • Save jesusrp98/7f19984788f10c243107b2c5bc51c4e1 to your computer and use it in GitHub Desktop.
Save jesusrp98/7f19984788f10c243107b2c5bc51c4e1 to your computer and use it in GitHub Desktop.
Schedules notifications if neccesary
Future initNotifications(BuildContext context) async {
bool updateNotifications;
final SharedPreferences prefs = await SharedPreferences.getInstance();
// Checks if is necessary to update scheduled notifications
try {
updateNotifications =
prefs.getString('notifications.launches.upcoming') !=
launch.launchDate.toIso8601String();
} catch (e) {
updateNotifications = true;
}
// Update notifications if necessary
if (updateNotifications) {
// T - 1 day notification
await _scheduleNotification(
id: 0,
context: context,
time: FlutterI18n.translate(
context,
'spacex.notifications.launches.time_tomorrow',
),
subtract: Duration(days: 1),
);
// Update storaged launch date
prefs.setString(
'notifications.launches.upcoming',
launch.launchDate.toIso8601String(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment