iOS notification sounds not working when the app is in the background
const AndroidNotificationChannel channel = AndroidNotificationChannel( 'high_importance_channel', // id 'High Importance Notifications', // title// description playSound: true, sound: RawResourceAndroidNotificationSound('alert'), );
Future
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
// 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!); } } }
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