6

I'm writing an android app that tracks the user's location and shows the distance, time and price of the trip in a notification, all this tracked in a ForegroundService. The service tracks the location, price and the time, and I'm updating the notification every second. I get this TransactionTooLargeException in production for the very first notification update, it only happens on Samsung devices running Android 8.0+.

This is what's happening:

I call the start method from the service:

public void start(MeasureService service) {
    service.startForeground(NOTIFICATION_ID, getNotification(0, 0, 0));
}

I call the update method from the service:

public void update(int price, float meters, long time) {
    mNotificationManager.notify(NOTIFICATION_ID, getNotification(price, meters, time));
}

Actually, these calls are called right after one another, the crash is coming from the update method. Can this (calling them one after the other) be a problem?

this is the getNotification:

private Notification getNotification(int price, float meters, long seconds) {
    if (mNotificationBuilder == null) {
        createNotificationBuilder();
    }
    return mNotificationBuilder
            .setCustomContentView(getSmallView(price))
            .setCustomBigContentView(getBigView(price, seconds, meters))
            .build();
}

where the getSmallView and getBigView methods are like this:

private RemoteViews getSmallView(int price) {
    String priceText = ...;
    mSmallView.setTextViewText(R.id.price, priceText);
    return mSmallView;
}

private RemoteViews getBigView(int price, long seconds, float meters) {
    String priceText = ...;
    String timeText = ...;
    String distanceText = ...;
    mBigView.setTextViewText(R.id.price, priceText);
    mBigView.setTextViewText(R.id.time, timeText);
    mBigView.setTextViewText(R.id.distance, distanceText);
    return mBigView;
}

and this is how I create the notificationBuilder:

private void createNotificationBuilder() {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        mNotificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);

    mNotificationBuilder = mNotificationBuilder
            .setSmallIcon(R.drawable.icon)
            .setOngoing(true)
            .setContentIntent(getOpenMeasurePageIntent());
}

and the getOpenMeasurePageIntent:

private PendingIntent getOpenMeasurePageIntent() {
    Intent launchMeasureIntent = new Intent(mContext, MainActivity.class);
    launchMeasureIntent.setAction(...);
    launchMeasureIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    return PendingIntent.getActivity(mContext, 0, launchMeasureIntent, 0);
}

This is the crash log I get:

Caused by: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 560988 bytes
16  at android.app.NotificationManager.notifyAsUser(NotificationManager.java:319)
17  at android.app.NotificationManager.notify(NotificationManager.java:284)
18  at android.app.NotificationManager.notify(NotificationManager.java:268)
19  at com.myapp.service.measure.MyNotification.void update(int,float,long) <- this is called from the timer

I found a lot of similar issues online, but they are usually about passing big chunks of data in the intent, which I believe I'm not doing.

Any idea what I might be doing wrong?

Analizer
  • 1,594
  • 16
  • 30
  • Possible duplicate of https://stackoverflow.com/questions/22789588/how-to-update-notification-with-remoteviews/53222877 – rds Apr 13 '19 at 21:24
  • Have you tried starting foreground service with normal notification (without remote view)? – Derek K Apr 17 '19 at 09:18
  • can you add crash logs? – Abdul Aziz Apr 17 '19 at 09:21
  • that usually happens when you have a large amount of data that is being transferred, as you are updating notification frequently, you should try to update the notification only when there is actual change, also have a look at https://medium.com/@mdmasudparvez/android-os-transactiontoolargeexception-on-nougat-solved-3b6e30597345, perhaps this can help you in understanding the issue... – Abdul Aziz Apr 22 '19 at 06:41

1 Answers1

0

i think problem is in updating Notification every seconds.

Solution

i suggest you should update notification only when data likes(distance, price) changed.

Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25