Android P:如果用户回复了通知,则`NotificationManager.cancel`不起作用

时间:2018-08-07 22:53:04

标签: android notifications android-9.0-pie

NotificationManager.cancel(id)不再清除Android P上的通知, 您已经回复了带文本的通知。

用户在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 个答案:

答案 0 :(得分:0)

解决方案是:

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。

答案 1 :(得分:0)

您可以使用相同的ID通知新的通知,然后可以将其取消。

//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);