Sobes.tech
Junior — Middle

How can push notifications be implemented when the app is not running in device memory?

sobes.tech AI

Answer from AI

To receive push notifications when the app is not running in memory, cloud messaging services like Firebase Cloud Messaging (FCM) are typically used.

How it works:

  1. The server sends a push notification via FCM.
  2. FCM delivers the notification to the device.
  3. If the app is not running, the Android system automatically displays the notification in the panel.
  4. Tapping on the notification launches the app.

For this, the app needs to:

  • Register a service that will receive messages.
  • Handle receiving notifications in the service, considering that the app may be in the background or closed.

Example of registering a service in AndroidManifest.xml:

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

Thus, push notifications are delivered and displayed by the system, even if the app is not running.