gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 86 forks source link

I can't get it to work with FCM #151

Closed ayoub-kplr closed 9 months ago

ayoub-kplr commented 9 months ago

Alarm plugin version ex: 0.2.9 (please update to the latest version before submitting the issue) 3.0.10 Describe the bug I'm using FCM, and alarm doesn't work when it's being callled from the handler of onBackgroundMessage

To Reproduce Steps to reproduce the behavior:

  1. class FirebaseApi {
    final _firebaseMessaging = FirebaseMessaging.instance;
    final DatabaseReference dbRef = FirebaseDatabase.instance.ref();
    
    Future<void> initNotification() async {
    await _firebaseMessaging.requestPermission();
    // check for internet !!!!!!!!!!!!!!!!!!!!!
    final fCMToken = await _firebaseMessaging.getToken();
    print("Token: $fCMToken");
    
    }
    
    static Future<dynamic> _firebaseMessagingBackgroundHandler(
      RemoteMessage message) async {
    print("Handling background message");
    // await Alert.startAlarm("");
    final alarmSettings = AlarmSettings(
      id: 42,
      dateTime: DateTime.now(),
      assetAudioPath: 'assets/alarm.mp3',
      loopAudio: true,
      vibrate: true,
      volume: 0.8,
      fadeDuration: 3.0,
      notificationTitle: 'This is the title',
      notificationBody: 'This is the body',
      enableNotificationOnKill: true,
    );
    await Alarm.set(alarmSettings: alarmSettings);
    }
    
    Future initPushNotifications() async {
    // handle notif if the app was terminated and now opened
    FirebaseMessaging.instance.getInitialMessage().then((value) => null);
    // attach event listners for when a notification opens the app
    
    FirebaseMessaging.onMessageOpenedApp
        .listen(_firebaseMessagingBackgroundHandler);
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
    }
    }

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Device info Provide device info (Manufacturer, OS version, ...) Android 9

Additional context Add any other context about the problem here.