pushy / pushy-flutter

The official Pushy SDK for Flutter apps.
Apache License 2.0
21 stars 19 forks source link

[Feature Request] Field to know in which lifecycle the notification received #68

Closed bawahakim closed 4 months ago

bawahakim commented 4 months ago

In most notification library, we have a lot of information on the notification meta data. I find this library gives little information on such data.

One importantion information for us is the notification lifecycle on action (click): Foreground, Background, Terminated. It's very useful to manage various states based on when the notification was clicked.

pushy commented 4 months ago

Hi @bawahakim, Thanks for reaching out. We'd be glad to assist.

Your Flutter app can monitor for foreground/background lifecycle state changes by implementing the WidgetsBindingObserver interface for your Widget class declaration in main.dart:

AppLifecycleState? _state; 

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _state = state;
    });
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

You'll then be able to check the _state inside your backgroundNotificationListener() as follows:

if (_state == AppLifecycleState.paused || _state == null) {
  // in background
}

Please let us know if there's anything else we can help with.