پیام ها را با اطمینان دریافت کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
دریافت مطمئن پیام ها، حیاتی ترین ویژگی هر تجربه پیام رسانی اندرویدی است. همچنین اجرای پیام با توجه به سلامت سیستم و عمر باتری بسیار مهم است. این سند شما را از طریق استراتژیها و ابزارهای کلیدی راهنمایی میکند تا مطمئن شوید که برنامه Android شما پیامها را به طور مداوم، کارآمد و قابل اعتماد دریافت میکند.
مکانیسم های ارسال پیام
بهترین رویکرد برای پیام رسانی مطمئن به نیازهای خاص برنامه شما بستگی دارد. عواملی مانند:
- نیازهای زمان واقعی فعلی
- فرکانس پیام
- محدودیت های باتری
پیش زمینه پیام های زمان واقعی
هنگامی که برنامه شما در پیش زمینه است، کاربر معمولاً حجم بالایی از اطلاعات را انتظار دارد و می خواهد مواردی مانند:
- آیا شخصی که به او پیام داده می شود در دستگاه خود حضور دارد؟
- دارند تایپ می کنند؟
- آیا آنها پیام را خوانده اند؟
راه معمولی برای پشتیبانی از این نوع تبادل اطلاعات بلادرنگ، استفاده از پروتکل سرویس گیرنده-سرور، مانند WebSockets است. WebSocket ها ارتباط دائمی و تمام دوبلکس را بین برنامه شما و سرور فعال می کنند. کتابخانه OKHTTP شامل اجرای پروتکل WebSocket است که می توانید در کلاینت اندروید خود استفاده کنید.
پایگاه داده بیدرنگ Firebase یک بکاند و پیشفرض کلاینت از پیش ساخته شده ارائه میکند که میتواند این نوع ارتباطات را از طرف شما انجام دهد. از WebSockets به صورت داخلی برای ارتباط بلادرنگ بین مشتری و سرور خود استفاده می کند.
پسزمینه پیامهای بیدرنگ
هنگامی که برنامه شما دیگر در پیش زمینه نیست، بسیار مهم است که از انجام کارهایی که بر سلامت سیستم و عمر باتری تأثیر منفی می گذارد اجتناب کنید. از آنجایی که هنوز مهم است که اعلانهای پیام را به طور قابل اعتماد ارسال کنید، توصیه میکنیم از Firebase Cloud Messaging (FCM) استفاده کنید.
FCM یک راه حل پیام رسانی بین پلتفرمی است که به طور موثر اعلان ها و پیام های داده را به دستگاه های Android (و سایر دستگاه ها) ارسال می کند. از لایه حملونقل Android (ATL) برای دستگاههایی استفاده میکند که دارای خدمات Google هستند، به طوری که برنامه شما میتواند در زمانی که دیگر اجرا نمیشود از تغییرات مطلع شود. به موقع بودن تحویل پیام به وضعیت دستگاه، اولویت پیام و اینکه آیا برنامه شما به دلیل حالت خواب یا آماده به کار برنامه تحت محدودیت است یا خیر، بستگی دارد.
افزایش قابلیت اطمینان تحویل پیام
برای اینکه ارسال پیام خود را قوی تر کنید، این استراتژی ها را در نظر بگیرید:
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2024-10-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2024-10-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Receive messages reliably\n\nReceiving messages reliably is the most critical feature of any Android\nmessaging experience. It's also important to implement messaging with an eye on\nsystem health and battery life. This document guides you through the key\nstrategies and tools to ensure your Android app receives messages consistently,\nefficiently, and reliably.\n\nMessage delivery mechanisms\n---------------------------\n\nThe best approach for reliable messaging depends on your app's specific\nrequirements. Consider factors like:\n\n- Current real-time needs\n- Message frequency\n- Battery constraints\n\n### Foreground real-time messaging\n\nWhen your app is in the foreground, the user typically expects a reasonably high\nvolume of information, and wants to know things like:\n\n- Is the person being messaged present on their device?\n- Are they typing?\n- Have they read the message?\n\nThe typical way to support this sort of real-time data exchange is to use a\nclient-server protocol, such as [WebSockets](https://en.wikipedia.org/wiki/WebSocket). WebSockets enable\npersistent, full-duplex communication between your app and a server. The [OKHTTP\nlibrary](https://square.github.io/okhttp/) includes an [implementation of the WebSocket\nprotocol](https://square.github.io/okhttp/3.x/okhttp/okhttp3/WebSocket.html) that you can use in your Android client.\n\nThe [Firebase Realtime Database](https://firebase.google.com/docs/database) provides a prebuilt backend and\nclient frontend that can handle this sort of communication on your behalf. It\nuses WebSockets internally for real-time communication between its client and\nserver.\n\n### Background real-time messaging\n\nWhen your app is no longer in the foreground, it's critical to avoid doing\nthings that adversely impact system health and battery life. Since it's still\nimportant to deliver message notifications reliably, we recommend making use of\n[Firebase Cloud Messaging (FCM)](https://firebase.google.com/products/cloud-messaging).\n\nFCM is a cross-platform messaging solution that efficiently sends notifications\nand data messages to Android (and other) devices. It leverages the Android\nTransport Layer (ATL) for devices that have Google services, so that your app\ncan be notified of changes when it is no longer running. The timeliness of\nmessage delivery depends on the state of the device, the priority of the\nmessage, and whether your app is subject to restrictions because of [doze](/training/monitoring-device-state/doze-standby#understand_doze) or\n[app standby](/training/monitoring-device-state/doze-standby#understand_app_standby).\n\n- [Learn how to integrate FCM into your Android project](https://firebase.google.com/docs/android/setup)\n- [Explore the FCM (server) API and its capabilities](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)\n\nEnhance message delivery reliability\n------------------------------------\n\nTo make your message delivery even more robust, consider these strategies:\n\n- Use [`WorkManager`](/develop/background-work/background-tasks/persistent/getting-started) to [periodically wake up your app](/develop/background-work/background-tasks/persistent/getting-started/define-work#schedule_periodic_work) to check for new messages when the device has [network connectivity](/develop/background-work/background-tasks/persistent/getting-started/define-work#work-constraints), and (ideally) when the device is connected to a charger.\n- Get [insight into FCM delivery](https://firebase.google.com/docs/cloud-messaging/understand-delivery?platform=android) with tools such as the [Firebase console](https://firebase.google.com/console) and [Android SDK delivery\n metrics](https://firebase.google.com/docs/reference/fcmdata/rest/v1beta1/projects.androidApps.deliveryData) from the [FCM Data API](https://firebase.google.com/docs/reference/fcmdata/rest).\n- Use logging mechanisms and tools like [Firebase Crashlytics](https://firebase.google.com/docs/crashlytics) to monitor and troubleshoot message delivery issues."]]