I want to use this package https://pub.dev/packages/notification_permissions to do a Permissions check, I saw the example in it, but I couldn't use it and put it on my project correctly.

All I need is your help to use it correctly with the example attached below.

My project:

main file ==>

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:untitled/notification.dart';

import 'home_page.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  await initFcm();
  runApp(const App());
}

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
        debugShowCheckedModeBanner: false,
        home: HomePage(),
    );
  }
}

home file ==>

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home page'), centerTitle: true),
      body: const Center(
        child: ElevatedButton(
          onPressed: null,
          child: Text('Go to second page'),
        ),
      ),
    );
  }
}

notification file ==>

import 'dart:convert';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

Future<void> initFcm() async {
  await Firebase.initializeApp();

  var initializationSettingsAndroid = const AndroidInitializationSettings('@mipmap/ic_launcher');
  var initializationSettingsIOS = const IOSInitializationSettings();
  var initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
  flutterLocalNotificationsPlugin.initialize(initializationSettings);

  await flutterLocalNotificationsPlugin.initialize(initializationSettings);

  FirebaseMessaging.onMessage.listen((RemoteMessage? message) async {
    RemoteNotification? notification = message?.notification;
    AndroidNotification? android = message?.notification?.android;
    if (notification != null && android != null) {
      flutterLocalNotificationsPlugin.show(
        notification.hashCode,
        notification.title,
        notification.body,
        const NotificationDetails(android: AndroidNotificationDetails('channel.id', 'channel.name')),
        payload: json.encode(message?.data),
      );
    }
  });
}

dependencies ==>

firebase_core: ^1.22.0
firebase_messaging: ^13.0.1
flutter_local_notifications: ^9.9.1


Solution 1: Harsh Sureja

Use Permission Handler Package.

    void main() async{
      WidgetsFlutterBinding.ensureInitialized();
      await Permission.notification.isDenied.then((value) {
        if (value) {
          Permission.notification.request();
        }
      });
      await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
      await initFcm();
      runApp(const App());
    }