Skip to content

Instantly share code, notes, and snippets.

@robbiematthews
Last active November 13, 2020 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robbiematthews/1d037e2c366e523b2dda5f2e053ea2a9 to your computer and use it in GitHub Desktop.
Save robbiematthews/1d037e2c366e523b2dda5f2e053ea2a9 to your computer and use it in GitHub Desktop.
broadcastreceiver
package com.appboy.custombroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.appboy.Appboy;
import com.appboy.Constants;
import com.appboy.models.outgoing.AppboyProperties;
import com.appboy.push.AppboyNotificationUtils;
import com.appboy.support.AppboyLogger;
public class ServerEventBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = AppboyLogger.getAppboyLogTag(AppboyBroadcastReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
String packageName = context.getPackageName();
String pushReceivedAction = packageName + AppboyNotificationUtils.APPBOY_NOTIFICATION_RECEIVED_SUFFIX;
String notificationOpenedAction = packageName + AppboyNotificationUtils.APPBOY_NOTIFICATION_OPENED_SUFFIX;
String action = intent.getAction();
Log.d(TAG, String.format("Received intent with action %s", action));
if (pushReceivedAction.equals(action)) {
Log.d(TAG, "Received push notification.");
// Old SDK versions needed to be in the foreground to be logged. Starting in SDK 2.0.0, events can be logged in the background without issue. We recommend
// clients use the most recent SDK version whenever possible.
Bundle appboyExtras = intent.getExtras().getBundle(Constants.APPBOY_PUSH_EXTRAS_KEY);
if (appboyExtras != null && appboyExtras.containsKey("IS_SERVER_EVENT")) {
AppboyProperties eventProperties = new AppboyProperties();
// The campaign name is a string extra that clients can include in the push
String campaignName = appboyExtras.getString("CAMPAIGN_NAME");
eventProperties.addProperty("campaign_name", campaignName);
Appboy.getInstance(context).logCustomEvent("IAM Trigger", eventProperties);
}
} else if (AppboyNotificationUtils.isUninstallTrackingPush(intent.getExtras())) {
Log.d(TAG, "Got uninstall tracking push");
} else if (notificationOpenedAction.equals(action)) {
AppboyNotificationUtils.routeUserWithNotificationOpenedIntent(context, intent);
} else {
Log.d(TAG, String.format("Ignoring intent with unsupported action %s", action));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment