How to show notification in status bar? - android

So i have created this notification in my activity
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("New mail from " + sender)
.setContentText(subject)
.setSmallIcon(R.drawable.notification)
.build();
How can i now show it in status/notification bar along with the sound?

There is some good documentation at android developer site
and have a look at the NotificationManager doc's
Here you go, some code...:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, n);
Note that it is also a goo idea to add your intend to the notification...
mBuilder.setContentIntent(resultPendingIntent);
To add sound you can use the Notification.Builder.setSound() method.

How can i now show it in status/notification bar along with the sound?
Use the Notification.Builder.setSound() method.
You can get the default ringtone like this:
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
and then set it as notification sound:
Notification.Builder(getApplicationContext()).setSound(uri);
and then after you've built your notification you launch it with:
myNotificationManager.notify(myNotificationId, n);

Related

NotificationCompat at a time

I want to show notification at defined time. I use "setWhen()". But any argument of setWhen() is ignoring and notification always showing instantly. What I did wrong? My code:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setWhen(???)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Test")
.setContentText("Test");
Intent resultIntent = new Intent(this, MainActivity.class);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
That's not the purpose of setWhen. It's only a UI thing, if setShowWhen is enabled this timestamp is shown at the notification.
For your purpose, I would suggest the android AlarmManager. An example can be found here
setWhen() is used to show the time on the notification itself, not when the notification is shown.
The time on the top right of the notification is what setWhen() controls:
For showing the notification at a particular time, you can use the AlarmManager class. This has code to do the same.

Android how to compose notification on notification bar

I am new to android. I want to now how to create a notification in the notification bar and compose it like 'usb connected' or 'usb debugging connected' notify. Can anyone help me with some codes?
I have used NotificationCompat.Builder for the purpose
First we have to style the notification
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // icon to be displayed
.setContentTitle("Notification") // title for the notification
.setContentText(msg); // msg is some text you want to display
For Led light blinkering when notification is received
mBuilder.setLights(Color.BLUE,1000,1200);
To give notification sound you can make use of default notification sound set by the user
Uri sound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(sound);
To cancel the notification when the user has selected it
mBuilder.setAutoCancel(true);
To start an activity in app when the user clicks the notification
Intent resultIntent = new Intent(this,ExampleActivity.class);
PendingIntent resultPendingIntent =PendingIntent.getActivity(this,0, resultIntent,0);
mBuilder.setContentIntent(resultPendingIntent);
Finally to show the notification in the bar make use of notification manager
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

regular notification wont show X close button in android

I created a notification using android.support.v4.app.NotificationCompat.Builder. The notification shows up fine in the notification area.But it doesn't show the default close button on android 4.2.
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setOngoing(boolean)
As per the docuentation,its a type of regular notification which should show the X close button but it doesn't.
Using the following code:
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(iconId);
mBuilder.setContentTitle(titleText);
mBuilder.setContentText(moreinfoText);
mBuilder.setOngoing(false); //to make it regular
PendingIntent pintent =
PendingIntent.getActivity(context,noteId,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pintent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(noteId, mBuilder.build());
Searched on Google but couldn't find anything similar.
Please suggest.
Thanks for your time!

clearing ongoing notification in android

In my application iam showing a ongoing notification while doing a video upload. I want to clear the notification after upload. I searched a lot and cant get a clear answer.Please help me if anybody knows...
From Managing your Notifications in android documentation:
You can also clear it manually with cancel(int), passing it the
notification ID, or clear all your notifications with cancelAll().
So you should perform following to hide the notification:
mNotificationManager.cancel(notification_id);
where mNotificationManager is NotificationManager object and notification_id is int identifier of notification passed to NotificationManager object:
mNotificationManager.notify(notification_id, notification);
Just to show how this is done with Notification.Builder you can set the FLAG_AUTO_CANCEL like this:
Notification.Builder mBuilder = new Notification.Builder(getContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle("My notification")
.setContentText("Hello Notify!")
.setOngoing(true)
.setAutoCancel(true);
You can clear your notification by clicking on it..!!
Notification notification = new Notification("Your needed data..");
notification.flags |= Notification.FLAG_AUTO_CANCEL;

How to check which notifications are active in status bar in Android Dev?

I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).
How can I do this? (Thanks in advance).
Sample code is greatly appreciated.
I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see
the notification in the status bar?).
How can I do this?
You can't, sorry. Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.
EDIT:
NotificationManager.getActiveNotifications() was added in API 23 if you don't want to use the NotificationListenerService
Just to put all together. This is how it works
To build a notification,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSmallIcon(R.drawable.myicon).build();
To make a notification sound call setSound() of Notification,
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setSmallIcon(R.drawable.myicon).build();
To cancel the notification after user selected and launched the receiver Intent, call setAutoCancel(),
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setAutoCancel(true)
.setSmallIcon(R.drawable.myicon).build();
To make sound/vibrate only once for a particular notification use Notification.FLAG_ONLY_ALERT_ONCE. With this flag, your notification will make sound only once till it gets cancelled and you can call notify() as many times as you want with the notification id. Note that if you call cancel() or if user cancelled the notification or auto cancelled, notify() call will make the notification sound again.
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound
Finally to put the notification on notification panel,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, n);
Note that notification_id here is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).
To cancel a particular notification,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
You can cancel() a notification even if it doesn't exist or you can call notify() as many times as you want with the same id. Note that calling notify with different id will create new notifications.
So, regardless of whether the notification exist or not, if you call notify() again with the correct notification_id with the Notification.FLAG_ONLY_ALERT_ONCE flag set, you can keep your notification alive without disturbing the user with repeated sounds.
You need to set an id for each notification you make.
so you make a notification ..
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);
to clear it..
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0);
pendingIntent.cancel();
and to check if active..( existAlarm returns null if no pending intent available)
public PendingIntent existAlarm(int id) {
Intent intent = new Intent(this, alarmreceiver.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
return test;
}
So everything comes down to initialize an ID for each notification and how you make it unique.
A new method is introduced to the NotificationManager class in API 23:
public StatusBarNotification[] getActiveNotifications()
There exists a flag for that.
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
FLAG_ONLY_ALERT_ONCE:
...should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
Although, the notification will blink when it is sent again, but there won't be any sound or vibration.
It's possible now to check notifications outstanding in android 4.3 upwards
See here:
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
It seems that from Android M (API 23) it is possible to get your process like that, without using NotificationListenerService nor requiring additional permissions:
notificationManager.getActiveNotifications()
As of Android Marshmallow (API 23), you can recover a list of active notifications posted by your app. This NotificationManager method is getActiveNotifications(). More info here: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()

Categories

Resources