简体   繁体   English

Flutter 如何授予通知权限?

[英]Flutter how can i grant permission for notification?

I'm doing local push notifications.我正在做本地推送通知。 But when user doesn't grant permission, its not showing bubble for it.但是当用户不授予权限时,它不会显示气泡。 How can i grant access from user while that settings on the image is closed?当图像上的设置关闭时,如何授予用户访问权限? Thanks for help!感谢帮助!

在此处输入图像描述

You can defined the settings like this in main您可以在 main 中定义这样的设置

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('app_icon');
  final IOSInitializationSettings initializationSettingsIOS =
      IOSInitializationSettings(
    requestSoundPermission: false,
    requestBadgePermission: false,
    requestAlertPermission: false,
    onDidReceiveLocalNotification: onDidReceiveLocalNotification,
  );
  final MacOSInitializationSettings initializationSettingsMacOS =
      MacOSInitializationSettings(
          requestAlertPermission: false,
          requestBadgePermission: false,
          requestSoundPermission: false);
  final InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
      macOS: initializationSettingsMacOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: onSelectNotification);

Then to get permission call (iOS)然后获得权限调用(iOS)

final bool result = await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
        IOSFlutterLocalNotificationsPlugin>()
    ?.requestPermissions(
    alert: true,
    badge: true,
    sound: true,
    );

Adding these will pop up a page for the user to accept getting notifications.添加这些将弹出一个页面供用户接受接收通知。 But please notice that if the user denies to give permissions then the notifications are not saved and we will not be able to force receiving a notification.但请注意,如果用户拒绝授予权限,则通知不会保存,我们将无法强制接收通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM