NotificationManager.cancel(id)不再清除Android P上的通知(如果您已使用文本回复了)。
用户在Android P中回复通知后,是否可以清除该通知?
Android P :  `NotificationManager.cancel`  does not work if user has replied to notification-LMLPHP

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)
    }
}

最佳答案

解决方法是:

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

只需添加
.setOngoing(true)

或者,您可以解决问题,如果(需要粘性通知)->当您需要取消此粘性通知时,必须再次显示具有相同ID且Ongoing = true的通知,默认情况下Ongoing为false。

10-08 15:18