با استفاده از Firebase Cloud Messaging پیام ها را دریافت کنید

این راهنما نحوه تنظیم Firebase Cloud Messaging را در برنامه‌های موبایل و کلاینت وب شما شرح می‌دهد تا بتوانید پیام‌ها را به طور قابل اعتمادی دریافت کنید.

برای دریافت پیام‌ها، می‌توانید از سرویسی استفاده کنید که FirebaseMessagingService ارث‌بری می‌کند. سرویس شما باید فراخوانی‌های onMessageReceived و onDeletedMessages لغو کند. برای مثال کامل، به نمونه شروع سریع Firebase Cloud Messaging مراجعه کنید.

onMessageReceived برای اکثر انواع پیام‌ها ارائه می‌شود، به جز موارد استثنای زیر:

  • پیام‌های اعلان زمانی که برنامه شما در پس‌زمینه است، ارسال می‌شوند . در این حالت، اعلان به سینی سیستم دستگاه ارسال می‌شود. با لمس یک اعلان توسط کاربر، به‌طور پیش‌فرض، لانچر برنامه باز می‌شود.

  • پیام‌هایی که هم حاوی اعلان و هم حاوی داده هستند، وقتی در پس‌زمینه دریافت می‌شوند . در این حالت، اعلان به سینی سیستم دستگاه ارسال می‌شود و داده در بخش‌های اضافیِ intent مربوط به اکتیویتیِ لانچر شما ارسال می‌شود.

به طور خلاصه:

حالت برنامه اعلان داده‌ها هر دو
پیش‌زمینه onMessageReceived onMessageReceived onMessageReceived
پیشینه سینی سیستم onMessageReceived اعلان: سینی سیستم داده: در موارد اضافی از هدف.

برای اطلاعات بیشتر در مورد انواع پیام، به اعلان‌ها و پیام‌های داده مراجعه کنید.

تابع فراخوانی onMessageReceived یک پنجره اجرای کوتاه دارد. عوامل زیادی می‌توانند بر طول این پنجره تأثیر بگذارند، از جمله تأخیرهای سیستم عامل، زمان شروع برنامه، مسدود شدن نخ اصلی توسط سایر عملیات یا طولانی شدن بیش از حد فراخوانی‌های قبلی onMessageReceived .

For this reason, you should avoid long-running tasks (such as fetching images from a server to display in a notification) in onMessageReceived and instead schedule a task using WorkManager to handle any tasks that might take more than a couple of seconds to complete. For more information on message priority and how it impacts processing, see Message processing for high and normal priority messages .

ویرایش مانیفست برنامه

برای استفاده از FirebaseMessagingService ، باید موارد زیر را در مانیفست برنامه خود اضافه کنید:

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

توصیه می‌شود برای سفارشی‌سازی ظاهر اعلان‌ها، مقادیر پیش‌فرض را تنظیم کنید. می‌توانید یک آیکون پیش‌فرض سفارشی و یک رنگ پیش‌فرض سفارشی تعیین کنید که هر زمان مقادیر معادل در فایل اعلان تنظیم نشده باشند، اعمال می‌شوند.

این خطوط را داخل تگ application اضافه کنید تا آیکون پیش‌فرض سفارشی و رنگ سفارشی تنظیم شود:

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

اندروید آیکون پیش‌فرض سفارشی را نمایش می‌دهد و از آن استفاده می‌کند.

  • تمام پیام‌های اعلان ارسال شده از آهنگساز Notifications .
  • هر پیام اعلانی که به صراحت آیکون را در بار داده اعلان تنظیم نکند.

اگر یک آیکون پیش‌فرض سفارشی تنظیم نشده باشد و آیکونی در فایل اعلان‌ها (notification payload) تنظیم نشده باشد، اندروید آیکون برنامه را به رنگ سفید نمایش می‌دهد.

نادیده گرفتن onMessageReceived

با بازنویسی (override) متد FirebaseMessagingService.onMessageReceived ، می‌توانید اقداماتی را بر اساس شیء RemoteMessage دریافتی انجام دهید و داده‌های پیام را دریافت کنید:

Kotlin

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: ${remoteMessage.from}")

    // Check if message contains a data payload.
    if (remoteMessage.data.isNotEmpty()) {
        Log.d(TAG, "Message data payload: ${remoteMessage.data}")

        // Check if data needs to be processed by long running job
        if (needsToBeScheduled()) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }

    // Check if message contains a notification payload.
    remoteMessage.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

نادیده گرفتن onDeletedMessages

In some situations, FCM may not deliver a message. This happens when there are too many messages (>100) pending for your app on a particular device at the time it connects or if the device hasn't connected to FCM in more than one month. In these cases, you may receive a callback to FirebaseMessagingService.onDeletedMessages() . When the app instance receives this callback, it should perform a full sync with your app server. If you haven't sent a message to the app on that device within the last 4 weeks, FCM won't call onDeletedMessages() .

مدیریت پیام‌های اعلان در یک برنامه در پس‌زمینه

وقتی برنامه شما در پس‌زمینه است، اندروید پیام‌های اعلان را به سینی سیستم هدایت می‌کند. با لمس اعلان توسط کاربر، به‌طور پیش‌فرض، لانچر برنامه باز می‌شود.

این شامل پیام‌هایی می‌شود که حاوی اعلان و داده هستند (و تمام پیام‌های ارسالی از کنسول اعلان‌ها). در این موارد، اعلان به سینی سیستم دستگاه تحویل داده می‌شود و داده در بخش‌های اضافیِ intent مربوط به اکتیویتیِ لانچر شما تحویل داده می‌شود.

برای اطلاعات بیشتر در مورد ارسال پیام به برنامه خود، به داشبورد گزارش FCM مراجعه کنید، که تعداد پیام‌های ارسالی و باز شده در دستگاه‌های اپل و اندروید را به همراه داده‌های مربوط به «نمایش‌ها» (اعلان‌های مشاهده شده توسط کاربران) برای برنامه‌های اندروید ثبت می‌کند.