相关疑难解决方法(0)

Android通知管理器TransactionTooLargeException

我正在使用具有持续通知的前台服务,该服务非常频繁地更新.

服务启动时,我使用通知构建器初始化通知管理器并保留所有引用.

通知具有带按钮的自定义布局.每次用户按下按钮时,我都会更新远程视图并调用notificationManager.notify(service_id,notificationBuilderCompat.build()).

最后,在多次单击后,我收到TransactionTooLargeException错误,其中包含数据包的大小(以字节为单位).每个notify()都会为此数字添加更多字节.

处理这个问题的最佳方法是什么?

演示使用的代码:

class MyService: LifecycleService() {

var notificationBuilderCompat: NotificationCompat.Builder
var notificationManager: NotificationManager
var viewModel: ViewModel
val FOREGROUND_SERVICE = 10
val EXTRA_NOTIFICATION_TYPE = "EXTRA_NOTIFICATION_TYPE"
val NOTIFICATION_CHANNEL_ID = "channel_01"

override fun onCreate() {
    super.onCreate()

    // View model that holds the data
    viewModel = ViewModel()

    registerReceiver(broadcastReceiver, IntentFilter(NOTIFICATION_MESSAGE_INTENT)
    notificationManager = getSytemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // In case of android O
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "notification_name", NotificationManager.IMPORTNACE_MAX)
        notificationManager.createNotificationChannel(channel)
    }

    notificationBuilderCompat = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL_ID) …
Run Code Online (Sandbox Code Playgroud)

android android-notifications

5
推荐指数
0
解决办法
587
查看次数

android.os.TransactionTooLargeException for ForegroundService通知

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

这是正在发生的事情:

start从服务中调用方法:

public void start(MeasureService service) {
    service.startForeground(NOTIFICATION_ID, getNotification(0, 0, 0));
}
Run Code Online (Sandbox Code Playgroud)

update从服务中调用方法:

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

实际上,这些调用是彼此接连调用的,崩溃来自该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();
}
Run Code Online (Sandbox Code Playgroud)

其中getSmallViewgetBigView方法是这样的:

private RemoteViews getSmallView(int price) {
    String priceText = …
Run Code Online (Sandbox Code Playgroud)

android android-notifications foreground-service

5
推荐指数
1
解决办法
286
查看次数