skip to Main Content

I am using flutter_local_notifications to use notifications service.

When I set it up , I don’t get any errors or warnings but the notifications don’t show.

Here is my settings :

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin =
  FlutterLocalNotificationsPlugin();

  Future<void> initNotification() async {
    AndroidInitializationSettings initializationSettingsAndroid =
    const AndroidInitializationSettings('@mipmap/ic_launcher');

    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());
  }
}

I initialize my notifications like this:

Future<void> main() async {

  WidgetsFlutterBinding.ensureInitialized();
  NotificationService().initNotification();
....
}

These are my permissions (Some permissions are for different purposes):

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

How can I solve this problem?

2

Answers


  1. Chosen as BEST ANSWER

    I have solved the problem . It was caused by system not asking permission so I had to do it manually. Here is the simple code :

    import 'package:permission_handler/permission_handler.dart';
    
     PermissionStatus status = await Permission.notification.request();
    

    adding this code solved all issues.


  2. You should initialize Firebase using the code below. I believe that will resolve your issue.

    Future<void> main() async {
    
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      NotificationService().initNotification();
     
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search