0Pricing
Flutter Mobile Development · 课时

Firebase 云消息传递与通知

使用 Firebase Cloud Messaging 在 Flutter 应用中发送和接收推送通知,并处理前台、后台和点击后的状态。

Firebase 云消息传递与通知 是 CoddyKit 上的免费 Flutter Mobile Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flutter Mobile Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flutter Mobile Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「Firebase 云消息传递与通知」课时是免费的吗?

是的 — 「Firebase 云消息传递与通知」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flutter Mobile Development 课程的其余内容,请升级到 CoddyKit PRO。 Flutter Mobile Development 课程共包含 4 节课。

「Firebase 云消息传递与通知」这节课中我会学到什么?

使用 Firebase Cloud Messaging 在 Flutter 应用中发送和接收推送通知,并处理前台、后台和点击后的状态。 你通过在浏览器中直接运行的动手代码来练习 Flutter Mobile Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flutter Mobile Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flutter Mobile Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Firebase 云消息传递与通知」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flutter Mobile Development 课中编写并运行代码吗?

能。每节 Flutter Mobile Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Firebase 配置与身份验证
  2. Cloud Firestore 数据库
  3. 云存储与云函数
  4. Firebase 云消息传递与通知
← 返回 Flutter Mobile Development