การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน
ส่งและรับการแจ้งเตือนแบบพุชในแอป Flutter ด้วย Firebase Cloud Messaging พร้อมจัดการสถานะขณะอยู่เบื้องหน้า เบื้องหลัง และเมื่อผู้ใช้แตะ
การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน”
ส่งและรับการแจ้งเตือนแบบพุชในแอป Flutter ด้วย Firebase Cloud Messaging พร้อมจัดการสถานะขณะอยู่เบื้องหน้า เบื้องหลัง และเมื่อผู้ใช้แตะ คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม
ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่า Firebase และการยืนยันตัวตน
- ฐานข้อมูล Cloud Firestore
- Cloud Storage และฟังก์ชัน
- การส่งข้อความบนคลาวด์ของ Firebase และการแจ้งเตือน