flutter_local_notifications icon indicating copy to clipboard operation
flutter_local_notifications copied to clipboard

iOS notification sounds not working when the app is in the background

Open RajSoni8866 opened this issue 10 months ago • 1 comments

const AndroidNotificationChannel channel = AndroidNotificationChannel( 'high_importance_channel', // id 'High Importance Notifications', // title// description playSound: true, sound: RawResourceAndroidNotificationSound('alert'), );

Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { print('Handling a background message ${message.data}'); }

class PushNotificationsManager { // singleton static final PushNotificationsManager _singleton = PushNotificationsManager._internal(); factory PushNotificationsManager() => _singleton;

PushNotificationsManager._internal();

static PushNotificationsManager get shared => _singleton;

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance; String? token;

var androidSettings = const AndroidInitializationSettings( '@drawable/ic_state_name', ); var macSettings = const DarwinNotificationDetails(); var iOSSettings = const DarwinInitializationSettings( requestSoundPermission: false, requestBadgePermission: false, requestAlertPermission: false, );

Future firebaseCloudMessaging_Listeners() async { if (defaultTargetPlatform == TargetPlatform.iOS) await iOS_Permission();

// Set the background messaging handler early on, as a named top-level function
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
AndroidNotificationChannel channel = const AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications',
  playSound: true, // title// description
  importance: Importance.high,
  sound: RawResourceAndroidNotificationSound('alert'),
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

if (defaultTargetPlatform == TargetPlatform.android) {
  flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
        AndroidFlutterLocalNotificationsPlugin
      >()
      ?.requestNotificationsPermission();
}

await flutterLocalNotificationsPlugin.initialize(
  InitializationSettings(android: androidSettings, iOS: iOSSettings),
  onDidReceiveBackgroundNotificationResponse: _onLocalNotificationTap,
  onDidReceiveNotificationResponse: ((value) {
    if (value.payload != null) {
      _onLocalNotificationTap(value);
    }
  }),
);

/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance
    .setForegroundNotificationPresentationOptions(
      alert: false,
      badge: true,
      sound: true,
    );
await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin
    >()
    ?.createNotificationChannel(channel);

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

_firebaseMessaging.getToken().then((token) async {
  if (token != null) {
    print('FCM TOKEN : $token');
  }
});

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  RemoteNotification? notification = message.notification;
  AndroidNotification? android = message.notification?.android;
  var data = message.data;

  if (notification != null) {
    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        iOS: const DarwinNotificationDetails(
          presentAlert: true,
          presentBadge: true,
          presentSound: true,
        ),
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          sound: channel.sound,
          playSound: true,
          icon: '@drawable/ic_state_name',
        ),
      ),
      payload: jsonEncode(message.data),
    );
  }
});

}

iOS_Permission() async { await _firebaseMessaging.requestPermission( alert: true, badge: true, sound: true, announcement: true, carPlay: true, criticalAlert: true, ); }

static void _onLocalNotificationTap(NotificationResponse value) async { if (value.payload != null) { Map<String, dynamic> dic = jsonDecode(value.payload!); } } }

RajSoni8866 avatar Feb 20 '25 06:02 RajSoni8866

Please provide a link to a repo hosting a minimal app that can reproduce the issue with FCM and only uses this plugin. This helps ensure what you're reporting is to do with this plugin. Requesting this as well as a lot of devs who use FCM that get notifications in the background are receiving notifications shown by the FCM plugin

MaikuB avatar Mar 15 '25 05:03 MaikuB