Android:需要用户交互/输入的通知
Android: Notification that requires user interaction / input
我正在编写一些 Android 应用程序,它必须在事件发生后制作一些 Notification/Alarm。我想知道 NotificationManager 是否有像 requireInteraction() 这样的功能?
现在,当某个事件发生时,应用程序只显示一个通知 1 秒,就是这样..我希望用户单击“确定”以停止此 vibration/sound
我从这里找到了一些通知代码:
感谢@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);
}
这个显示通知,它不等待用户输入
如果需要在点击通知后添加按钮或操作,您可以在构建器中使用:
添加按钮:
.addAction(new NotificationCompat.Action(*icon*, "Title", *intent for notification*));
或添加用户点击通知后发生的操作
.setContentIntent(*intent*);
如果需要更多详细信息,请查看有关 tab action and actions 的文档。
我正在编写一些 Android 应用程序,它必须在事件发生后制作一些 Notification/Alarm。我想知道 NotificationManager 是否有像 requireInteraction() 这样的功能?
现在,当某个事件发生时,应用程序只显示一个通知 1 秒,就是这样..我希望用户单击“确定”以停止此 vibration/sound
我从这里找到了一些通知代码:
感谢@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);
}
这个显示通知,它不等待用户输入
如果需要在点击通知后添加按钮或操作,您可以在构建器中使用:
添加按钮:
.addAction(new NotificationCompat.Action(*icon*, "Title", *intent for notification*));
或添加用户点击通知后发生的操作
.setContentIntent(*intent*);
如果需要更多详细信息,请查看有关 tab action and actions 的文档。