简体   繁体   English

如何清除android Q中动作点击的动作类型通知?

[英]How to clear action type notification on action click in android Q?

In android 10, I am using reply type notification.在 android 10 中,我使用的是回复类型通知。 I want to clear that notification on reply action.我想清除有关回复操作的通知。 To clear the notification I am using this code:要清除通知,我正在使用此代码:

 val notificationManager =
        context.getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.cancel(notifyId)

For variable notifyId , I am passing the notification Id.对于变量notifyId ,我正在传递通知 Id。

this code works for all the android versions except android version 10.此代码适用于除 android 版本 10 之外的所有 android 版本。

This is used to create the reply action:这用于创建回复操作:

val replyAction = NotificationCompat.Action.Builder(
            R.drawable.ic_send,
            btnlable,
            getReplyPendingIntent(
                notificationId,
                title,
                msg,
                senderName,
                action,
                apiName,
                type,
                intentType
            )
        )
            .addRemoteInput(remoteInput)
            .setAllowGeneratedReplies(true)
            .build()

This is used to create the notification:这用于创建通知:

val notification = NotificationCompat.Builder(mContext, channelID)
            .setSmallIcon(R.drawable.noti_icon)
            .setContentTitle(title)   //Set the title of Notification
            .setContentText(msg)    //Set the text for notification
            .addAction(replyAction)
            .setSound(null)
            .setWhen(getTimeMilliSec(timeStamp))
            .setLargeIcon(BitmapFactory.decodeResource(mContext.resources, R.drawable.logo))
            .setContentIntent(resultPendingIntent)
            .build();
        notification.flags = Notification.FLAG_AUTO_CANCEL

I don't know where you call .cancel method but I'm doing it on onHandleIntent method of class that inherit from IntentService class and it works:我不知道你在哪里调用 .cancel 方法,但我在从 IntentService 类继承的类的 onHandleIntent 方法上执行它并且它工作:

class ApplicationIntentService() : IntentService("AppIntentService") {

    companion object{
        const val BIG_TEXT_STYLE_ACTION1_NAME = "big_text_style.action1"
    }

    override fun onHandleIntent(intent: Intent?) {
        var notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (intent?.action == BIG_TEXT_STYLE_ACTION1_NAME) {
            notificationManager.cancel(0)
        }
    }
}

If you did this too, make sure you register your IntentService implementation in Manifest.xml:如果您也这样做,请确保在 Manifest.xml 中注册您的 IntentService 实现:

    <service
        android:name=".Services.ApplicationIntentService"
        android:exported="false">
    </service>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM