java - Android:需要用户交互/输入的通知

标签 java android android-studio alarmmanager android-notifications

我正在编写一些Android应用程序,它必须在事件发生后发出一些通知/警报。我想知道NotificationManager是否有像requireInteraction()这样的函数?

现在,当发生特定事件时,应用程序仅显示一个通知 1 秒,仅此而已..我希望用户单击“确定”以停止此振动/声音

我从这里找到了一些通知代码: NotificationCompat.Builder deprecated in Android O

谢谢@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 的文档和actions如果您需要更多详细信息。

关于java - Android:需要用户交互/输入的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449201/

相关文章:

java - 这个声明是什么意思?

java - 使用包含多个属性的标记解析 Xml [Java,DOM]

android - Ionic Cordova 应用程序未在 Android 上安装

java - 错误 : finished with non-zero exit value2

java - 用于比较器的 Junit 匹配器?

java - 当我的 REST API 返回 500 HTTP 状态时如何禁用 Tomcat 的 html 错误页面

android - 在 Firebase 和 Google Play 服务之间使用哪些依赖项?

android - TabHost 中选项卡之间的导航

java - Apollo Android Client - 无法访问类路径上生成的类

java - 使用 GSON 和 Volley 在 Android 上显示 JSON 数据