Push notifications in their own package: a driver per platform and the language the server doesn't know
A push notification looks like a feature and is actually a boundary. It starts on a server, crosses two different platform services, gets displayed by native code that runs while your app is dead. Only later, if the user taps, does it come back to Dart.
I'll keep using the auction app from the previous articles. The notifications that matter are "you've been outbid", "the auction closes in five minutes" and "you won". All three need to arrive in the language the user chose inside the app.
Push can't be solved in Dart
The first instinct is to look for a package that does everything in Dart. There isn't one, and the reason is structural: when the notification arrives, your Dart code may not be running. Backgrounded is one case; killed by the system is another. What decides what appears on screen is the operating system, from the payload and native code.
That changes the design. Instead of "a class that handles push", what works is an explicit boundary: Dart declares what it needs, and each platform implements it its own way.
The package: domain in the middle, platform at the edges
I isolated notifications into their own package, organized in a way that paid off on the first maintenance pass:
push_notification/
domain/ contracts, typed failures, what a notification is
application/ the service that orchestrates, knowing nothing about platforms
infrastructure/ device registration against the backend
platform/ android_push_driver · ios_push_driver · mock_push_driver
The driver is a narrow interface, and the narrowness is the point:
abstract interface class PushDriver {
Future<String?> requestToken();
Future<void> subscribe(String topic);
Future<void> unsubscribe(String topic);
Stream<PushOpened> get opened;
}The third driver, the fake one, is what I'd recommend most. Push is the most tedious feature to test: it needs a real device, granted permission, a cooperating server and patience. With a fake driver, everything above the boundary (subscribing to topics, handling opens, navigating) becomes testable in seconds, in CI, without touching anything native.
Typed failures help more than you'd expect:
sealed class PushFailure {
const PushFailure();
}
final class PermissionDenied extends PushFailure {}
final class TokenUnavailable extends PushFailure {}
final class TopicRejected extends PushFailure {
const TopicRejected(this.topic);
final String topic;
}"Registration failed" is useless information in support. "Permission denied" and "token unavailable" are two problems with different causes and different fixes.
The language the server doesn't know
Here's the problem nobody warns you about, and it has two layers.
The first: if the user picks a language inside the app, the server has no idea, unless you tell it. And don't tell it at send time. Tell it at device registration, alongside the token, and update it whenever the user switches:
Future<void> registerDevice() async {
final token = await driver.requestToken();
if (token == null) return;
await repository.register(
token: token,
// the language is an attribute of the device, not of the send
locale: appLocale.current, // pt-BR, en, zh-Hans
platform: currentPlatform,
);
}The second layer is the painful one: the system language is not the app language. Someone with an iPhone in English may have chosen Portuguese in your app, and Portuguese is what they expect on the lock screen. Every native formatting API will, by default, hand you the system language.
Four strategies, and the question that decides between them
The question isn't technical, it's about ownership: who owns the copy, the server or the app? Everything else follows from that.
1. Finished text, in the registered language. The server resolves the translation and sends title and body already written. Simple, works with the app killed, small payload. Depends entirely on the registered locale being correct.
2. Multilingual payload. The server sends every language and the client picks:
{
"data": {
"title": {
"pt-BR": "Seu lance foi superado",
"en": "You've been outbid",
"zh-Hans": "你的出价被超越了"
}
}
}This solves the language problem without depending on registration being up to date, which is its real advantage. It fits comfortably in three locales. APNs' 4 KB limit only starts to bite as the language list grows or bodies get long. The true cost isn't size: it's that you transmit three versions to deliver one, and the copy catalog moves onto the server.
3. A key and parameters. The server sends type, values and identifiers; the
client composes the sentence.
{
"aps": {
"mutable-content": 1,
"alert": {
"title": "You've been outbid",
"body": "Tap to open the auction"
}
},
"data": {
"type": "bid_outbid",
"auctionId": "a_8123",
"amount": "1850.00",
"currency": "USD",
"itemTitle": "Vintage watch"
}
}Both blocks are required and play different roles. aps is what the system displays if
nobody rewrites anything. Without it, iOS treats the send as a silent push and
nothing appears on screen. data is what the client uses to compose the final
sentence.
4. loc-key and loc-args. The platform already solves this, and almost nobody
uses it. The payload carries a key and arguments, and the system looks the string
up in the app's bundle, with no extension at all:
{
"aps": {
"alert": {
"loc-key": "bid_outbid_body",
"loc-args": ["$1,850.00", "Vintage watch"]
}
}
}On FCM the equivalent is title_loc_key and body_loc_args. It's the cheapest option
of all, and it comes with a catch that decides everything: it resolves against the
bundle's language, which is the system language, not the one the user chose in your
app. It only works if your language switcher changes AppleLanguages in
UserDefaults, which most Flutter apps don't do. They swap the locale on the Dart
side, and the native bundle never finds out.
I went with the third, and the criterion was ownership of the copy. With the app as owner, the sentence follows the installed version, not the one the server assumes. Currency and number formatting come out right per locale without the server knowing each country's convention. And fixing badly worded copy becomes an app release, instead of a migration of every scheduled message.
If your case is the opposite (many messages, changing per campaign, written by people who never open the repository), the server should own it, and the second strategy is the most honest one: it delivers the right language without requiring device registration to be perfect.
The third one's cost is real and worth stating: it requires native code on both sides, and on iOS it requires a new target.
On iOS, translating before display requires an extension
To change a notification's content before it appears, the payload has to ask for permission and you need a Notification Service Extension:
{ "aps": { "mutable-content": 1, "alert": { "title": "…", "body": "…" } } }The alert is still the fallback text from the earlier example: if the extension
fails, that's what shows up.
And there you land in exactly the same problem as the Live Activity: the extension is a separate process and doesn't inherit the language chosen in the app. The solution is the same: the app writes the choice into the App Group container, and the extension reads it before composing the text.
final class NotificationService: UNNotificationServiceExtension {
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler handler: @escaping (UNNotificationContent) -> Void
) {
let content = request.content.mutableCopy() as! UNMutableNotificationContent
// the language comes from the App Group, not from Locale.current
let locale = SharedLocale.readFromAppGroup() ?? "en"
content.title = Strings.title(for: content.userInfo, locale: locale)
content.body = Strings.body(for: content.userInfo, locale: locale)
handler(content)
}
}Worth knowing up front: if the extension runs out of time or fails, the system displays the payload's original content. Which means send a reasonable fallback text in the payload even when you're using keys. Otherwise the failure shows up as an empty notification.
On Android the path is more direct, because you already control display: I centralized notification construction in a single native place that reads the chosen language from shared storage and assembles title, body and image before publishing to the channel.
Topic or token: both, for different things
Topics are for what's collective: "everyone following this auction". Tokens are for what's individual: "you've been outbid". Mixing them is what produces the wrong notification for the wrong person.
And topics have a state trap: subscribing to a topic is a server operation, not an app one. If the user mutes an auction, the source of truth is the server, not the local preference, which kept diverging after reinstalls and device changes. It took me a while to treat "mute" and "favorite" as remote state, and the inconsistencies stopped.
The payload that has to survive a dead app
The case that breaks in production and never in development: the notification arrives, the app is killed, the user taps. The system launches the app from scratch, and the payload that motivated the tap arrives before the widget tree exists.
If you try to navigate at that moment, there's nowhere to navigate to. The fix is to store the last background payload on the native side and hand it over when Dart says it's ready:
Future<void> onAppReady() async {
final pending = await driver.consumePendingOpen();
if (pending != null) router.handle(pending.deepLink);
}Note the consume: deliver and delete. A payload that isn't consumed reopens the same
screen on the next launch, and the user can't understand why the app insists on an
auction that already ended.
What I'd do differently
I'd have registered the language on the device from day one. I started by sending finished text from the server, discovered the language problem when the app got its second locale, and redoing it afterwards cost a new iOS extension and a migration of every scheduled message.
And I'd have written the fake driver before the real ones. I wrote it last, just so I could test. Then I realized that if I'd written it first, it would have defined the interface, instead of the interface being defined by the first push service I happened to integrate.
Mobile app security: obfuscation is the start, not the defense
SPKI pinning with an owner and an alert threshold, device attestation scoped deliberately, an env contract instead of untracking a file, and why obfuscating without a de-obfuscation runbook sabotages your future self.
Read article →Flutter CI/CD: from manual builds to a pipeline you trust
What a mobile pipeline needs to prevent before it needs to do anything: a version gate, signing keys that live on nobody's laptop, a fully pinned toolchain, and why obfuscation symbols are part of the artifact.
Read article →