I am working over a project and i am facing a strange problem in notification.
Basically i want to play alarm in notification and for that i am adding sound i.e.uri in Notification Builder.
Here is my code:
builder.setSmallIcon(R.mipmap.app_icon).setContentTitle(title)
.setContentText(msg)
.setColor(getResources().getColor(R.color.white))
.addAction(R.drawable.alarm_off, getString(R.string.dismiss), pendingIntent)
.setOngoing(true)
.setSound(Uri.parse(path));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(WAKE_UP_ALARM_ID, notification);
Notification appears in panel but when i touched the notification panel to scroll down, music stops automatically.
I am unable to understand the scenario.
Please help me to sort out the problem.
Thanks
The flags that are used in notification builder
1.Notification.DEFAULT_SOUND:used to play sound.
2.Notification.FLAG_INSISTENT:this flag is makes your sound to ring continuously until you have done few operations on notification ie,
either you drag the bar or you click the bar.
3.Notification.FLAG_AUTO_CANCEL:this flag is used to automatically cancel the notification after you have seen it
In my application I am using GCM services. I want to beep or play a small mp3 file at onMessage function.
My GCMIntentService is extended from GCMBaseIntentService.
Can somebody please share the code or suggest me how to achieve this.
Thanks
You could use:
Notification notification = new Notification(icon, title, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND; // To play default notification sound
notification.sound = Uri.parse("PATH_TO_YOUR_SOUND_FILE"); // to play custom notification sound
NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(System.currentTimeMillis(), notification);
I use NotificationManager to play the sound. But I dont know how to stop it, when I am press any key. For example, back button. Thanks so much.~~~^^
NotificationManager manager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.sound=Uri.parse("android.resource://"
+ getPackageName() + "/" +R.raw.fin);
manager.notify(1, notification);
I believe that the NotificationManager uses the MediaPlayer to play the sound.
This plays your entire sound by default.
So you need to get a call to mediaplayer.stop.
Have you tried just canceling your notification? notification Cancel
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;
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()