5 Steps to Send Notification in Flutter with Specified Time

Screenshot-2023-03-06-at-06.02.40

Send Notification in flutter – Flutter Local Notifications lets you deliver local alerts to app users using Flutter. Local alerts are app-sent without a server or internet connection. They may give app users reminders, alerts, and other notifications even while the app isn’t functioning. Flutter Local Notifications offers a simple API for scheduling, showing, and managing user notifications. It supports Android and iOS with platform-specific notifications. Send Notification in Flutter lets you plan or trigger alerts depending on app use. Notification icons, sounds, and vibration patterns may be customized. Flutter Local Notifications makes your Flutter app more engaging and helpful by introducing local notification capability.

Send Notification in

To Send Notification in Flutter application daily at a specified time, you can use the flutter_local_notifications package.

Example implementation Send Notification in Flutter:

  1. Add the flutter_local_notifications package to your pubspec.yaml file:
dependencies:
  flutter_local_notifications: ^5.0.0+2
  1. Initialize the notification plugin in your main.dart file:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('app_icon');

  final InitializationSettings initializationSettings =
      InitializationSettings(
    android: initializationSettingsAndroid,
  );

  await flutterLocalNotificationsPlugin.initialize(initializationSettings);

  runApp(MyApp());
}
  1. Create a function to schedule the notification:
Future<void> scheduleNotification() async {
  const AndroidNotificationDetails androidPlatformChannelSpecifics =
      AndroidNotificationDetails(
    'channel id',
    'channel name',
    'channel description',
    importance: Importance.max,
    priority: Priority.high,
  );

  const NotificationDetails platformChannelSpecifics =
      NotificationDetails(android: androidPlatformChannelSpecifics);

  await flutterLocalNotificationsPlugin.zonedSchedule(
    0,
    'Title',
    'Body',
    _nextInstanceOfNotificationTime(),
    platformChannelSpecifics,
    androidAllowWhileIdle: true,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
  );
}

This method will send a notice at the next available instance of the given time using the zonedSchedule technique. If you need the next occurrence of a certain time, just use the _nextInstanceOfNotificationTime method.

  1. Define the function to get the next occurrence of the provided time:
TZDateTime _nextInstanceOfNotificationTime() {
  final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
  tz.TZDateTime scheduledDate =
      tz.TZDateTime(tz.local, now.year, now.month, now.day, 10, 0);

  if (scheduledDate.isBefore(now)) {
    scheduledDate = scheduledDate.add(const Duration(days: 1));
  }

  return scheduledDate;
}

This function returns the next occurrence of 10:00 AM local time. You can change this function to always return the next instance of.

  1. Call the scheduleNotification function from your app:
classMyApp extendsStatelessWidget {
  @override
  Widget build(BuildContext context) {
    scheduleNotification();

    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

This will schedule a Send Notification in Flutter when the app is launched.

Note: To test the notification, you need to run the app on a physical device or emulator that supports notifications.

here’s a complete example code that Send Notification in Flutter daily at 10:00 AM:

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

void main() {
  runApp(MyApp());
}

classMyApp extendsStatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Local Notifications Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Local Notifications Demo'),
    );
  }
}

classMyHomePage extendsStatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class_MyHomePageState extendsState<MyHomePage> {
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  @override
  void initState() {
    super.initState();

    // Initialize the notification plugin
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('app_icon');

    final InitializationSettings initializationSettings =
        InitializationSettings(
      android: initializationSettingsAndroid,
    );

    flutterLocalNotificationsPlugin.initialize(initializationSettings);

    // Schedule the daily notification
    _scheduleDailyNotification();
  }

  Future<void> _scheduleDailyNotification() async {
    // Get the next instance of 10:00 AM
    tz.TZDateTime scheduledDate = _nextInstanceOfTenAM();

    // Define the notification details
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails(
      'daily notification channel id',
      'daily notification channel name',
      'daily notification description',
      importance: Importance.max,
      priority: Priority.high,
    );

    const NotificationDetails platformChannelSpecifics =
        NotificationDetails(android: androidPlatformChannelSpecifics);

    // Schedule the notification
    await flutterLocalNotificationsPlugin.zonedSchedule(
      0,
      'Title',
      'Body',
      scheduledDate,
      platformChannelSpecifics,
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
    );
  }

  tz.TZDateTime _nextInstanceOfTenAM() {
    final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
    tz.TZDateTime scheduledDate =
        tz.TZDateTime(tz.local, now.year, now.month, now.day, 10, 0);

    if (scheduledDate.isBefore(now)) {
      scheduledDate = scheduledDate.add(const Duration(days: 1));
    }

    return scheduledDate;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(
          'Daily notification scheduled at 10:00 AM',
          style: TextStyle(fontSize: 18),
        ),
      ),
    );
  }
}

Add these import lines at the start of your file:

import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

Also, add the following lines of code to your main function to initialize the timezone database:

voidmain() async {
  tz.initializeTimeZones();
  runApp(MyApp());
}

This should schedule a notification daily at 10:00 AM in the user’s local timezone.

Conclusion

Send Notification in Flutter are easy to build in Flutter using FCM and can dramatically improve your app’s UX by keeping users informed and engaged. read too How to Create Loading Dialog in Flutter

Screenshot-2023-03-06-at-06.02.40
Previous Post

No more post

Screenshot-2023-03-06-at-06.02.40
Next Post

No more post

Hello, I'm Cakra. I'm currently occupied with developing an application using the Flutter framework. Additionally, I'm also working on writing some articles related to it.