Prerequisites

It is expected that you’ve already implemented:

Firebase Cloud Messaging SDK

  1. Make sure that you’ve implemented Firebase Cloud Messaging SDK according to this instruction.

  2. Edit your App Manifest:

    1. A service that extends FirebaseMessagingService. This is required if you want to do any message handling beyond receiving notifications on apps in the background. To receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on, you must extend this service:

      <service 
      android:name=".java.MyFirebaseMessagingService"
      android:exported="false">
      <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT"/>
      </intent-filter>
      </service>
    2. Within the application component, metadata elements to set a default notification icon and color. Android uses these values whenever incoming messages do not explicitly set icon or color:

      <meta-data
      android:name="com.google.firebase.messaging.default_notification_icon"
      android:resource="@drawable/ic_stat_ic_notification" /> <meta-data
      android:name="com.google.firebase.messaging.default_notification_color"
      android:resource="@color/colorAccent" />
    3. Refer to this part of Firebase Push Cloud Messaging documentation for details

  3. [Optional] Request runtime notification permission on Android 13+.

    Android 13 introduces new runtime permission for showing notifications. This affects all apps running on Android 13 or higher that use FCM notifications.

    By default, the FCM SDK (version 23.0.6 or higher) includes the [POST_NOTIFICATIONS](<https://developer.android.com/reference/android/Manifest.permission#POST_NOTIFICATIONS>) permission defined in the manifest. However, your app will also need to request the runtime version of this permission via the constant, android.permission.POST_NOTIFICATIONS. Your app will not be allowed to show notifications until the user has granted this permission.

    To request the new runtime permission:

    // Declare the launcher at the top of your Activity/Fragment: private val requestPermissionLauncher = registerForActivityResult(      ActivityResultContracts.RequestPermission() 
    ) { isGranted: Boolean ->
    if (isGranted) {
    // FCM SDK (and your app) can post notifications.
    } else {
    // TODO: Inform user that that your app will not show notifications.
    }
    }
    // ...
    private fun askNotificationPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
    ) {
    // FCM SDK (and your app) can post notifications.
    } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
    // TODO: display an educational UI explaining to the user the features that will be enabled
    // by them granting the POST_NOTIFICATION permission. This UI should provide the user
    // "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
    // If the user selects "No thanks," allow the user to continue without notifications.
    } else {
    // Directly ask for the permission requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
    }
    }

Appmetrica SDK

  1. Set the onNewToken callback that sends push_token event with {”token”: <registartion_id_here>} event parameter to Appmetrica via Appmetrica SDK method YandexMetrica.reportEvent:

    /** 
    * There are two scenarios when onNewToken is called:
    * 1) When a new token is generated on initial app startup
    * 2) Whenever an existing token is changed * Under #2, there are three scenarios when the existing token is changed:
    * A) App is restored to a new device
    * B) User uninstalls/reinstalls the app
    * C) User clears app data
    */
    @Override
    public void onNewToken(@NonNull String token) {
    String eventParameters = "{\\"token\\":\\"${token}\\"}";
    YandexMetrica.reportEvent("push_token", eventParameters);
    }

Did this answer your question?