I am trying to figure out if app is opened by clicking on push notification or by clicking on app launcher icon, scenario is that i want to check if app is opened by clicking push notification then i’ll load data on home screen from cache other wise i need to get data from api.
i am using getInitialMessage() method but its not working in my case
here is my code
Class NotificationService{
String? msg;
bool fromNotification = false;
final _firebaseMessaging = FirebaseMessaging.instance;
Future<void> initNotification() async
{
RemoteMessage? initialMessage = await _firebaseMessaging.getInitialMessage();
if (initialMessage != null)
{ msg = initialMessage.toString();
fromNotification = true;
}
}
}
2
Answers
You can achieve this by setting a flag at the time when the app launches. This can be done within your PushNotificationHandler class using the below function.
Assuming your first screen is called MyHomePage, you can pass the flag to it as shown below:
Using getInitialMessage() method:
You can then use the flag as you wish.
I have achieved this through the onMessageOpenedApp function. See my code below.
Define a PushNotificationHandler class as shown below:
The function initialize() will return bool.
Define your app as shown below:
In the MyHomePage widget, I am printing out how the app has been opened.
This should solve the issue you are having.