Implementation examples
These samples show how to:
- Pass the FCM token to
AdTrace.setPushTokenwhen it is created or refreshed - Handle Adtrace silent pushes if you also send your own notifications
- 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.
- Java
- Kotlin
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
}
}
}
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
public class MyFirebaseMessagingService extends FirebaseMessagingService {
override fun onCreate() {
super.onCreate()
FirebaseMessaging.getInstance().token
.addOnCompleteListener { task ->
if (task.isSuccessful && task.result != null) {
AdTrace.setPushToken(task.result!!, applicationContext)
}
}
}
override fun onNewToken(token: String) {
super.onNewToken(token)
AdTrace.setPushToken(token, applicationContext)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
if (remoteMessage.data.isNotEmpty()) {
val payload = remoteMessage.data
if (Util.isAdTraceUninstallDetectionPayload(payload)) {
// Adtrace silent uninstall check. No UI required.
} else {
// Handle your other data payloads here
}
}
remoteMessage.notification?.let {
// 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().
- Java
- Kotlin
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);
}
}
import android.app.Application
import com.google.firebase.FirebaseApp
import io.adtrace.sdk.AdTrace
import io.adtrace.sdk.AdTraceConfig
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
val appToken = "{YourAppToken}"
val environment = AdTraceConfig.ENVIRONMENT_SANDBOX
val config = AdTraceConfig(this, appToken, environment)
AdTrace.onCreate(config)
}
}
Next: Test the integration