android.os.TransactionTooLargeException for ForegroundService通知

时间:2018-08-20 18:47:06

标签: android android-notifications foreground-service

我正在编写一个可跟踪用户位置并在通知中显示旅程的距离,时间和价格的android应用,所有这些都在ForegroundService中进行了跟踪。该服务跟踪位置,价格和时间,我每秒都在更新通知。 我在生产中第一次收到此TransactionTooLargeException通知更新,它仅在运行Android 8.0+的三星设备上发生。

正在发生的事情:

我从服务中调用start方法:

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

我从服务中调用update方法:

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

实际上,这些调用是彼此接连调用的,崩溃来自update方法。 (一个接一个地打电话)会不会有问题?

这是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();
}

其中getSmallViewgetBigView方法如下:

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;
}

这就是我创建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());
}

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);
}

这是我得到的崩溃日志:

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

我在网上发现了很多类似的问题,但它们通常是关于故意传递大数据的问题,我认为我没有这样做。

知道我可能在做什么错吗?

1 个答案:

答案 0 :(得分:0)

我认为问题在于每秒钟更新一次通知。

解决方案

我建议您仅在喜欢数据(距离,价格)更改时才更新通知。