Android P:如果用户回复了通知,则"NotificationManager.cancel"无法正常工作 [英] Android P: `NotificationManager.cancel` does not work if user has replied to notification

查看:92
本文介绍了Android P:如果用户回复了通知,则"NotificationManager.cancel"无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NotificationManager.cancel(id)不再清除Android P上的通知, 如果 您已使用文本回复了该通知.

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

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

推荐答案

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

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

这篇关于Android P:如果用户回复了通知,则"NotificationManager.cancel"无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆