1

I'm programming some Android App that must make some Notification/Alarm after event occurs. Im wondering is there any function for NotificationManager like requireInteraction()?

Now when the certain event occurs the app just shows one notification for 1 sec, that's it..i'd like user to click OK to stop this vibration/sound

I found some code for notification from here: NotificationCompat.Builder deprecated in Android O

Thanks @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);

    }

This one shows notification and it doesnt wait for user Input

Kabir
  • 852
  • 7
  • 11
Dmytro
  • 372
  • 2
  • 12
  • Did you check information about `addAction()` while building notification? – Blind Kai Aug 11 '19 at 10:38
  • @BlindKai nice point! I checked that one, but with `addAction()` it is only possible to be redirected to certain activity. I need something to make notification vibrate till user interaction – Dmytro Aug 11 '19 at 10:48
  • Probably you could check the answer here [link](https://stackoverflow.com/a/13950364/9871059) and if you need to stop it by pressing the action button you just need to call `cancel()` method. – Blind Kai Aug 11 '19 at 10:51

1 Answers1

0

If there is a need to add the buttons or action after clicking on notification you can use in your builder:

To add an button:

.addAction(new NotificationCompat.Action(*icon*, "Title", *intent for notification*));

or to add action that happen after user click the notification

.setContentIntent(*intent*);

Check the documentation about tab action and actions if you need more details.

Blind Kai
  • 514
  • 5
  • 14
  • Thank you for your reply! This solves 50% of my probelm. Is there any way to notificate user for a long period of time? (Till his/her reaction to this notification) – Dmytro Aug 11 '19 at 10:52
  • Sorry, I'm not sure what do you mean. Basically notification stays until user click on that if `notificationBuilder.setAutoCancel(true)` or until user clear it. If you need some specific behavior you probably want to get known about FLAGs. – Blind Kai Aug 11 '19 at 10:57
  • I mean that smartphone must vibrate the whole time untill user clicks on it or clears notifications – Dmytro Aug 11 '19 at 11:04
  • I'm sorry, but I really think that there are topics and information about `os.Vibrator`, and vibration patterns. – Blind Kai Aug 11 '19 at 11:06