Firebase Cloud Messaging と通知
Firebase Cloud Messaging を使って Flutter アプリでプッシュ通知を送受信し、フォアグラウンド、バックグラウンド、タップ後の状態に対応します。
「Firebase Cloud Messaging と通知」はCoddyKit上の無料Flutter Mobile Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.0Requesting 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.
AI チューターと学ぶ Dart — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 22
- レッスン
- 88
よくある質問
「Firebase Cloud Messaging と通知」レッスンは無料ですか?
はい。「Firebase Cloud Messaging と通知」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flutter Mobile Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flutter Mobile Developmentコースには全4レッスンが含まれています。
「Firebase Cloud Messaging と通知」で何を学びますか?
Firebase Cloud Messaging を使って Flutter アプリでプッシュ通知を送受信し、フォアグラウンド、バックグラウンド、タップ後の状態に対応します。 ブラウザで直接実行するハンズオンコードでFlutter Mobile Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flutter Mobile Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlutter Mobile Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Firebase Cloud Messaging と通知」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlutter Mobile Developmentレッスンでコードを書いて実行できますか?
はい。すべてのFlutter Mobile Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Firebaseのセットアップと認証
- Cloud Firestoreデータベース
- Cloud StorageとFunctions
- Firebase Cloud Messaging と通知