我正在使用具有持续通知的前台服务,该服务会非常频繁地更新。
当服务启动时,我使用通知构建器初始化通知管理器并保留所有引用。
通知具有带按钮的自定义布局。每次用户按下按钮时,我都会更新远程视图并调用 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)
}
ovveride fun onStartCommand(intent: Intent?, flags: Int, startId:Int): Int {
// Received start notification command
if (intent.action == START_FOREGROUND_ACTION) {
setupNotification()
viewModel.observerData.observe(this, Observer { data ->
remoteViews.setTextViewText(R.id.notification_text, data)
// This handles the update with the new data, and also where the exception happens
notificationManager.notify(FOREGROUND_SERVICE, notificationBuilderCompat.build())
})
} else {
stopForground(true)
unregisterReceiver(broadcastReceiver)
stopSelf()
}
}
fun setupNotification() {
remoteViews = RemoteViews(packageName, R.layout.item_notification)
// Setting all the remote views here
remoteViews.setImageViewResource(R.id.notification_app_icon, R.mipmap.ic_luancher)
// Setting the click event
val clickIntent = Intent(NOTIFICATION_MESSAGE_INTENT)
clickIntent.putExtra(EXTRA_NOTIFICATION_TYPE, 1)
val clickPendingIntent = PendingIntent.getBroadcast(this, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)
remoteVies.setOnClickPendingIntent(R.id.notification_button, clickPendingIntent)
notificationBuilderCompat.setContent(remoteViews)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setTicker("Notificaion on")
.setCustomBigContentView(remoteViews)
.setSmallIcon(R.drawable.icon)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setDefaults(0)
startForeground(FORGROUND_SERVICE, notificationBuilderCompat.build()
}
// This is the broadcast receiver class to handle the click on the notification button
val broadcastReceiver = object: BroadcastReceiver() {
ovveride fun onReceive(context: Context?, intent: Intent?) {
// Getting the notification type
val notificationType = intent!!.getIntExtra(EXTRA_NOTIFICATION_TYPE, 0)
if (notificationType == 1) {
// Updating the data here which invokes the observer
viewModel.updateData()
}
}
}
}