5

can anyone show me with code that how can I schedule the notification in flutter using local notification plugin. Tried the example in git repository buy it doesn't work for me, although a normal notification is working but how can i schedule it for a specific time like a reminder?

Sarthak Solanki
  • 468
  • 1
  • 6
  • 18
  • 1
    See my answer and try yourself out. Then if you still are not able to schedule a notification, update your post with the code you have used in your project and if you are not able to schedule the notification on both Android an iOS or only on one platform. It's important to post a minimal code you have tried so that people can help you base on your code. And this make also your question compliant with SO best practice. – shadowsheep Jan 16 '19 at 11:24

2 Answers2

5

From the plugin sample code if you wanna schedule a notification you have to use a code like that:

/// Schedules a notification that specifies a different icon, sound and vibration pattern
  Future _scheduleNotification() async {
    var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));
    var vibrationPattern = new Int64List(4);
    vibrationPattern[0] = 0;
    vibrationPattern[1] = 1000;
    vibrationPattern[2] = 5000;
    vibrationPattern[3] = 2000;

    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        'your other channel id',
        'your other channel name',
        'your other channel description',
        icon: 'secondary_icon',
        sound: 'slow_spring_board',
        largeIcon: 'sample_large_icon',
        largeIconBitmapSource: BitmapSource.Drawable,
        vibrationPattern: vibrationPattern,
        color: const Color.fromARGB(255, 255, 0, 0));
    var iOSPlatformChannelSpecifics =
        new IOSNotificationDetails(sound: "slow_spring_board.aiff");
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.schedule(
        0,
        'scheduled title',
        'scheduled body',
        scheduledNotificationDateTime,
        platformChannelSpecifics);
  }

The part you must focus is that:

// Schedule a notification in 5 secs from now
var scheduledNotificationDateTime =
        new DateTime.now().add(new Duration(seconds: 5));

I suggest you to clone the plugin repo and try its example if you are not sure about the native projects setup to do in order to have your notifications to be shown.

/// IMPORTANT: running the following code on its own won't work as there is setup required for each platform head project.

/// Please download the complete example app from the GitHub repository where all the setup has been done

shadowsheep
  • 14,048
  • 3
  • 67
  • 77
  • It's working when we set low period of time like 120 seconds but when we set notification for 2000 seconds(30 minutes). Then it's not working. Is their any problem in scheduling for long time in this method? – Abdullah Khan Apr 12 '20 at 07:03
  • flutterlocalnotificationsplugin sound not working ios please healp me – Hitesh sapra Apr 26 '21 at 12:12
-1
const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails('repeating channel id','repeating channel name', 'repeating description');
const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title', 'repeating body', RepeatInterval.everyMinute, platformChannelSpecifics, androidAllowWhileIdle: true);

you can use periodicallyShow flutter_local_notifications