با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برای شروع کار با FCM، سادهترین مورد استفاده را بسازید: ارسال یک پیام اعلان آزمایشی از سازنده Notifications به یک دستگاه توسعهدهنده زمانی که برنامه در پسزمینه دستگاه است. این صفحه تمام مراحل را برای دستیابی به این هدف، از راهاندازی تا تأیید، فهرست میکند - اگر برنامه Flutter را برای FCM راهاندازی کرده باشید، ممکن است مراحلی را که قبلاً انجام دادهاید پوشش دهد.
از ریشه پروژه Flutter خود، دستور زیر را برای نصب افزونه اجرا کنید:
flutterpubaddfirebase_messaging
پس از تکمیل، برنامه Flutter خود را بازسازی کنید:
flutterrun
به رمز ثبت نام دسترسی پیدا کنید
برای ارسال پیام به یک دستگاه خاص، باید رمز ثبت نام آن دستگاه را بدانید. از آنجایی که برای تکمیل این آموزش باید توکن را در قسمتی در کنسول Notifications وارد کنید، پس از بازیابی آن حتماً آن را کپی کنید یا به طور ایمن ذخیره کنید.
برای بازیابی رمز ثبت نام فعلی برای یک نمونه برنامه، getToken() را فراخوانی کنید. اگر مجوز اعلان داده نشده باشد، این روش از کاربر مجوزهای اعلان می خواهد. در غیر این صورت، توکن را برمی گرداند یا به دلیل خطا، آینده را رد می کند.
اگر این اولین پیام شماست، ایجاد اولین کمپین خود را انتخاب کنید.
پیامهای اعلان Firebase را انتخاب کنید و ایجاد را انتخاب کنید.
در غیر این صورت، در تب Campaigns ، کمپین جدید و سپس Notifications را انتخاب کنید.
متن پیام را وارد کنید تمام فیلدهای دیگر اختیاری هستند.
از قسمت سمت راست گزینه Send test message را انتخاب کنید.
در فیلد با عنوان افزودن نشانه ثبت FCM ، نشانه ثبت نامی را که در بخش قبلی این راهنما به دست آورده اید، وارد کنید.
تست را انتخاب کنید.
پس از انتخاب تست ، دستگاه مشتری مورد نظر (با برنامه در پسزمینه) باید اعلان را دریافت کند.
برای اطلاعات بیشتر در مورد تحویل پیام به برنامه خود، به داشبورد گزارش FCM مراجعه کنید، که تعداد پیامهای ارسال شده و باز شده در دستگاههای Apple و Android را به همراه دادههای «impressions» (اعلانهایی که کاربران مشاهده میکنند) را برای برنامههای Android ثبت میکند.
مدیریت تعامل
وقتی کاربران روی یک اعلان ضربه می زنند، رفتار پیش فرض در اندروید و iOS باز کردن برنامه است. اگر برنامه خاتمه یابد، راه اندازی می شود و اگر در پس زمینه باشد، به پیش زمینه می آید.
بسته به محتوای یک اعلان، ممکن است بخواهید هنگام باز شدن برنامه، تعامل کاربر را کنترل کنید. برای مثال، اگر یک پیام چت جدید با استفاده از اعلان ارسال شود و کاربر آن را انتخاب کند، ممکن است بخواهید با باز شدن برنامه، مکالمه خاصی را باز کنید.
بسته firebase-messaging دو راه برای مدیریت این تعامل ارائه میدهد:
getInitialMessage() : اگر برنامه از حالت پایان یافته باز شود، این متد Future حاوی RemoteMessage برمی گرداند. پس از مصرف، RemoteMessage حذف خواهد شد.
onMessageOpenedApp : Stream که وقتی برنامه از حالت پسزمینه باز میشود، یک RemoteMessage ارسال میکند.
برای اطمینان از تجربه ای روان برای کاربران خود، باید هر دو سناریو را مدیریت کنید. مثال کد زیر چگونگی دستیابی به این امر را شرح می دهد:
classApplicationextendsStatefulWidget{@overrideState<StatefulWidget>createState()=>_Application();}class_ApplicationextendsState<Application>{// In this example, suppose that all messages contain a data field with the key 'type'.Future<void>setupInteractedMessage()async{// Get any messages which caused the application to open from// a terminated state.RemoteMessage?initialMessage=awaitFirebaseMessaging.instance.getInitialMessage();// If the message also contains a data property with a "type" of "chat",// navigate to a chat screenif(initialMessage!=null){_handleMessage(initialMessage);}// Also handle any interaction when the app is in the background via a// Stream listenerFirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);}void_handleMessage(RemoteMessagemessage){if(message.data['type']=='chat'){Navigator.pushNamed(context,'/chat',arguments:ChatArguments(message),);}}@overridevoidinitState(){super.initState();// Run code required to handle interacted messages in an async function// as initState() must not be asyncsetupInteractedMessage();}@overrideWidgetbuild(BuildContextcontext){returnText("...");}}
نحوه مدیریت تعامل به تنظیمات برنامه شما بستگی دارد. مثال بالا یک مثال اساسی از استفاده از StatefulWidget را نشان می دهد.
مراحل بعدی
ارسال پیام به برنامه های پیش زمینه
هنگامی که با موفقیت پیامهای اعلان ارسال کردید، در حالی که برنامه شما در پسزمینه است، برای شروع ارسال به برنامههای پیشزمینه ، به دریافت پیامها در برنامه Flutter مراجعه کنید.
فراتر از پیام های اطلاع رسانی بروید
برای افزودن سایر رفتارهای پیشرفته تر به برنامه خود، به اجرای سرور نیاز دارید.
تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی."],[],[],null,["\u003cbr /\u003e\n\nTo get started with FCM, build out the simplest use case: sending a\ntest notification message from the\n[Notifications composer](//console.firebase.google.com/project/_/notification) to a development device\nwhen the app is in the background on the device.\nThis page lists all the steps to achieve this, from setup to verification\n--- it may cover steps you already completed if you\nhave [set up a Flutter app](/docs/cloud-messaging/flutter/client)\nfor FCM.\n| **Important:** This guide focuses on the background case. If you want to receive messages when your app is in the foreground as well, see also [Receive Messages in a Flutter App](/docs/cloud-messaging/flutter/receive).\n\nInstall the FCM plugin\n\n1. [Install and initialize the Firebase SDKs for Flutter](/docs/flutter/setup)\n if you haven't already done so.\n\n2. From the root of your Flutter project, run the following command to install\n the plugin:\n\n flutter pub add firebase_messaging\n\n3. Once complete, rebuild your Flutter application:\n\n flutter run\n\nAccess the registration token\n\nTo send a message to a specific device, you need to know that device's\nregistration token. Because you'll need to enter the token in a field in the\nNotifications console to complete this tutorial, make sure to copy the token\nor securely store it after you retrieve it.\n\nTo retrieve the current registration token for an app instance, call\n`getToken()`. If notification permission has not been granted, this method will\nask the user for notification permissions. Otherwise, it returns a token or\nrejects the future due to an error. \n\n final fcmToken = await FirebaseMessaging.instance.getToken();\n\nSend a test notification message\n\n1. Install and run the app on the target device. On Apple devices, you'll need\n to accept the request for permission to receive remote notifications.\n\n2. Make sure the app is in the background on the device.\n\n3. In the Firebase console, open the [Messaging page](https://console.firebase.google.com/project/_/messaging/).\n\n4. If this is your first message, select **Create your first\n campaign**.\n\n 1. Select **Firebase Notification messages** and select **Create**.\n5. Otherwise, on the **Campaigns** tab, select **New campaign**\n and then **Notifications**.\n\n6. Enter the message text. All other fields are optional.\n\n7. Select **Send test message** from the right pane.\n\n8. In the field labeled **Add an FCM registration token**, enter the registration\n token you obtained in a previous section of this guide.\n\n9. Select **Test**.\n\nAfter you select **Test**, the targeted client device (with the app in\nthe background) should receive the notification.\n\nFor insight into message delivery to your app, see the\n[FCM reporting dashboard](//console.firebase.google.com/project/_/notification/reporting),\nwhich records the number of messages sent and opened on Apple and Android\ndevices, along with data for \"impressions\" (notifications seen by users) for\nAndroid apps.\n\nHandling interaction\n\nWhen users tap a notification, the default behavior on both Android \\& iOS is to open the application. If the application is terminated,\nit will be started, and if it is in the background, it will be brought to the foreground.\n\nDepending on the content of a notification, you may want to handle the user's interaction when the application\nopens. For example, if a new chat message is sent using a notification and the user selects it, you may want to\nopen the specific conversation when the application opens.\n\nThe `firebase-messaging` package provides two ways to handle this interaction:\n\n1. `getInitialMessage()`: If the application is opened from a terminated state, this method returns a `Future` containing a `RemoteMessage`. Once consumed, the `RemoteMessage` will be removed.\n2. `onMessageOpenedApp`: A `Stream` which posts a `RemoteMessage` when the application is opened from a background state.\n\nTo ensure a smooth experience for your users, you should handle both scenarios. The code example\nbelow outlines how this can be achieved: \n\n class Application extends StatefulWidget {\n @override\n State\u003cStatefulWidget\u003e createState() =\u003e _Application();\n }\n\n class _Application extends State\u003cApplication\u003e {\n // In this example, suppose that all messages contain a data field with the key 'type'.\n Future\u003cvoid\u003e setupInteractedMessage() async {\n // Get any messages which caused the application to open from\n // a terminated state.\n RemoteMessage? initialMessage =\n await FirebaseMessaging.instance.getInitialMessage();\n\n // If the message also contains a data property with a \"type\" of \"chat\",\n // navigate to a chat screen\n if (initialMessage != null) {\n _handleMessage(initialMessage);\n }\n\n // Also handle any interaction when the app is in the background via a\n // Stream listener\n FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);\n }\n\n void _handleMessage(RemoteMessage message) {\n if (message.data['type'] == 'chat') {\n Navigator.pushNamed(context, '/chat',\n arguments: ChatArguments(message),\n );\n }\n }\n\n @override\n void initState() {\n super.initState();\n\n // Run code required to handle interacted messages in an async function\n // as initState() must not be async\n setupInteractedMessage();\n }\n\n @override\n Widget build(BuildContext context) {\n return Text(\"...\");\n }\n }\n\nHow you handle interaction depends on your application setup. The example above\nshows a basic example of using a `StatefulWidget`.\n\nNext steps\n\nSend messages to foregrounded apps\n\nOnce you have successfully sent notification messages while your app is in\nthe background, see\n[Receive Messages in a Flutter App](/docs/cloud-messaging/flutter/receive)\nto get started sending to foregrounded apps.\n\nGo beyond notification messages\n\nTo add other, more advanced behavior to your app, you'll need a\n[server implementation](/docs/cloud-messaging/server).\n\nThen, in your app client:\n\n- [Receive messages](/docs/cloud-messaging/flutter/receive)\n- [Subscribe to message topics](/docs/cloud-messaging/flutter/topic-messaging)"]]