I am trying to show flutter local notification when receiving firebase notification in background, foreground and terminated states of app. i am trying to resolve this using work manager package and it is working like a charm in android. it works in foreground, background and terminated states as well, but it is not working in ios device. if i am trying to execute task which is not registered in info plist and appDelegate it gives me an error but after registering it is not doing anything (no error and not executing my code as well). i imagine that my task is registered but iOS did not execute it in some reason. if anyone has done something similar, please provide some examples or maybe some thoughts about what i am doing wrong?
Trying to resolve problem using work manager and flutter local notifications
2
Answers
I struggled with this issue for many weeks until I managed to have notifications appear for my ios app regardless of its state.
First, you must tick ‘Background fetch’ and ‘Background processing’ modes in Xcode. Those can be found under Signing & Capabilities.
Next, you must register your tasks and give your app notifications capabilities in your AppDelegate.swift.
You will need to also register your task in the info.plist as so:
registerPeriodicTask doesn’t work for ios, so instead, you should use the flutter_local_notifications periodicallyShow function.
For reference, I provided a snippet from my code that shows how I am using workmanager and flutter_local_notifications;
Remember that ios devices can decide to postpone running your background work manager task for many reasons (e.g. low battery). Moreover, notifications won’t be displayed using an ios simulator.
You can do this by using
flutter_local_notification
, If you are open to it. It is much simpler and compact.Pre-requesites:
flutter_local_notification
listening to the fcm stream.Complete Code:
notification_helper.dart
// class explicitly to handle foreground notification