0Pricing
Flutter Mobile Development · Lezione

Firebase Cloud Messaging e notifiche

Invii e riceva notifiche push nella Sua app Flutter usando Firebase Cloud Messaging, gestendo gli stati in primo piano, in background e dopo il tocco.

Firebase Cloud Messaging e notifiche è una lezione Flutter Mobile Development gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flutter Mobile Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flutter Mobile Development include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why Push Notifications?

Firebase Cloud Messaging (FCM) lets your server send push notifications to users even when the app is closed. It is key for re-engagement and real-time alerts.

Adding the Package

Add firebase_messaging to your dependencies. You should already have firebase_core set up from earlier lessons.

dependencies:
  firebase_core: ^2.0.0
  firebase_messaging: ^14.0.0

Requesting Permission

On iOS (and Android 13+) you must request notification permission from the user.

final messaging = FirebaseMessaging.instance;
await messaging.requestPermission(
  alert: true, badge: true, sound: true,
);

The Device Token

Each device gets a unique FCM token. Send it to your server so you can target that device.

final token = await messaging.getToken();
print('FCM token: ' + (token ?? 'none'));

Token Refresh

Tokens can change. Listen for refreshes and update your server.

messaging.onTokenRefresh.listen((newToken) {
  sendTokenToServer(newToken);
});

Three Message States

You must handle messages in three situations:

  • Foreground — app open
  • Background — app minimized
  • Terminated — app closed

Foreground Messages

When the app is open, FCM does not show a system notification automatically — you receive the data and display it yourself.

FirebaseMessaging.onMessage.listen((message) {
  final title = message.notification?.title ?? '';
  showLocalNotification(title);
});

Background Handler

Register a top-level background handler before runApp. It must be a top-level function.

Future<void> bgHandler(RemoteMessage m) async {
  print('Background: ' + (m.messageId ?? ''));
}

void main() {
  FirebaseMessaging.onBackgroundMessage(bgHandler);
}

Handling a Tapped Notification

When the user taps a notification that opened the app, route them to the right screen.

FirebaseMessaging.onMessageOpenedApp.listen((message) {
  final route = message.data['route'];
  if (route != null) navigator.pushNamed(route);
});

Topic Subscriptions

Subscribe devices to topics to broadcast to groups without managing individual tokens.

await messaging.subscribeToTopic('news');
await messaging.unsubscribeFromTopic('news');

Local Notifications for Display

Pair FCM with flutter_local_notifications to show rich notifications while in the foreground.

final plugin = FlutterLocalNotificationsPlugin();
await plugin.show(0, 'Title', 'Body', notificationDetails);

Quick Check

What is true about a notification received while the app is in the foreground?

Recap

You learned Firebase Cloud Messaging:

  • Permissions and device tokens
  • Foreground, background and terminated handling
  • Tapped-notification routing and topics
  • Pairing with local notifications

Now your app can re-engage users with timely pushes.

Domande Frequenti

La lezione «Firebase Cloud Messaging e notifiche» è gratuita?

Sì — il testo completo di «Firebase Cloud Messaging e notifiche» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flutter Mobile Development, passa a CoddyKit PRO. Il corso Flutter Mobile Development include 4 lezioni in totale.

Cosa imparerò in «Firebase Cloud Messaging e notifiche»?

Invii e riceva notifiche push nella Sua app Flutter usando Firebase Cloud Messaging, gestendo gli stati in primo piano, in background e dopo il tocco. Eserciti Flutter Mobile Development con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flutter Mobile Development?

Non è richiesta alcuna esperienza precedente. Flutter Mobile Development su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Firebase Cloud Messaging e notifiche»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flutter Mobile Development?

Sì. Ogni lezione Flutter Mobile Development include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Configurazione e autenticazione con Firebase
  2. Database Cloud Firestore
  3. Cloud Storage e Functions
  4. Firebase Cloud Messaging e notifiche
← Torna a Flutter Mobile Development