Flutter,检查通知设备设置

问题描述 投票:0回答:1

我正在使用 Firebase Messaging 推送通知。当我向设备令牌发送通知时一切都很好,但是当我想检查用户是否打开或关闭设备通知时,ios 总是返回给我PermissionStatus.permanentlyDenied。在 Android 上一切都很好,检查正确。我正在使用app_settings和permission_handler来检查。

检查功能:

    if (Platform.isAndroid) {
      if (!await Permission.notification.isGranted) {
        setState(() {
          needNotifications = true;
        });
      }
    } else if (Platform.isIOS) {
      PermissionStatus status = await Permission.notification.request();
      print("Permission Status: $status");

      FirebaseMessaging messaging = FirebaseMessaging.instance;
      NotificationSettings settings = await messaging.getNotificationSettings();

      if (settings.authorizationStatus == AuthorizationStatus.authorized) {
        setState(() {
          needNotifications = false;
        });
      } else if (settings.authorizationStatus ==
          AuthorizationStatus.provisional) {
        setState(() {
          needNotifications = true;
        });
      } else {
        setState(() {
          needNotifications = true;
        });
      }
      if (status == PermissionStatus.permanentlyDenied) {
        setState(() {
          needNotifications = true;
        });
      }
    }
  }

我将信息添加到 Podfile,使用 :

编辑 info.plist
    <string>Powiadomienia są niezbędne do działania aplikacji</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
        <string>fetch</string>
    </array>

通过 xcode 在模拟器 ios 中通过 xcode 在真实设备上通过 xcode 以及通过真实设备上的 testflight 进行测试。应用程序总是返回给我PermissionStatus.permanentlyDenied

android ios flutter mobile
1个回答
0
投票
当用户拒绝应用程序的权限时,会发生

PermissionStatus.permanentlyDenied
。您可以通过将用户定向到应用程序设置来请求用户授予所需的权限。参考以下代码片段:

import 'package:permission_handler/permission_handler.dart';

Future<void> requestPermission() async {
  PermissionStatus status = await Permission.notification.status;

  if (status == PermissionStatus.permanentlyDenied) {
    // Inform the user to enable permissions from settings
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('Permission Required'),
        content: Text(
            'Please enable location permissions in the app settings to proceed.'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: Text('Cancel'),
          ),
          TextButton(
            onPressed: () async {
              Navigator.of(context).pop();
              await openAppSettings(); // Open the app settings
            },
            child: Text('Open Settings'),
          ),
        ],
      ),
    );
  } else if (status.isDenied) {
    // Request permission again
    final newStatus = await Permission.location.request();
    if (newStatus.isGranted) {
      print("Permission granted");
    } else {
      print("Permission denied");
    }
  } else if (status.isGranted) {
    print("Permission already granted");
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.