user2233706
user2233706

Reputation: 7225

Display scheduled notification conditionally

How can I use flutter_local_notifications to display a scheduled notification based on a condition. If the conditions aren't met, I don't want to display the notification. It seems when you schedule a notification, it is displayed unconditionally.

In the condition, I want to check some state on the embedded datastore, Sembast, I maintain in my app. My app interacts with the datastore directly. So in the condition I would poll the datastore, possibly using the existing code in my app. Not sure if this is possible because the notification could be scheduled when my app isn't running.

For example, I schedule a notification to occur everyday at 9:00 AM. At 9:00 AM I want some code to run to check if the condition is true, and display the notification only then. This should happen even if the app is closed.

Upvotes: 2

Views: 1148

Answers (2)

user2233706
user2233706

Reputation: 7225

I used a combination of background_fetch and flutter_local_notifications to display a notification conditionally and when the app is not running.

background_fetch only allows you to run code in the background at an interval greater than 15min and you can't schedule it at a specific time. Not ideal, but I can work with it. The important thing is that on Android, you get events even if the app is closed (called 'headless events'). On iOS, you only get headless events if the app was not terminated by the user. This is because of iOS limitations/features.

In the background_fetch event handler, both headless and non headless, I had the following code to generate the notification:

  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'your channel id', 'your channel name', 'your channel description',
      importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
  var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
      0, 'my event', 'my event', platformChannelSpecifics,
      payload: 'item x');

Upvotes: 2

That Guy
That Guy

Reputation: 234

You would need to write a firebase cloud function. This can be done using NodeJs or other backend framework.

Upvotes: 0

Related Questions