onBackgroundMessage
- Flutter course
- Advanced
- Lesson 29/69
- 4 min
Handle background notifications with onBackgroundMessage.
What is onBackgroundMessage?
The onBackgroundMessage method registers a top-level function to handle notifications that arrive while the app is in the background. This function runs in a separate isolate; therefore it cannot be defined inside a class or as an anonymous function.
Future<void> handler(RemoteMessage msg) async {
print(msg.title);
}
FirebaseMessaging.onBackgroundMessage(handler);The vm:entry-point pragma
Because the background handler runs in a separate isolate, it can only access its own arguments; it cannot reach the app's main state. Therefore the handler function must be top-level and marked with @pragma('vm:entry-point').
@pragma('vm:entry-point')
Future<void> handler(RemoteMessage msg) async {
print(msg.title);
}Practice Write a background handler
Write a top-level handler function that returns Future<void>, takes a RemoteMessage, and prints message.title. In main, register the handler with FirebaseMessaging.onBackgroundMessage and call it with RemoteMessage('Merhaba').
Starter code
class RemoteMessage {
final String title;
RemoteMessage(this.title);
}
class FirebaseMessaging {
static final FirebaseMessaging instance = FirebaseMessaging._();
FirebaseMessaging._();
static void onBackgroundMessage(Function handler) {}
}
// arka plan işleyici fonksiyonu yaz
// üst düzey ve async olmalı, message.title yazdırsın
void main() async {
// işleyiciyi kaydet
// işleyiciyi RemoteMessage('Merhaba') ile çağır
}Sample solution
class RemoteMessage {
final String title;
RemoteMessage(this.title);
}
class FirebaseMessaging {
static final FirebaseMessaging instance = FirebaseMessaging._();
FirebaseMessaging._();
static void onBackgroundMessage(Function handler) {}
}
Future<void> handler(RemoteMessage message) async {
print(message.title);
}
void main() async {
FirebaseMessaging.onBackgroundMessage(handler);
await handler(RemoteMessage('Merhaba'));
}Expected output
Merhaba
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How should the background notification handler be defined?
- Inside a widget build method
- Only as an anonymous function inside main
- As an instance method inside a class
- As a top-level async function Correct answer
Explanation: Because the background handler runs in a separate isolate, it must be a top-level function; it cannot be defined inside a class or as anonymous.
2 Register the handler
FirebaseMessaging.____;
FirebaseMessaging.onBackgroundMessage(handler);
Explanation: To register the background handler you call FirebaseMessaging.onBackgroundMessage and pass the handler function as a parameter.
3 onBackgroundMessage parts
| Term | Meaning |
|---|---|
onBackgroundMessage |
Background handler registration |
top-level function |
Top-level function |
isolate |
Separate execution environment |
background |
App in background |