skip to Main Content

I want to get the notification permission on my app. For that I use Flutter Permission Handler library. Despite the fact that I have read the documentation and a number of StackOverflow topics I have not found the solution to my problem.

I can get the status of the notification permission but when I try to update this permission nothing happens. I have no pop up appear on the my app screen.

My code is very simple I have HomePage who call another page after the notification permission is granted or not. For that I had create a NotificationServiceClass with two function askNotificationPermission and getNotificationPermission.

The goal of getNotificationPermission is to get the status of the notification permission.

The goal of askNotificationPermission is to set the status of the notification permission.

I verified that askNotificationPermission is call by adding printf inside my function.

I probably made a mistake but I don’t see what it is.

Thank you so much for your help.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:mvc_pattern/mvc_pattern.dart';
import 'package:smart_diese/business_logic/services/notification/notification.dart';
import 'package:smart_diese/business_logic/services/sip/backgroundCallbacks.dart';
import 'package:smart_diese/db/model/application.dart';
import 'package:smart_diese/db/model/utilisateur.dart';
import 'package:smart_diese/db/smart_diese_database.dart';
import 'package:smart_diese/main.dart';
import 'package:smart_diese/views/ui/guide/Guide%20Professionnel.dart';
import 'package:smart_diese/views/ui/root/rootPage.dart';
import 'package:smart_diese/views/ui/tableau/TableuBord.dart';

class HomeUI extends StatefulWidget {
  const HomeUI({super.key});

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

class HomeUIState extends State<HomeUI> {
  bool notificationStatus = false;
  bool _isLoading = false;

  final NotificationService _notificationService = NotificationService();

  Future<void> initialize() async {
    setState(() => _isLoading = true);

    await _notificationService.getNotificationPermission();

    setState(() => _isLoading = false);
  }

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

    initialize();
  }

  @override
  Widget build(BuildContext context) {
    if (notificationStatus == false && _isLoading == false) {
      return Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(20),
          child: ListView(
            children: <Widget>[
              SizedBox(
                height: MediaQuery.of(context).size.height * 0.15,
              ),
              Center(
                child: Container(
                  height: MediaQuery.of(context).size.height * 0.1,
                  decoration: const BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(20)),
                  ),
                  child: const FaIcon(
                    FontAwesomeIcons.triangleExclamation,
                    color: Colors.orange,
                    size: 60,
                  ),
                ),
              ),
              SizedBox(
                height: MediaQuery.of(context).size.height * 0.07,
              ),
              const Text(
                'Notification',
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.center,
              ),
              SizedBox(
                height: MediaQuery.of(context).size.height * 0.05,
              ),
              const Text(
                "L'application a besoin de votre autorisation pour vous envoyer des notifications, dans le but de recevoir les appels entrants",
                style: TextStyle(
                  fontSize: 15,
                ),
                textAlign: TextAlign.center,
              ),
              SizedBox(
                height: MediaQuery.of(context).size.height * 0.20,
              ),
              ElevatedButton(
                onPressed: () => setState(() => notificationStatus = true),
                child: const Text('Continuer sans les notifications'),
              ),
              ElevatedButton(
                onPressed: () async {
                  await _notificationService.askNotificationPermission();
                  final bool tmp =
                      await _notificationService.getNotificationPermission();
                  setState(
                    () => notificationStatus = tmp,
                  );
                },
                child: const Text('Accorder les permissions'),
              ),
            ],
          ),
        ),
      );
    } else if (_isLoading == false && notificationStatus == true) {
      // Call another page
    } else {
      return const Scaffold(
        body: Center(
          child: CircularProgressIndicator(),
        ),
      );
    }
  }
}
class NotificationService {
  Future<bool> getNotificationPermission() async {
    final PermissionStatus status = await Permission.notification.status;

    if (status.isGranted) {
      return true;
    } else {
      return false;
    }
  }

  Future<void> askNotificationPermission() async {
    await Permission.notification.request();
  }
}

2

Answers


  1. I’ve had this same problem. What worked was deleting the app from the emulator and running the code after.
    I assume there is some check to see if it already established whether to accept certain permissions.

    Login or Signup to reply.
  2. You forgot to call the function which actually requests the permission and it works after that is changed (note: I didn’t get a popup, but get PermissionStatus.granted from my print). I edited your code as follows:

    class NotificationService {
      Future<bool> getNotificationPermission() async {
        
        final PermissionStatus status = await askNotificationPermission();
        print('Status');
        print(status);
    
        if (status.isGranted) {
          return true;
        } else {
          return false;
        }
      }
    
      Future<PermissionStatus> askNotificationPermission() async {
        return await Permission.notification.request();
      }
    }
    

    I’m keeping my previous answer there incase it helps someone else.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search