Flutter_local_notifications: SelectNotification callback before showing first screen

Created on 13 Sep 2018  路  13Comments  路  Source: MaikuB/flutter_local_notifications

Is it possible to somehow get the callback before showing the root page? Basically to determine which page to load. I've tried a bunch of things but could not get it to work so far.
In my case I would like to show a different page as root if the app gets newly started via a notification callback. I can't just do pushReplacment or pushAndRemoveUntil inside the active root page since I wanna preserve the stack in case the app has been opened before the callback.

Most helpful comment

I've release a new version of the plugin (0.3.8) that adds a getNotificationAppLaunchDetails()that you could use in the main function as I mentioned above so that you can change the default/home route. The select notification callback will still be called but you could use the results of the previous method to prevent it from running code (e.g. navigating to another screen) if needed. Try it out and see how you go

All 13 comments

couldn't you just check if Navigator.canPop(context) returns false and if it does then you could use pushReplacement or pushAndRemoveUntil? If you weren't on the root page when the app has already been opened and the navigation stack isn't empty then it should return true

That would be possible if the root page is always the same otherwise logic gets more complicated, also if there are form elements on it which might be filled out already.
Another thing I've noticed is that sometimes the root page shows up very briefly before the callback page is displayed which would not be the ideal user experience especially in case of pushReplacement.

I'm not sure there's a way around it at the moment with the flow of when things get executed on the platform side too. One thought I've had that may or may not work is to expose a method through the plugin that you could call in the main function to determine if the app is being launched due to a notification, you'd use the result of that to change the home page (https://docs.flutter.io/flutter/material/MaterialApp/home.html). initState can't be async but I believe main can be

I've release a new version of the plugin (0.3.8) that adds a getNotificationAppLaunchDetails()that you could use in the main function as I mentioned above so that you can change the default/home route. The select notification callback will still be called but you could use the results of the previous method to prevent it from running code (e.g. navigating to another screen) if needed. Try it out and see how you go

Awesome, works great! Tested it both on iOS and Android.

The didNotificationLaunchApp flag once set to true always remains true, it breaks my other flows

@onimeshc-iprogrammer it is only meant to stay true within the same app session if a notification launched the app but not if it's been restarted. If that is still the same scenario you're referring to then this is working as intended and you'll need to resolve within your own app as it's a method to query the launch state

Is there any way to reset the flag programmatically?

No and there are no plans to. You'll need to fix the logic in your own app, which I would guess is calling the method multiple times

Let me understand, I have all my routes been set using the router, when I launch the app from notification after being killed I get the flag as true, which is fine, later when my app is in active/minimised state I yet get the flag as true.

And please can I get about what method do I need to call multiple times?

Let me understand, I have all my routes been set using the router, when I launch the app from notification after being killed I get the flag as true, which is fine, later when my app is in active/minimised state I yet get the flag as true.

Yes that is expected, it's still the same app session. Launching is about if the app was started where before then it not running at all i.e. being in the background doesn't count as it was just suspended.

And please can I get about what method do I need to call multiple times?

Sorry but I don't understanding what you mean as the wording doesn't make sense. Can you rephrase? If you mean how to stop calling the same method, you could track some state in your own app to make sure it only happens once

@MaikuB Is it possible to not open the app while onSelectNotification callback is triggered as usual?

No

Was this page helpful?
0 / 5 - 0 ratings