Local notification flutter

Sarthak Solanki picture Sarthak Solanki · Jan 16, 2019 · Viewed 11.9k times · Source

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?

Answer

shadowsheep picture shadowsheep · Jan 16, 2019

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