簡體   English   中英

如何清除android Q中動作點擊的動作類型通知?

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

在 android 10 中,我使用的是回復類型通知。 我想清除有關回復操作的通知。 要清除通知,我正在使用此代碼:

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

對於變量notifyId ,我正在傳遞通知 Id。

此代碼適用於除 android 版本 10 之外的所有 android 版本。

這用於創建回復操作:

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

這用於創建通知:

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

我不知道你在哪里調用 .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)
        }
    }
}

如果您也這樣做,請確保在 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