Skip to content

Instantly share code, notes, and snippets.

@benjaminths benjaminths/BaseActivity.java Secret
Last active Jul 2, 2019

Embed
What would you like to do?
package com.batchlabs.android.firebasebatchintegration;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
public class BaseActivity extends Activity
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FirebaseBatchIntegration.handleIntent(getApplicationContext(), getIntent());
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
FirebaseBatchIntegration.handleIntent(getApplicationContext(), newIntent);
}
}
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import com.batch.android.Batch;
import com.batch.android.BatchPushPayload;
import com.google.firebase.analytics.FirebaseAnalytics;
class FirebaseBatchIntegration {
@SuppressLint("StaticFieldLeak")
private static Context context;
static void handleIntent(Context appContext, Intent intent) {
context = appContext;
Bundle payload = intent.getBundleExtra(Batch.Push.PAYLOAD_KEY);
if (payload != null) {
String source = payload.getString("utm_source");
if (source != null) {
track(source);
} else {
try {
BatchPushPayload batchPayload = BatchPushPayload.payloadFromReceiverExtras(payload);
String deeplink = batchPayload.getDeeplink();
handleDeeplink(deeplink);
} catch (BatchPushPayload.ParsingException e) {
// Safely ignore it
}
}
}
}
private static void handleDeeplink(String rawDeeplink) {
Uri deeplink = Uri.parse(rawDeeplink);
String source = deeplink.getQueryParameter("utm_source");
if (!TextUtils.isEmpty(source)) {
track(source);
} else {
String fragment = deeplink.getFragment();
if (fragment != null && fragment.length() > 11 && fragment.startsWith("utm_source=")) {
track(fragment.substring(11));
}
}
}
private static void track(String source) {
Bundle params = new Bundle();
params.putString(FirebaseAnalytics.Param.SOURCE, source);
FirebaseAnalytics.getInstance(context).logEvent("batch_notification_open", params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.