Firebase Cloud Messaging والإشعارات
أرسل الإشعارات الفورية واستقبلها في تطبيق Flutter باستخدام Firebase Cloud Messaging، مع التعامل مع حالات المقدمة والخلفية والنقر.
Firebase Cloud Messaging والإشعارات درس مجاني في Flutter Mobile Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 Cloud Messaging والإشعارات» مجاني؟
نعم — نص درس «Firebase Cloud Messaging والإشعارات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flutter Mobile Development، انتقل إلى CoddyKit PRO. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.
ماذا ستتعلم في «Firebase Cloud Messaging والإشعارات»؟
أرسل الإشعارات الفورية واستقبلها في تطبيق Flutter باستخدام Firebase Cloud Messaging، مع التعامل مع حالات المقدمة والخلفية والنقر. تتمرن على Flutter Mobile Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flutter Mobile Development؟
لا تُشترط خبرة سابقة. Flutter Mobile Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «Firebase Cloud Messaging والإشعارات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flutter Mobile Development هذا؟
نعم. كل درس في Flutter Mobile Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد Firebase والمصادقة
- قاعدة بيانات Cloud Firestore
- التخزين السحابي والوظائف
- Firebase Cloud Messaging والإشعارات