flutter_local_notifications icon indicating copy to clipboard operation
flutter_local_notifications copied to clipboard

iOS Background Custom Sound Not Working

Open bedirhanayydin opened this issue 1 year ago • 7 comments

Hello, the code I wrote works on both iOS and Android. On iOS, the custom notification sound plays when the app is in the foreground. However, when the app is in the background, notifications are received but the notification sound doesn't play. How can I solve this issue on iOS?

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void sendNotification({String? title, String? body}) async {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  // iOS (Darwin) settings
  const DarwinInitializationSettings initializationSettingsIOS = DarwinInitializationSettings(
    requestSoundPermission: true,
    requestBadgePermission: true,
    requestAlertPermission: true,
  );
  const InitializationSettings initializationSettings = InitializationSettings(
    iOS: initializationSettingsIOS,
  );
  await flutterLocalNotificationsPlugin.initialize(
    initializationSettings,
  );

 

  DarwinNotificationDetails iosNotificationDetails = DarwinNotificationDetails(
    sound: "slow_spring_board.mp3",
    presentSound: true,
    presentAlert: true,
    presentBadge: true,
  );

  flutterLocalNotificationsPlugin.show(
    0,
    title,
    body,
    NotificationDetails(
      iOS: iosNotificationDetails,
    ),
  );
}

bedirhanayydin avatar Jun 03 '24 18:06 bedirhanayydin

having the same problem

Blacktaler avatar Jun 11 '24 10:06 Blacktaler

@MaikuB can you help us

bedirhanayydin avatar Jun 11 '24 10:06 bedirhanayydin

Are you using FCM?

MaikuB avatar Jun 12 '24 10:06 MaikuB

yes

There's your answer then. Please make sure you're distinguishing between FCM and this plugin that when you raise an issue and include code that it actually reproduces the issue specific to this plugin. The native APIs that the plugin don't behave differently based on if the app is in the foreground or not. A relatively easy way to reproduce this is schedule a notification using the plugin and kill the app so that the scheduled notification appears whilst the app is killed

MaikuB avatar Jun 12 '24 10:06 MaikuB

My codes are above, where do you think I'm making a mistake?

bedirhanayydin avatar Jun 12 '24 10:06 bedirhanayydin

This isn't about a coding mistake and I don't specifically help with debugging an individual's code. As mentioned in my previous post you'll need to make sure you understand how FCM works

MaikuB avatar Jun 12 '24 10:06 MaikuB

Thank you, I actually understood the fcm, but I just didn't understand why the custom sound I get when the application is open does not play the same sound when the application is closed.

bedirhanayydin avatar Jun 12 '24 10:06 bedirhanayydin

The same problem. iOS version 16.7.8, flutter_local_notifications version 16.3.3.

perzend avatar Sep 04 '24 09:09 perzend

Thank you, I actually understood the fcm, but I just didn't understand why the custom sound I get when the application is open does not play the same sound when the application is closed.

What I said before still applies on making sure you understand FCM and would explain it. Please make sure you read the docs on how FCM works. Currently nothing you have provided has shown there to be an issue that relates to this plugin

MaikuB avatar Sep 04 '24 09:09 MaikuB

use sound.caf to work success

esslaaam avatar Oct 25 '24 00:10 esslaaam

Can you explain what code you used to get the caf to work?. My experienced friend who does Apple builds tried caf and aiff files and wav as well. They did not work for this code here The code works well on Android.

  Future<void> _zonedScheduleNotification(Duration duration) async {
    final scheduledTime = tz.TZDateTime.now(tz.local).add(duration);

    const notificationDetails = NotificationDetails(
      android: AndroidNotificationDetails(
        'my_time_schedule_channel',
        'My Time Schedule Timer',
        channelDescription:
            'Notifications for your My Time Schedule timer sessions.',
        sound: RawResourceAndroidNotificationSound('bell'),
        playSound: true,
        importance: Importance.max,
        priority: Priority.high,
      ),
      iOS: DarwinNotificationDetails(
        sound: 'bell.wav', // Ensure it's included in iOS assets
      ),
      macOS: DarwinNotificationDetails(sound: 'bell.wav'),
    );

    if (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) {
      await flutterLocalNotificationsPlugin.zonedSchedule(
        0,
        'My Time Schedule',
        'Your session has ended.',
        scheduledTime,
        notificationDetails,
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
      );
    } else if (Platform.isLinux || Platform.isWindows) {
      // Fallback: simulate schedule with delayed Timer
      Timer(duration, () async {
        await flutterLocalNotificationsPlugin.show(
          0,
          'My Time Schedule',
          'Your session has ended.',
          notificationDetails,
        );
      });
    }
  }


bksubhuti avatar May 27 '25 13:05 bksubhuti

Can you explain what code you used to get the caf to work?. My experienced friend who does Apple builds tried caf and aiff files and wav as well. They did not work for this code here The code works well on Android.

  Future<void> _zonedScheduleNotification(Duration duration) async {
    final scheduledTime = tz.TZDateTime.now(tz.local).add(duration);

    const notificationDetails = NotificationDetails(
      android: AndroidNotificationDetails(
        'my_time_schedule_channel',
        'My Time Schedule Timer',
        channelDescription:
            'Notifications for your My Time Schedule timer sessions.',
        sound: RawResourceAndroidNotificationSound('bell'),
        playSound: true,
        importance: Importance.max,
        priority: Priority.high,
      ),
      iOS: DarwinNotificationDetails(
        sound: 'bell.wav', // Ensure it's included in iOS assets
      ),
      macOS: DarwinNotificationDetails(sound: 'bell.wav'),
    );

    if (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) {
      await flutterLocalNotificationsPlugin.zonedSchedule(
        0,
        'My Time Schedule',
        'Your session has ended.',
        scheduledTime,
        notificationDetails,
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
      );
    } else if (Platform.isLinux || Platform.isWindows) {
      // Fallback: simulate schedule with delayed Timer
      Timer(duration, () async {
        await flutterLocalNotificationsPlugin.show(
          0,
          'My Time Schedule',
          'Your session has ended.',
          notificationDetails,
        );
      });
    }
  }

can you try ios side with mp3 the model created in dotnet is like this in ios

var apnsConfig = new ApnsConfig
            {
                Aps = new Aps
                {
                    Sound = "test.mp3",
                    ContentAvailable = true,
                    MutableContent = true,
                },
                Headers = new Dictionary<string, string>
                {
                    { "apns-priority", "10" },
                }
            };

bedirhanayydin avatar May 27 '25 13:05 bedirhanayydin