NotificationManager.cancel(id) 不再清除 Android P 上的通知,如果您已经用文本回复了通知。
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。
关于安卓 P : `NotificationManager.cancel` does not work if user has replied to notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51736472/
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
快捷目录前言一、涉及到的相关技术简介二、具体实现过程及踩坑杂谈1.安卓手机改造成linux系统实现方案2.改造后的手机Linux中软件的安装3.手机Linux中安装MySQL5.7踩坑实录4.手机Linux中安装软件的正确方法三、Linux服务器部署前后端分离项目流程1.前提准备(安装必要软件,搭建环境):2.前后端分离项目的详细部署过程:总结前言总体概述:本篇文章隶属于“手机改造服务器部署前后端分离项目”系列专栏,该专栏将分多个板块,每个板块独立成篇来详细记录:手机(安卓)改造成个人服务器(Linux)、Linux中安装软件、配置开发环境、部署JAVA+VUE+MySQL5.7前后端分离项目
我有一个使用form_tag助手的基本表单,但我想添加一个取消按钮,这样做的语法是什么?我希望取消按钮显示为按钮而不是链接,然后将用户带到不同的URL(表明他们不想提交表单)。泰,弗雷德 最佳答案 如果您想清除/重置表单字段,请按照weltschmerz的建议进行操作。但是,我通常希望“取消”按钮不会清除表单,而是让我离开表单,这意味着我不打算提交它。如果你想要后者,我会在取消时创建一个链接(或按钮)到你想去的页面,例如:=link_to'Cancel',my_page_path或者如果你想要一个按钮:=button_tag"Can
我写了下面的代码,我想在“取消”按钮下调用一段代码:vm.saveGroup=function(){SweetAlert.swal({title:"NamethisDeviceGroup",text:"Pleaseprovideanamethatyouwantyourdevicegrouptobesavedunder.Also,makesurethatyoualreadyspecifiedallneededfiltersbeforeyousavethelist.",type:"input",showCancelButton:true,closeOnConfirm:false,showL
我正在使用confirm()函数来注销。我如何检查用户是否单击了“确定”或“取消”。我的确认功能是确认('你确定')但是现在,同时单击确定和取消页面重定向。我该如何解决? 最佳答案 if(confirm('AreYouSure?')){window.location="http://www.google.com/";}else{alert("Youarenotredirected.")}DOCUMENTATION 关于javascript-如何在使用Confirm()函数时检查用户是否单
在微信小程序开发中遇到在video组件的两个问题1.安卓手机里播放视频会有明显的卡顿问题刚开始以为是网络问题,或者是视频文件问题。排查了一下发现都没问题最后加了个属性就OK了uniapp和原生小程序方法:custom-cache="false"custom-cache={{false}}video组件兼容iOS手机custom-cache加了这个属性会让2.iOS手机第一次播放视频会有几秒黑屏问题因此我加了当前手机型号的判断uni.getDeviceInfo().deviceType获取当前设备api当为iPhone时不加custom-cache属性,否则加上custom-cache=“fal
安卓渐变的背景框实现1.背景实现方法1.利用PorterDuffXfermode进行图层的混合,这是最推荐的方法,也是最有效的。2.利用canvas裁剪实现,这个方法有个缺陷,就是圆角会出现毛边,也就是锯齿。3.利用layer绘制边框1.背景万恶的设计小姐姐又来搞事情啦,你说好好的设计一个纯色的背景框框不好嘛,非要把一个框框弄成渐变的,如果不拿出放大镜估计没几个人能看出来它是渐变的。来,我让你看看是啥样框子是从左到右渐变的,设计应该是做了一个底图,然后上面盖了一个白色圆角矩形。那么我们该怎么去实现它呢?实现方法下面介绍三种实现它的方法。先贴上源码地址,大家记得给个starhttps://git
这是一个demo为我正在经历的行为。如果您编辑ID为1的现有行,将文本更改为其他内容,然后按取消按钮,该行将正确恢复到其先前的状态。为了重现我的问题,您需要:添加新行按更新按钮保存。再次选择该行并按更新按钮。按取消键该行消失了!即使有类似的问题,我也没有找到满意的答案。有人说我需要定义一个id。从我的演示中可以看出,这没有任何区别,新行有一个id,它仍然消失。当你使用远程数据源时有一些建议,但这对我来说不起作用,我需要使用本地数据。最后,有this回答。虽然它确实可以防止新行消失,但取消该行不会将数据恢复到其旧状态,它只会关闭编辑器并且数据与编辑后的位置相同。
我目前正在DockerCloud上开发Websocket应用程序。重新部署我的应用程序时,现在我总是收到此错误消息:WebSocketconnectionfailed:WebSocketopeninghandshakewascanceled查看数据框时,我看到一个包含此数据的帧:(Opcode-1)在研究这个问题时,似乎与SSL/证书等有关。一些解决方案包括添加自签名证书。令人困惑的是,相同的证书似乎适用于https但不适用于wss:此外,在使用其他浏览器进行测试时,我发现Safari运行良好。所以我确信服务器实际上是在正确的端口上使用正确的证书运行的。只有chrome似乎有问题。我知