Scheduled Flutter Local Notifications not showing even after permission granted

253 views Asked by At

I'm working on a Flutter application where I'm trying to schedule and show local notifications using the flutter_local_notifications package. I have ensured that the required permissions are granted, but the notifications are still not appearing.

I have tried normal notificaiton and they are showing but when I try to schedule notifications nothing shows up.

I've checked and confirmed notification permissions. Checked logs for any errors or warnings related to notifications.

Code:

I initialize the time zones and set up the notifications as follows: main.dart

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  NotificationService().initNotification();
  tz.initializeTimeZones();

here is the notification_settings.dart

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  Future<void> showTestNotification() async {
    // Check if notifications are granted
    final bool? isGranted = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.areNotificationsEnabled();

    if (!isGranted!) {
      print("Notifications are not granted");
      AppSettings.openAppSettings();
      return;
    }

    print("Notifications are granted");

    const AndroidNotificationDetails androidPlatformChannelSpecifics =
        AndroidNotificationDetails('your_channel_id', 'your_channel_name', importance: Importance.max, priority: Priority.high, showWhen: false);

    const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);

    await flutterLocalNotificationsPlugin.zonedSchedule(
      0,
      'Test Notification',
      'This is a test notification after 5 seconds',
      tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
      platformChannelSpecifics,
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
    );
  }

here is the button to trigger

OutlinedButton(
                  onPressed: showTestNotification,
                  child: Text("Schedule 5-sec Notification"),
                ),

I am not sure why it is not working however when I try the below code it works:

                OutlinedButton(
                  onPressed: () {
                    _notificationService.showNotification(
                      id: 1,
                      title: 'Hi',
                      body: 'This is a test notification.',
                    );
                  },

notification service:

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin();

  Future<void> initNotification() async {
    AndroidInitializationSettings initializationSettingsAndroid = const AndroidInitializationSettings('flutter');

    var initializationSettingsIOS = DarwinInitializationSettings(
        requestAlertPermission: true, requestBadgePermission: true, requestSoundPermission: true, onDidReceiveLocalNotification: (int id, String? title, String? body, String? payload) async {});

    var initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    await notificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) async {});
  }

  notificationDetails() {
    return const NotificationDetails(
        android: AndroidNotificationDetails(
          'channelId',
          'channelName',
          importance: Importance.max,
        ),
        iOS: DarwinNotificationDetails());
  }

  Future showNotification({int id = 0, String? title, String? body, String? payLoad}) async {
    return notificationsPlugin.show(id, title, body, await notificationDetails());
  }

0

There are 0 answers