Flutter · One codebase, native feel
How do you implement push notifications reliably?
Short answer
Register for permission, obtain and refresh the device token, and send data-rich messages through FCM or APNs. Handle foreground, background and cold-start paths and test on real devices.
Push delivery has three stages: the OS receives the message, the app processes it, and the UI reflects it. Each stage can fail. Tokens rotate, permission can be denied, background execution is limited, and notification taps need a route that works even on a cold start.
Future<void> initPush() async {
final settings = await FirebaseMessaging.instance.requestPermission();
if (settings.authorizationStatus == AuthorizationStatus.denied) return;
final token = await FirebaseMessaging.instance.getToken();
if (token == null) return;
await api.registerDeviceToken(token);
FirebaseMessaging.instance.onTokenRefresh.listen(api.registerDeviceToken);
FirebaseMessaging.onMessage.listen((message) {
final route = message.data['route'];
if (route != null) appRouter.go(route);
});
}- Ask for permission at a moment the user understands, not on first launch by default
- Register tokens per user and remove them on logout
- Android notification channels control importance and sound; create them deliberately
- iOS needs APNs configuration and correct entitlements; test on a physical device
- Use data messages for app logic and notification messages for visible alerts, subject to platform behaviour
Tap handling and cold start
A notification tap can launch the app from terminated state. Read the initial message and route after the router is ready, otherwise the navigation is lost. Deduplicate handling if both foreground and background callbacks fire. Deep link payloads should be validated like any external input.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you handle a notification tap when the app was terminated?
- 2What happens when a device token refreshes?
- 3Why are Android notification channels required?
- 4How would you avoid notifying a user who just logged out?
Say it in the interview
I request permission at a sensible moment, register the token against the user, and listen for token refresh. Foreground, background and cold-start taps each need explicit handling, and on cold start I delay routing until the router exists. I test on real devices because APNs and background execution behave differently from simulators. I also treat push as best effort and refresh critical state on open.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Flutter mock interview and get scored feedback on depth, clarity and confidence.