Unity SDK - Uninstall Tracking

Uninstall Tracking

Track app uninstalls to measure user retention and optimize re-engagement campaigns by integrating push notification services with the Singular SDK.

Important: Google deprecated GCM APIs in April 2018. Use Firebase Cloud Messaging (FCM) for all Android uninstall tracking implementations.

Android Uninstall Tracking

Prerequisites

Before implementing uninstall tracking in your Unity app, configure your app in the Singular platform following the guide Setting Up Android Uninstall Tracking.


System Requirements

Uninstall tracking requires Firebase Cloud Messaging and specific device configurations.

FCM Requirements (source):

  • Android Version: Devices must run Android 4.1 (API 16) or higher
  • Google Play Services: Devices must have the Google Play Store app installed
  • Emulator Support: Android 4.1+ emulators with Google APIs are supported
  • Distribution: Apps can be distributed outside the Google Play Store while still supporting uninstall tracking

Note: Users on unsupported Android versions or devices without Google Play Services will not be tracked for uninstalls.


Implementation Steps

Step 1: Integrate Firebase Cloud Messaging

Set up Firebase Cloud Messaging in your Unity app if not already configured.

Follow Google's official guide to Set up Firebase Cloud Messaging for Unity. This includes:

  1. Add Firebase to your Unity project
  2. Import the Firebase Messaging Unity package
  3. Configure Firebase settings for Android
  4. Request notification permissions (Android 13+)

Step 2: Configure AndroidManifest.xml

Register your Firebase Messaging Service in the Android manifest to receive FCM messages.

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

Important: Replace .java.MyFirebaseMessagingService with the fully qualified name of your class that extends FirebaseMessagingService.


Step 3: Register FCM Device Token

Retrieve the FCM device token and send it to Singular for uninstall tracking before SDK initialization.

C#
using UnityEngine;
using Singular;
using Firebase.Messaging;
using System.Threading.Tasks;

public class UninstallTrackingManager : MonoBehaviour
{
    async void Start()
    {
        // Initialize Firebase
        await InitializeFirebase();

        // Get FCM token and register it with Singular
        await RegisterFCMToken();

        // Initialize Singular SDK after registering the token
        SingularSDK.InitializeSingularSDK();
    }

    private async Task InitializeFirebase()
    {
        var dependencyStatus = await Firebase.FirebaseApp.CheckAndFixDependenciesAsync();

        if (dependencyStatus == Firebase.DependencyStatus.Available)
        {
            Debug.Log("Firebase is ready");
        }
        else
        {
            Debug.LogError($"Could not resolve Firebase dependencies: {dependencyStatus}");
        }
    }

    private async Task RegisterFCMToken()
    {
        try
        {
            // Get FCM token
            string token = await Firebase.Messaging.FirebaseMessaging.GetTokenAsync();

            if (!string.IsNullOrEmpty(token))
            {
                // Register token with Singular BEFORE SDK initialization
                SingularSDK.RegisterTokenForUninstall(token);
                Debug.Log($"FCM token registered with Singular: {token}");
            }
            else
            {
                Debug.LogWarning("FCM token is empty");
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Error getting FCM token: {ex.Message}");
        }
    }
}

Critical: The RegisterTokenForUninstall() method must be called beforeSingularSDK.InitializeSingularSDK(). Registering the token after initialization will not enable uninstall tracking.


Step 4: Handle Token Refresh

Update the FCM token with Singular whenever it refreshes to maintain accurate uninstall tracking.

C#
using UnityEngine;
using Singular;
using Firebase.Messaging;

public class FCMTokenRefreshHandler : MonoBehaviour
{
    void Start()
    {
        // Subscribe to token refresh event
        Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    }

    void OnDestroy()
    {
        // Unsubscribe when destroyed
        Firebase.Messaging.FirebaseMessaging.TokenReceived -= OnTokenReceived;
    }

    private void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs e)
    {
        Debug.Log($"New FCM token received: {e.Token}");

        // Update token with Singular
        SingularSDK.RegisterTokenForUninstall(e.Token);

        // Also send token to your server if needed
        SendTokenToServer(e.Token);
    }

    private void SendTokenToServer(string token)
    {
        // Implement your server communication logic here
        Debug.Log($"Sending token to server: {token}");
    }
}

Best Practice: FCM tokens can refresh at any time (app updates, device restore, etc.). Always subscribe to the TokenReceived event to keep Singular updated with the latest token.


iOS Uninstall Tracking

Prerequisites

Uninstall tracking on iOS is based on Apple Push Notification service (APNs) technology.

If your app doesn't support push notifications, see Apple's guide to Registering Your App with APNs.


Registering APNS Device Token

Pass the device token returned from APNs using the RegisterTokenForUninstall method after the SDK is initialized.

Method Signature:

public static void RegisterTokenForUninstall(string APNSToken)

Parameters:

  • APNSToken: The device token returned from APNs as a hexadecimal string

Token Format: The APNS token is usually binary data in its native form, but you must pass it to Singular as a hexadecimal string representation.


Usage Example

Register the APNS device token with Singular for iOS uninstall tracking.

C#
using UnityEngine;
using Singular;

#if UNITY_IOS
using Unity.Notifications.iOS;
#endif

public class iOSUninstallTracking : MonoBehaviour
{
    void Start()
    {
        #if UNITY_IOS
        // Initialize Singular SDK first
        SingularSDK.InitializeSingularSDK();

        // Request notification authorization
        RequestNotificationAuthorization();
        #endif
    }

    #if UNITY_IOS
    private void RequestNotificationAuthorization()
    {
        var authorizationOption = AuthorizationOption.Alert | 
                                  AuthorizationOption.Badge | 
                                  AuthorizationOption.Sound;

        using (var req = new AuthorizationRequest(authorizationOption, true))
        {
            while (!req.IsFinished)
            {
                // Wait for authorization
            }

            string deviceToken = req.DeviceToken;

            if (!string.IsNullOrEmpty(deviceToken))
            {
                // Convert device token to hex string and register with Singular
                SingularSDK.RegisterTokenForUninstall(deviceToken);
                Debug.Log($"APNS token registered: {deviceToken}");
            }
            else
            {
                Debug.LogWarning("Failed to get APNS device token");
            }
        }
    }
    #endif
}

Example Token:

// Pass the APNS token as a hex-string
SingularSDK.RegisterTokenForUninstall("ba85ab31a7c7f5c2f012587f29fb0e596d4b67e7b7b2838fa1a8582c1f7dbdee");

Platform Difference: Unlike Android where the token must be registered before SDK initialization, iOS tokens should be registered after SDK initialization.


Verification and Troubleshooting

Verify Implementation

Confirm uninstall tracking is working correctly.

  1. Check Logs: Verify token registration appears in your Unity console logs
  2. Test Token Generation: Ensure tokens are generated on first app launch
  3. Monitor Dashboard: Check Singular dashboard for uninstall tracking data after 24-48 hours
  4. Test Token Refresh: Clear app data and verify token updates correctly

Common Issues

  • Android Token Not Generated: Verify Firebase dependencies are correctly added and Firebase is configured in your Unity project
  • Token Not Updating: Check that you've subscribed to the TokenReceived event for Android or properly handle APNs callbacks for iOS
  • Missing Data: Ensure devices meet platform requirements (Android 4.1+ with Google Play Services, iOS with APNs support)
  • Configuration Error: Confirm uninstall tracking is enabled in Singular platform settings
  • Initialization Order: For Android, verify RegisterTokenForUninstall() is called before InitializeSingularSDK()

Additional Resources: For detailed troubleshooting, see the Android Uninstall Tracking Setup Guide and Firebase Cloud Messaging for Unity Documentation.