Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.