Home > Net >  How to dynamically set the custom notification sound in flutter (android only )?
How to dynamically set the custom notification sound in flutter (android only )?

Time:11-15

Using the package flutter_local_notification to show scheduled notification , by using the custom sound but i want the user to choose the ringtone from the list , on ios it is working fine , but on the android side it plays the old ringtone if also the new one is selected .

 
  addnotification(DateTime time, int id, String? ringtone) {
    log('$ringtone', name: 'notification ringtone');
    tzData.initializeTimeZones();
    log(tz.local.toString(), name: 'time zone');
    final scheduleTime = tz.TZDateTime.from(time, tz.local);

    NotificationDetails notificationDetails = NotificationDetails(
        android: AndroidNotificationDetails(
          'mychannel2',
          'alarm',
          // const UriAndroidNotificationSound(
          //     'assets/ringtones/alarm.mp3'), //
          sound: RawResourceAndroidNotificationSound('$ringtone'),
          importance: Importance.max,
          priority: Priority.high,
          // autoCancel: false,
          // timeoutAfter: 10000,

          // vibrationPattern: Int64List(5),
          // category: AndroidNotificationCategory.alarm,
          enableLights: true,
          fullScreenIntent: true,
          enableVibration: true,
          audioAttributesUsage: AudioAttributesUsage.alarm,
          visibility: NotificationVisibility.public,
          playSound: true,
        ),
        iOS: DarwinNotificationDetails(
            presentSound: true, sound: '$ringtone.wav'));

    _localNotificationsPlugin.zonedSchedule(id, 'Morning Alarm',
        'wake up the alarm is ringing...', scheduleTime, notificationDetails,
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
        androidAllowWhileIdle: true,
        matchDateTimeComponents: DateTimeComponents.time);

    // _localNotificationsPlugin.show(id, 'alarm', 'alarm', notificationDetails);
  }
 

CodePudding user response:

As per their documentation:

For Android 8.0 , sounds and vibrations are associated with notification channels and can only be configured when they are first created. Showing/scheduling a notification will create a channel with the specified id if it doesn't exist already. If another notification specifies the same channel id but tries to specify another sound or vibration pattern then nothing occurs.

You can try creating a new channel for each notification with different sound.

More info could be found here https://pub.dev/packages/flutter_local_notifications#-android-setup

  • Related