Android: How to create an "Ongoing" notification? - android

Hello, How do i create the permanent notification like the first one for Battery Indicator?

In case you are using NotificationCompat.Builder , you can use :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
mBuilder.setOngoing(true); //this will make ongoing notification

Assign Notification.FLAG_ONGOING_EVENT flag to your Notification.
Sample code:
yourNotification.flags = Notification.FLAG_ONGOING_EVENT;
// Notify...
If you aren't familiar with the Notification API, read Creating Status Bar Notifications on Android developers website.

It is actually recommended to bitwise-or notification flags, rather than set the flags directly. This allows you to set multiple flags at once.
For example:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
will set both flags at once, whereas:
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
will only set the FLAG_SHOW_LIGHTS flag.

public static final int FLAG_ONGOING_EVENT
Since: API Level 1
Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.
public static final int FLAG_FOREGROUND_SERVICE
Since: API Level 5
Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service.

Related

setGroup() is undefined for the type NotificationCompat.Builder?

I like to have a stck type notification for my message app which is a webapp.
My notifications are working..But each time a new notification comes the previous notification dissapears and new one comes. When i googled i found that setGroup can be used. But whe i used it it is showing that
setGroup() is undefined for the type NotificationCompat.Builder.
My notification function is:
public void CreateNotification(String msg)
{
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
Intent notificationIntent = new Intent(AndroidMobileAppSampleActivity.this, AndroidMobileAppSampleActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(AndroidMobileAppSampleActivity.this, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) AndroidMobileAppSampleActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new NotificationCompat.Builder(AndroidMobileAppSampleActivity.this)
.setSmallIcon(R.drawable.icon)
.setTicker("New message from "+msg)
.setWhen(System.currentTimeMillis())
.setContentTitle("Mybuzzin")
.setContentText(msg)
.setContentIntent(contentIntent)
//At most three action buttons can be added
.setAutoCancel(true).build();
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.flags |=Notification.FLAG_SHOW_LIGHTS| Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notifyID, noti);
}
I had the same problem, but after several hours and many other problems i solved it. I assume you are using also Eclipse, otherwise the solution will be different.
You have to update your android in the SDK manager. Especially the Android SDK Build-Tools and afterwards you have to install API20.
Now you have to update your ADT as described here: https://stackoverflow.com/a/24437737/2061089
Don't worry after the update its working now. Before you can install the update, you have to remove all components that you want to update.
After restarting eclipse you only have to update ALL your support(v4,v7 and v13) packages in your projects. Depending on what you are using.
Finished. Now everything is up to date and eveything is working.
Hope it helps!

Can't keep notification sound playing after notification drawer is opened

I'm trying to make an alarm app, and I'm really close, but I want the alarm sound to keep playing until the user has manually disabled it via my app (puzzle to turn off alarm type idea). The problem Im having now is that the sound plays up until I pull down the notification drawer, then it stops.
Here is the relevant code:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle(alarm.getLabel());
notificationBuilder.setContentText(alarm.getMessage());
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notificationBuilder.setSound(alarmSound);
notificationBuilder.setOngoing(true);
Notification notification = notificationBuilder.build();
if(alarm.getVibrate()) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_INSISTENT;
notificationManager.notify(2, notification);
I have tried messing around with the flags, but nothing seems to keep it playing...
Thanks for any help in advance.
Found a solution. Had to use a background Service to play the ringtone rather than the notification manager.

How to use notification with sound and vibration?

How do you allow the device to vibrate or make a sound when a notification is launched.
Ive heard of the FLAGS. But how would i use them for Sound and Vibration?
Edit In Android v4 support library, there is NotificationCompat.Builder and the method setSound which will work if using that class instead. However, the info below will still work.
Notification notif ... //create your notification
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
That adds sound and vibrate.
Always remember to check the docs. They have a LOT of answers, such as this one:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
That has the info I posted above as well as info on how to use a custom sound or vibrate and also the entire process of creating a notification.
EDIT: Don't forget to include the Vibrate permission in your manifest.
Notification notification // new notification object.
notification.defaults = Notification.DEFAULT_ALL;
Shows all the default notifications. (sound, vibration, led)

make android notification stay until user clears it

My question is regarding Notification and NotificationManager,
in my app I use a Notification to alert the user, but it plays the mp3 I gave it only once, and vibrates only ones, I wish it to play a sound and vibrate until the user clear the Notification by pressing clear. I couldn't find an example that simplified it, what's the best way to achieve that?
Thank All that reply,
Amitos80
there are flags you can set for achieving desired result,
Notification notification = new Notification(mIcon, mTickerText, mWhen);
notification.setLatestEventInfo(mContext, mContentTitle, mContentText, mContentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(ID, notification);
This will work definitely.
If you find it useful dont forget to mark it as an answer.

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