Describe the bug Ive added push notification custom sound and issue is sound is playing when phone is in silent mode or vibrate mode.
Steps to reproduce
Ive added .wav file into raw and given NotificationManager.IMPORTANCE_HIGH | NotificationManager.IMPORTANCE_DEFAULT
MainActivity.kt
import android.app.NotificationChannel
import android.app.NotificationManager
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val soundUri = Uri.parse("android.resource://" + applicationContext.packageName + "/" + R.raw.notification)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val channel = NotificationChannel("high_importance_channel",
"High Importance Notifications", NotificationManager.IMPORTANCE_HIGH)
channel.setSound(soundUri, audioAttributes)
val notificationManager = getSystemService<NotificationManager>(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
}
}
main.dart
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
playSound: true,
enableLights: true,
sound: RawResourceAndroidNotificationSound('notification'),
);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@drawable/ic_notification');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String? payload) async {
print("onSelectNotification Called");
if (payload != null) {
PushNotificationResponse pushData =
pushNotificationResponseFromJson(payload);
handleLocalNotificationClick(pushData);
}
});
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("onMessage Called");
PushNotificationResponse pushData =
pushNotificationResponseFromMap(message.data);
(notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
icon: '@drawable/ic_notification',
playSound: true,
color: ColorRes.primaryColor,
enableLights: true,
sound: RawResourceAndroidNotificationSound('notification'),
),
iOS: IOSNotificationDetails(
presentSound: true,
presentBadge: true,
presentAlert: true,
),
),
payload: pushNotificationResponseToJson(pushData),
);
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
print("onMessageOpenedApp Called ");
});
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? message) async {
print("getInitialMessage Called ");
});
Sending push notification using API
{
"to" : "FCM TOKEN",
"data":{
"body":"Test Notification !!!",
"title":"Test Title !!!"
},
"notification":{
"sound":"notification",
"title":"My Title",
"body":"My body"
}
}
It should not play sound while silent mode
Let me know if im missing anything.