Relic Solution: Tracking Mobile Background Data

The New Relic Mobile SDK is intended to live within the foreground of a mobile application lifecycle. This is to ensure the agent has the smallest possible footprint, while tracking and reporting information about your app when the user is directly interacting with it.

But let’s face it - data can happen when your mobile app isn’t in the foreground. There might be information originating from background and long running services, and I’m here to help report that information.

I’ll provide a few code snippet examples specific to Android, but the basic concepts covered can be applied to other platforms. If you have examples of this functionality in other platforms, please share by commenting below!

Persistent Data Storage

In my example, I’d like to track if a user received a push notification, and report any possible exceptions thrown in the service.

We’ll place relevant event data using the SharedPreferences API, and report it once the app has launched.

public class MessageReceiver {
    public boolean onMessageReceived(Context context, Bundle bundle, Message message) {
        SharedPreferences sharedPreferences = getSharedPreferences("PushNotificationService", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("MessageReceived", message);
        try {
            //Push notification logic
        } catch (Exception e) {
            editor.putString("ExceptionThrown", e.getMessage());
        return false;

Now let’s retrieve any data from that service, and report it as a custom event. We’ll process this as soon as the device has returned to the foreground, and the agent is started.

    protected void onCreate(Bundle savedInstanceState) {

        SharedPreferences sharedPreferences = getSharedPreferences("PushNotificationService", Context.MODE_PRIVATE);
        Map<String, Object> attributes = (Map<String, Object>) sharedPreferences.getAll();
        if (attributes.size() > 0) {
            boolean addedToPool = NewRelic.recordCustomEvent("PushNotificationService", attributes);
            Log.d(TAG, "NR Event added to pool: " + addedToPool);

Insights API

Now I’d like to track when users of my pedometer service have reached 10,000 steps within a day. I don’t want to rely on the user to launch the app to report this data, so let’s leverage the Insights REST API.

try {
    String url = "<Account_Id>/events";
    JSONObject jsonBody = new JSONObject();
    jsonBody.put("eventType", "PedometerTracking");
    jsonBody.put("stepCount", stepCount);
    jsonBody.put("sessionId", NewRelic.currentSessionId());

    OkHttpClient client = new OkHttpClient();

    RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonBody.toString());
    Request request = new Request.Builder()
        .addHeader("X-Insert-Key", "<Insights_insert_key>")

    Response response = client.newCall(request).execute();

} catch (JSONException | IOException e) {

In this example I’m using OkHttp3 to send a POST request to the Insights API with event data packaged within a JSON object.

If you have other use cases or solutions for other platforms - please share below!


I understand it is an example, but doing SharedPreferences in onCreate() is the thing I would try to avoid. Can NewRelic.recordCustomEvent done in non UI thread?

Absolutely, @brian.chu - anywhere after onCreate() works too.

Would the NewRelic object (and the session ID) be available when the App is launched in the background and therefore NewRelic object hasn’t initialized?

Hi @asad.ather - thanks for getting in touch regarding this functionality! The ability to track data before the New Relic Agent is initialized, or while the App is in the background, isn’t available at this time. That being said, we’re always looking to our customers for suggestions on how to improve our product and I’d encourage you to post this in the Feature Idea section of our community!

1 Like