Home > database >  Unable to send local notifications from my flutter app
Unable to send local notifications from my flutter app

Time:12-08

I am using flutter_local_notifications dependency in my app to send a local notification after a button is pressed. In the app, on the execution of the button, the local notification should appear and the app should navigate to the second screen. However, only the second action is getting executed and notifications aren't showing up. I have taken references from this YouTube tutorial to create local notifications in my app.

The notificationservice.dart file of my app is:

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

class NotificationService {
  static final NotificationService _notificationService = NotificationService._internal();

  factory NotificationService() {
    return _notificationService;
  }

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =

 

    FlutterLocalNotificationsPlugin();
    
      NotificationService._internal();
    
      Future<void> initNotification() async {
        final AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@drawable/ic_launcher');

    final IOSInitializationSettings initializationSettingsIOS =
    IOSInitializationSettings(
      requestAlertPermission: false,
      requestBadgePermission: false,
      requestSoundPermission: false,
    );

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

    await flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

  Future<void> showNotification(int id, String title, String body, int seconds) async {
    await flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      body,
      tz.TZDateTime.now(tz.local).add(Duration(seconds: seconds)),
      const NotificationDetails(
        android: AndroidNotificationDetails(
          'main_channel',
          'Main Channel',
          channelDescription: 'Main channel notifications',
          importance: Importance.max,
          priority: Priority.max,
          icon: '@drawable/ic_launcher'
        ),
        iOS: IOSNotificationDetails(
          sound: 'default.wav',
          presentAlert: true,
          presentBadge: true,
          presentSound: true,
        ),
      ),
      uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
      androidAllowWhileIdle: true,
    );
  }
}

The implementation of the local notification functionality in TextButton widget is as follows:

TextButton(
            onPressed: () {
                //lines of codes
                NotificationService().showNotification(1, "title", "body", 5);

                Navigator.of(context).pushNamed(CaseConfirmationScreen.routeName);
            },
            child: Text(
              "File Case",
              style: GoogleFonts.poppins(
                color: Colors.white,
                fontSize: 26.0,
                fontWeight: FontWeight.w500,
                letterSpacing: 2.0,
              ),
            ),
            style: TextButton.styleFrom(
              backgroundColor: Colors.red,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(6.0),
              ),
              padding: const EdgeInsets.symmetric(
                horizontal: 22.0,
                vertical: 15.0,
              ),
            ),
          ),

The error shown in the debug console is this.

The sourcecode of the app in the Youtube Tutorial

What should I do to clear this bug?

CodePudding user response:

Add your icon to [projectFolder]/android/app/src/main/res/drawable (for example app_icon.png) and use that name here:

var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
  • Related