All Collections
2. Define push_token to mobile analytics
Embedding Firebase + AppsFlyer (Android)
Embedding Firebase + AppsFlyer (Android)
M
Written by Marat Zhanabekov
Updated over a week ago

Goals

The goal of this guide is to set up:

  • basic push notifications handling via Firebase Cloud Messaging SDK

  • sending registration_id (push_token in Firebase Cloud Messaging system) to the AppsFlyer system

Android

Prerequisites

It is expected that you’ve already implemented:

Appsflyer SDK

  1. Send push_token event with token event property to AppsFlyer via AppsFlyer SDK log event method on every app start. To retrieve the current token, you need to call FirebaseMessaging.getInstance().getToken() method.

    /*
    * Event parameters for push_token event
    */
    Map<String, Object> eventParameters0 = new HashMap<String, Object>();
    eventParameters0.put("token", token); // that should be `registration_id` obtained from Firebase

    /*
    * Send push_token event.
    * Trigger: on every app start
    */
    AppsFlyerLib.getInstance().logEvent(getApplicationContext(), "push_token", eventParameters0);

  2. We strongly recommend triggering the push_token event logging on EVERY app start.

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

    Android 13 introduces a 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)
    }
    }
Did this answer your question?