ともちゃんのアプリ開発日記

組込みC言語プログラマだったともちゃんが、四苦八苦しながら、AndroidのJAVA/Kotlin、iOSのSwiftUIを習得して行きます。ともちゃんの備忘録も兼ねています。

Android 8.0の通知ドット(通知バッジ)を削除する。

Android 8.0の通知ドット(通知バッジ)が邪魔な時があるので、削除する設定です。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(MAIN_CHANNEL, getString(R.string.main_channel), NotificationManager.IMPORTANCE_LOW)
/** 指定できる値は以下7種類。
NotificationManager.IMPORTANCE_UNSPECIFIED (-1000);
NotificationManager.IMPORTANCE_NONE (0);
NotificationManager.IMPORTANCE_MIN (1);
NotificationManager.IMPORTANCE_LOW (2);
NotificationManager.IMPORTANCE_DEFAULT (3);
NotificationManager.IMPORTANCE_HIGH (4);
NotificationManager.IMPORTANCE_MAX (5);
**/
channel.setShowBadge(false)
notificationManager.createNotificationChannel(channel)

}

val intent = Intent(applicationContext, MainActivity::class.java)
val intents = arrayOf(intent)
val pendingIntent = PendingIntent.getActivities(applicationContext, 0, intents, PendingIntent.FLAG_UPDATE_CURRENT)
val builder = NotificationCompat.Builder(applicationContext, MAIN_CHANNEL)
builder.setContentIntent(pendingIntent)
builder.setContentTitle(resources.getString(R.string.app_name))
builder.setContentText(resources.getString(R.string.please_tap_here))
builder.setWhen(System.currentTimeMillis())
builder.setAutoCancel(false)
builder.setSmallIcon(R.drawable.notification_main_small)
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.notification_main_large))
builder.setBadgeIconType(NotificationCompat.BADGE_ICON_NONE)

val notification = builder.build()

startForeground(NOTIFICATION_ID, notification)

 上記の

channel.setShowBadge(false)

 と

builder.setBadgeIconType(NotificationCompat.BADGE_ICON_NONE

 で、通知ドット(通知バッジ)を消しています。