Saya ingin menampilkan notifikasi dari layanan saya untuk android O tetapi saya mendapat error ini dan saya ingin menggunakan IMPORTANCE_UNSPECIFIED dengan saluran notifikasi.
Malah muncul error ini.
java.lang.IllegalArgumentException: Invalid importance level
Sebagai gambaran, kira-kira potongan kode saya seperti ini.
if (android.os.Build.VERSION.SDK_INT >= 26) {
try {
// Create the channel for the notification
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_UNSPECIFIED);
// Set the Notification Channel for the Notification Manager.
mNotificationManager.createNotificationChannel(mChannel);
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.instant_app_icon)
.setContentTitle(getString(R.string.app_name))
// .setContentText("Instant Service")
.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
Notification notification = builder.build();
startForeground(1, notification);
}
catch (Exception ex){
ex.printStackTrace();
}
}
Pencerahan
Sebagaimana dokumentasi resmi, Anda tidak bisa menggunakan IMPORTANCE_UNSPECIFIED
, berikut cuplikannya.
Sebagai gantinya Anda dapat menggunakan salah satu level importance di bawah ini.
- IMPORTANCE_DEFAULT
- IMPORTANCE_HIGH
- IMPORTANCE_LOW
- IMPORTANCE_MAX
- IMPORTANCE_MIN