Skip to main content

Implementation examples

These samples show how to:

  1. Pass the FCM token to AdTrace.setPushToken when it is created or refreshed
  2. Handle Adtrace silent pushes if you also send your own notifications
  3. Initialize Firebase before the Adtrace SDK

If you do not send your own push notifications, you can skip overriding onMessageReceived and only implement token forwarding (onNewToken / fetch token).

warning

When you override onMessageReceived, do not break handling of your own notifications. Test your notifications in the foreground after any change.

FirebaseMessagingService

Call AdTrace.setPushToken(token, context) in onNewToken (and optionally when you fetch the current token). For Adtrace silent payloads you can use Util.isAdTraceUninstallDetectionPayload, or the simple title / body null check described in Setup.

import io.adtrace.sdk.AdTrace;
import io.adtrace.sdk.Util;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onCreate() {
super.onCreate();
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (task.isSuccessful() && task.getResult() != null) {
AdTrace.setPushToken(task.getResult(), getApplicationContext());
}
});
}

@Override
public void onNewToken(String token) {
super.onNewToken(token);
AdTrace.setPushToken(token, getApplicationContext());
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

if (!remoteMessage.getData().isEmpty()) {
Map<String, String> payload = remoteMessage.getData();
if (Util.isAdTraceUninstallDetectionPayload(payload)) {
// Adtrace silent uninstall check. No UI required.
} else {
// Handle your other data payloads here
}
}

if (remoteMessage.getNotification() != null) {
// Handle notification payload here
}
}
}

Simple silent-push check (no Util helper)

Same idea as the SDK README:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().get("title") == null
&& remoteMessage.getData().get("body") == null) {
// Silent push
} else {
// Your notification handling
}
}

Initialize Firebase before Adtrace

Initialize Firebase in your Application class before AdTrace.onCreate().

import android.app.Application;
import com.google.firebase.FirebaseApp;
import io.adtrace.sdk.AdTrace;
import io.adtrace.sdk.AdTraceConfig;

Same idea as the <a href={`${androidLinks.github}#uninstall-tracking`} target="_blank">Android SDK README</a>:

@Override
public void onCreate() {
super.onCreate();

FirebaseApp.initializeApp(this);

String appToken = "{YourAppToken}";
String environment = AdTraceConfig.ENVIRONMENT_SANDBOX;
AdTraceConfig config = new AdTraceConfig(this, appToken, environment);
AdTrace.onCreate(config);
}
}

Next: Test the integration