HelloCW
HelloCW

Reputation: 2255

How can I set flags of NotificationChannel with Kotlin?

The Code A is from a sample project.

  1. Is Code A correct?

  2. Is Code B correct?

  3. Is there a better way to set flags of NotificationChannel ?

Code A

val notification = builder.build()
 notification.flags = notification.flags or NotificationCompat.FLAG_ONGOING_EVENT  //I think it will be overwrite.
 notification.flags = notification.flags or NotificationCompat.FLAG_NO_CLEAR

Code B

val notification = builder.build()
notification.flags = notification.flags or NotificationCompat.FLAG_ONGOING_EVENT or NotificationCompat.FLAG_NO_CLEAR

Both

val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(
                CHANNEL_ID,
                mChannelName,
                NotificationManager.IMPORTANCE_LOW 
            )
            notificationManager.createNotificationChannel(notificationChannel)
            NotificationCompat.Builder(this, notificationChannel.id)
        } else {
            NotificationCompat.Builder(this)
        }
       

Upvotes: 0

Views: 510

Answers (1)

Geno Chen
Geno Chen

Reputation: 5214

See you again :-)


  1. NotificationCompat.FLAG_ONGOING_EVENT has value 2, NotificationCompat.FLAG_NO_CLEAR has value 32.
  2. And, by seeking around some values with name NotificationCompat.FLAG_* we can realize that many of them are power of 2.
  3. They are called, by the sample code provided by you, with operator or.

Then, your question

//I think it will be overwrite.

It won't be overwritten. They are all kept by the operator or.

  1. Is Code A correct?
  2. Is Code B correct?

If the sample code is correct, then BOTH A and B are correct.

Upvotes: 1

Related Questions