dart Flutter本地通知,自定义声音不适用于iOS

at0kjp5o  于 2023-04-27  发布在  Flutter
关注(0)|答案(3)|浏览(226)

我在我的项目中使用flutter_local_notifications: 4.0.1+2。出现通知“我正在尝试在iOS上播放自定义声音”。我尝试按照以下步骤为iOS实现自定义声音,但不起作用:

flutterLocalNotificationsPlugin
  .resolvePlatformSpecificImplementation<
      IOSFlutterLocalNotificationsPlugin>()
  ?.requestPermissions(
    alert: true,
    badge: true,
    sound: true,
  );

  var initializationSettings = InitializationSettings(
  android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
  flutterLocalNotificationsPlugin.initialize(initializationSettings);

  const AndroidNotificationDetails androidPlatformChannelSpecifics =
      AndroidNotificationDetails(
          'Adhan Times Notification', 'Islamic App', 'Allah O Akbar',
          priority: Priority.high,
          sound: RawResourceAndroidNotificationSound('azan'));
  const IOSNotificationDetails iOSPlatformChannelSpecifics =
      IOSNotificationDetails(sound: 'azan.aiff');

azaan.aiff文件位于app/ios/Runner中。
任何帮助都将不胜感激谢谢

mwngjboj

mwngjboj1#

除了IOSNotificationDetails构造函数的sound属性之外,playsound属性必须设置为true。

sg2wtvxw

sg2wtvxw2#

const DarwinNotificationDetails darwinNotificationDetails =
    DarwinNotificationDetails(sound: 'slow_spring_board.aiff');

    final NotificationDetails notificationDetails = NotificationDetails(
      iOS: darwinNotificationDetails,
      //  linux: linuxPlatformChannelSpecifics,
    );
    await flutterLocalNotificationsPlugin.show(
      id++,
      'custom sound notification title',
      'custom sound notification body',
      notificationDetails,
    );

也拖放声音文件在ios文件夹中的xcode

axr492tv

axr492tv3#

您可以在主屏幕的init方法中调用下面的方法

void _requestPermissions() {
flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
        IOSFlutterLocalNotificationsPlugin>()
    ?.requestPermissions(
      alert: true,
      badge: true,
      sound: true,
    );
flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
        MacOSFlutterLocalNotificationsPlugin>()
    ?.requestPermissions(
      alert: true,
      badge: true,
      sound: true,
    );
  }

相关问题