user8367195 user8367195 - 3 years ago 61
Android Question

Sending immediate notification with options

So I have a timer using AlarmManager and once it hits 0 I want it to send a notification with various attributes depending on what the user chooses (vibrate or not, play sound or not, show text or not, show... rectangle thingy in the drop-down tray whatever it's called, etc).

I am not sure what the expected class to use here is since it's immediate, i.e. it's after the alarm manager has already hit 0 and I am now in the onReceive() method of my broadcast receiver, so I don't think I need to use a PendingIntent at this point.

Do I use

NotificationCompat.Builder
? Will it do everything I need? And if so do I use the support v4 or v7 version?

Trying this:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.mipmap.ic_launcher))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText("This text should be in notification drawer!");

NotificationManager notificationManager = (NotificationManager) c.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
Toast.makeText(c, "Toast text!", Toast.LENGTH_LONG).show();

Answer Source

As per discussion in the comments -

Yes NotificationCompat.Builder will do all of the above things you need. .build is enough to send notification instantly. And if you want to set a specific time you can use .setWhen as well. You can find all methods related to action that you need to do here - Notification Developer Guide

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download