2017年11月22日 星期三

Notification icon flash on status bar

Problem:


當需要在短時間內持續更新notification的內容時
在建立Notification時,記得使用同一個Notification Builder物件,並且設定 setOnlyAlertOnce(true)
以免SystemUI一直在update notification,造成status bar UI一直在跳動

Code snippet:


return new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
            mBuilder
                        .setContentTitle(title)
                        .setContentText(contentText)
                        .setContentIntent(contentIntent)
                        .setSmallIcon(R.drawable.noti_icon)
                        .setLargeIcon(appIcon)
                        .setSubText(appLabel)
                        .setColor(color)
                        .setOnlyAlertOnce(true)
                        .addAction(stopAction)
                        //.addAction(optionsAction)

                        .build();