我使用Flutter permission_handler 时没有任何弹出窗口。

huangapple go评论70阅读模式
英文:

I don't have any pop up when I use flutter permission_handler

问题

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.

我想在我的应用程序上获取通知权限。为此,我使用Flutter Permission Handler库。尽管我已阅读了文档和一些StackOverflow主题,但我没有找到解决我的问题的方法。

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.

我的代码非常简单,我有一个HomePage,根据是否授予通知权限调用另一页。为此,我创建了一个NotificationServiceClass,其中包含两个函数askNotificationPermission和getNotificationPermission。

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

getNotificationPermission的目标是获取通知权限的状态。

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

askNotificationPermission的目标是设置通知权限的状态。

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

我通过在我的函数内部添加printf来验证askNotificationPermission是否被调用。

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

我可能犯了一个错误,但我看不出是什么错误。

Thank you so much for your help.

非常感谢您的帮助。

英文:

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

答案1

得分: 0

我有相同的问题。解决方法是从模拟器中删除应用程序,然后再运行代码。
我假设有一些检查来确定是否已经建立了是否接受某些权限的设置。

英文:

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.

答案2

得分: 0

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 in case it helps someone else.

英文:

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&lt;bool&gt; getNotificationPermission() async {
    
    final PermissionStatus status = await askNotificationPermission();
    print(&#39;Status&#39;);
    print(status);

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

  Future&lt;PermissionStatus&gt; askNotificationPermission() async {
    return await Permission.notification.request();
  }
}

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

答案3

得分: 0

我完全从我的Flutter项目中删除了android文件夹,然后重新创建它,问题就解决了。

英文:

I completely deleted the android folder from my flutter project and recreated it afterwards, and that fixed my problem.

huangapple
  • 本文由 发表于 2023年5月21日 01:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定