PushHandler class
Handles FCM / APNs push messages using firebase_messaging.
Quick-start
// 1. Initialize Firebase in main():
await Firebase.initializeApp();
// 2. Register background handler BEFORE runApp():
PushHandler.handleBackground();
// 3. Set up the handler:
await PushHandler.instance.initialize(
onMessage: (msg) {
// Foreground message — show an in-app banner
InAppBannerService.show(context, InAppBannerConfig(
message: msg.body ?? '',
));
},
onMessageOpenedApp: (msg) {
// User tapped notification — navigate
router.push(msg.data['route'] as String? ?? '/');
},
onTokenRefresh: (token) {
if (token != null) api.updateDevicePushToken(token);
},
);
// 4. Get the current FCM token:
final token = await PushHandler.instance.getToken();
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getToken(
) → Future< String?> -
Returns the current FCM registration token, or
nullif unavailable. -
initialize(
{required void onMessage(PushMessage message), required void onMessageOpenedApp(PushMessage message), void onTokenRefresh(String? token)?}) → Future< void> - Initializes FCM message handling.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
requestPermission(
) → Future< bool> - Requests push notification permission (iOS / web).
-
resetForTesting(
) → void - Resets to uninitialised state.
-
setCallbacksForTesting(
{required void onMessage(PushMessage), required void onMessageOpenedApp(PushMessage)}) → void - Sets up message callbacks directly, bypassing Firebase SDK calls.
-
simulateMessage(
PushMessage message) → void - Simulates an incoming foreground push message.
-
simulateOpenedApp(
PushMessage message) → void - Simulates the user tapping a notification to open the app.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → PushHandler
-
The shared singleton instance.
no setter
Static Methods
-
handleBackground(
) → void - Registers the top-level background message handler.