Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/enable notification sound for your flutter application? [closed]

I want to disable sound for incoming notification when the user turn of the notification switch in my app.

I also read official documentation but couldn't find anything helpful.

Is there any way? Also I added property playsound: false but it's not working for me, I still got android default notification sound.

I tried This package for local notifications but didn't find what I am looking for!

Refer my code below for sending notification on android.

    import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class MusicNotification {
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  Future<void> init() async {
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings("@mipmap/ic_launcher");

    const DarwinInitializationSettings initializationSettingsIOS =
        DarwinInitializationSettings();

    const InitializationSettings initializationSettings =
        InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
    );

    await flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
    );
  }

  void showNotificationAndroid(String title, String value) async {
    const AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(
      'Music123',
      'Music',
      channelDescription: 'Music Notification',
      importance: Importance.max,
      priority: Priority.high,
      playSound: false,
      ticker: 'ticker',
    );

    int notification_id = 1;
    const NotificationDetails notificationDetails =
        NotificationDetails(android: androidNotificationDetails);

    await flutterLocalNotificationsPlugin.show(
        notification_id, title, value, notificationDetails,
        payload: 'Not present');
  }
}

and this is for calling notification event

MusicNotification().showNotificationAndroid(
    'title',
    'value');
like image 467
M.AQIB Avatar asked Oct 27 '25 00:10

M.AQIB


2 Answers

This should be handled by your backend service. You can mute the sound by not sending "sound" key in your Cloud Messaging API. Read more here

like image 86
Abhi Tripathi Avatar answered Oct 28 '25 18:10

Abhi Tripathi


One of the techniques can be deleting your old notification channel with sound and recreating the channel with no sound. This package is handy for dealing with notifications in flutter.

  • Delete Notification Channel
  • Create Notification Channel
like image 22
Niaz Ahmed Avatar answered Oct 28 '25 20:10

Niaz Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!