Home > Software design >  Changing Flutter Local Notifications Sound
Changing Flutter Local Notifications Sound

Time:12-07

Ive created this before the Main

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp(
    name: "AlertApp",
    options: DefaultFirebaseOptions.currentPlatform,
  );
}

Adding this on my main

FirebaseMessaging messaging = FirebaseMessaging.instance;

  await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );


  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

I wanted to have a Custom notification sound when there's a push notification from firebase, I have added this on my initstate but nothing has changed

@override
  void initState() {
    super.initState();
    var initializationSettingsAndroid = new AndroidInitializationSettings('ic_launcher');
    var initializationSettingsiOS = IOSInitializationSettings();
    var initializationSettings =
    InitializationSettings(android: initializationSettingsAndroid,iOS: initializationSettingsiOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channelDescription: channel.description,
                color: Colors.blue,
                icon: "@mipmap/ic_launcher",
              ),
            ));
      }
    });
  }

I tried it on my Android real device

CodePudding user response:

You can refer to this article on how to do that for Android and this article for iOS

CodePudding user response:

I appears that you havn't added sound to the initstate thus your code was not working.

AndroidNotificationDetails androidNotificationsDetails = AndroidNotificationDetails(
       'your other channel id',
       'your other channel name',
       'your other channel description',
        importance: Importance.Max,
        priority: Priority.Max,
        enableLights: true,
        playSound: true,
        sound: RawResourceAndroidNotificationSound('notification_sound'),
);

you have to add these lines to your code

playSound: true,
sound: RawResourceAndroidNotificationSound('notification_sound'),

Hope this helps. Happy Coding :)

  • Related