Android P: `NotificationManager.cancel` does not w

2020-06-17 07:02发布

NotificationManager.cancel(id) no longer clears the notification on Android P, if you have already replied to the notification with text.

Is there a way to clear a notification after the user has replied to it in Android P?

enter image description here


build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.notificationnocancel"
        minSdkVersion 23
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.notificationnocancel"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>


        <receiver
            android:name=".NotificationActionBroadcastReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="REPLY_ACTION_SELECTED_ACTION"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

MainActivity.kt

internal const val REPLY_ACTION_SELECTED_ACTION = "REPLY_ACTION_SELECTED_ACTION"

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        postNotificationButton.setOnClickListener {
            postNotification()
        }
    }

    private fun postNotification() {
        val broadcastIntent = Intent(REPLY_ACTION_SELECTED_ACTION)
        broadcastIntent.setPackage(packageName)

        val likePendingIntent = PendingIntent.getBroadcast(
                this,
                System.currentTimeMillis().toInt(),
                broadcastIntent,
                0
        )

        val replyPendingIntent = PendingIntent.getBroadcast(
                this,
                System.currentTimeMillis().toInt(),
                broadcastIntent,
                0
        )

        val likeAction = NotificationCompat.Action.Builder(
                R.mipmap.ic_launcher,
                "Like",
                likePendingIntent
        ).build()

        val replyAction = NotificationCompat.Action.Builder(
                R.mipmap.ic_launcher,
                "Reply",
                replyPendingIntent
        ).addRemoteInput(RemoteInput.Builder("text").setLabel("Reply").build()).build()

        val builder = NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Hello!")
                .setContentText("Body!")
                .setAutoCancel(true)
                .addAction(likeAction)
                .addAction(replyAction)



        val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(10, builder.build())
    }
}

NotificationActionBroadcastReceiver.kt

class NotificationActionBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val notificationManager = context.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        Log.i("Notification", String.format("There are %d notifications in the status bar from this app.", notificationManager.activeNotifications.size))
        notificationManager.cancel(10)
    }
}

2条回答
贪生不怕死
2楼-- · 2020-06-17 07:24

Solution is :

val builder = NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Hello!")
                .setContentText("Body!")
                .setAutoCancel(true)
                .addAction(likeAction)
                .addAction(replyAction)

just add

.setOngoing(true)

Or you can do the trick, if (you need sticky notification) -> when you need to cancel this sticky notification, you must show notification again with same id and with Ongoing = true, by default Ongoing is false.

查看更多
姐就是有狂的资本
3楼-- · 2020-06-17 07:28

you can notify a new Notification with the same ID , then you can cancel it.

//in your NotificationActionBroadcastReceiver
notificationManager.notify(ID , new Notification());//the same ID

yourHandler.postDelayed(new Runnable(){
                @Override
                public void run() {
                    notificationManager.cancel(ID);//the same ID
                }
} , 500);
查看更多
登录 后发表回答