Ad

Is There A Way To Stop Alarm Sound By Swiping From Notification In Flutter

I wanted to call the alarm when the notification came. I achieved it but I'm not stopping the alarm by swiping the notification. Here is my code example:

Future<void> _messageHandler(RemoteMessage message) async {
  debugPrint('Incoming notification is ${message.notification!.body}');
  FlutterRingtonePlayer.playAlarm(asAlarm: false);
  //FlutterRingtonePlayer.stop();
}

void main() async{
  HttpOverrides.global = new MyHttpOverrides();
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_messageHandler);
  runApp(MyApp());
}

Therefore, is there any way to fix this problem? I'm waiting for your ideas, thank you...

Ad

Answer

Future<void> _messageHandler(RemoteMessage message) async {
  debugPrint('Incoming notification is ${message.notification!.body}');
  FlutterRingtonePlayer.playAlarm(asAlarm: false);
  debugPrint('afterPlay');
  Future.delayed(const Duration(seconds: 15), () {
    FlutterRingtonePlayer.stop();
  });
  debugPrint('afterStop');
}
Ad
source: stackoverflow.com
Ad