Why Flutter Android Alarm Manager is not working in device once the App is closed?

Viewed 649

I am new to flutter. Alarm functionality is working unless the app is closed. After closing the app android alarm manager is not working. I have put all the tags in AndroidManifest.xml. On call back this _ringAlarm() method has been called.

Future<void> _ringAlarm() async {
  


  NotificationManager n = new NotificationManager();

  n.initNotificationManager();
  n.showNotificationWithDefaultSound("Keep it,it's YOURS", widget.text.split("_")[1]);
  //n.showNotificationWithAlarmSound();
  //Working in Simulator but not in Device
  //n._showNotificationCustomSound();
  //FlutterRingtonePlayer.playAlarm();
  FlutterRingtonePlayer.play(
    android: AndroidSounds.alarm,
    ios: IosSounds.glass,
    looping: false, // Android only - API >= 28
    volume: 1, // Android only - API >= 28
    asAlarm: false, // Android only - all APIs
  );
  Future.delayed(const Duration(milliseconds: 4000), () {
    FlutterRingtonePlayer.stop();
  });
  


  print(Const.todoTextList);

}


Here is implementation of notification manager class

class NotificationManager
{
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  AndroidInitializationSettings initializationSettingsAndroid;
  IOSInitializationSettings initializationSettingsIOS;
  InitializationSettings initializationSettings;

  void initNotificationManager()
  {
    initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
    initializationSettingsIOS = new IOSInitializationSettings();
    initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings);


  }

  void showNotificationWithDefaultSound(String title, String body)
  {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails('your channel id', 'your channel name', 'your channel description', importance: Importance.Max, priority: Priority.High);
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    flutterLocalNotificationsPlugin.show(0, title, body, platformChannelSpecifics);
  }



 static const MethodChannel platform = MethodChannel('your channel name');
 /* Future<void> showNotificationWithAlarmSound() async {
    /// this calls a method over a platform channel implemented within the
    /// example app to return the Uri for the default alarm sound and uses
    /// as the notification sound
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    final String alarmUri = await platform.invokeMethod('getAlarmUri');
    final UriAndroidNotificationSound uriSound =
    //UriAndroidNotificationSound(AndroidSounds.alarm.toString());
     UriAndroidNotificationSound(alarmUri);
    final AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
        'uri channel id', 'your channel name', 'your channel description',
        sound: uriSound,
        styleInformation: const DefaultStyleInformation(true, true));
    final NotificationDetails platformChannelSpecifics = NotificationDetails(androidPlatformChannelSpecifics,iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
        0, 'uri sound title', 'uri sound body', platformChannelSpecifics);
  }*/

  //For custom notification
  Future<void> _showNotificationCustomSound() async {
    const AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
      'your other channel id',
      'your other channel name',
      'your other channel description',
      sound: RawResourceAndroidNotificationSound('easy_going'),
    );
    const IOSNotificationDetails iOSPlatformChannelSpecifics =
    IOSNotificationDetails(sound: 'easy_going');
    //const MacOSNotificationDetails macOSPlatformChannelSpecifics =
    //MacOSNotificationDetails(sound: 'slow_spring_board.aiff');
    const NotificationDetails platformChannelSpecifics = NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
        0,
        'custom sound notification title',
        'custom sound notification body',
        platformChannelSpecifics);
  }
}

It is working good if I don't close actual app.Once I close the app notification will not work.I am unable to reproduce this issue in simulator.If I get any suggestion how to reproduce this in simulator that also will be helpful to me.

0 Answers
Related