java - Android:需要用户交互/输入的通知
问题描述
我正在编写一些必须在事件发生后发出通知/警报的 Android 应用程序。我想知道 NotificationManager 有没有像 requireInteraction() 这样的功能?
现在,当某个事件发生时,应用程序只显示一个通知 1 秒,就是这样..我希望用户单击“确定”以停止此振动/声音
我从这里找到了一些通知代码: Android O 中不推荐使用 NotificationCompat.Builder
谢谢@Mehul
public void showNotification (String from, String notification,
Intent intent) {
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
requestID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
Notification mNotification = builder
.setContentTitle(from)
.setContentText(notification)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.build();
notificationManager.notify(/*notification id*/requestID, mNotification);
}
这个显示通知,它不等待用户输入
解决方案
推荐阅读
- python-2.7 - 为什么写入配置单元表后有二进制类型
- ruby-on-rails - 在 Rails 控制台中重定向脚手架 ID
- excel - 设置单元格 (n) 颜色索引等于单元格 (n - 1) 颜色索引
- node.js - 为什么 Express.js 不设置我的标题?
- javascript - 未在 href 标记上评估 pug 变量
- swift - 队列实现 - 未解析的标识符 swift map() 和 toString
- django - Django 2.1 模型管理器在行动中缺失
- sql - SQL从所有组中获取最小日期
- python-3.x - Python NeuroEvolutionProject:ValueError:list.remove(x):x不在列表中(当x实际上在列表中时)
- kubernetes - traefik 如何将 Host:{containerName}.{domain} 路由到 docker 等 kubernetes 提供程序中的容器