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
Related
My Code:
mNotifyBuilder.setSmallIcon(R.drawable.ic_status_bar)
.setContentTitle("")
.setOngoing(true)
.setAutoCancel(false)
.setTicker("")
.setColor(ContextCompat.getColor(mContext, R.color.folderlist_bg_music))
.setChannelId(CHANNEL_ID)
.setContent(remoteViews).build();
Working fine on other phones, but not working on Vivo V7.
On first swipe the notification is removed and it reappears. But on second swipe, it dismisses completely.
Option 1:
You need to do the following way:
builder.setOngoing(true);
Option 2:
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Option 3:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_service_launcher)
.setContentTitle("My title")
.setOngoing(true)
.setContentText("Small text with details");
start a dummy foreground Service ...this will persist the notification while it's running.
A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.
Try adding this:
notification.flags |= Notification.FLAG_NO_CLEAR;
You can try the following:
builder.setOngoing(true); // Can't cancel your notification (except notificationManager.cancel(); )
Refer to this Ongoing notification
This is phone specific issue. Same thing works for other phones but not for Vivo phones.
OEMs shouldn't deviate from basic implementation that has been provided by stock Android.
I currently have a notification that is set as a headsup notification by having it play a sound and have max priority. I build it like this:
builder.setPriority(Notification.PRIORITY_MAX).setCategory(Notification.CATEGORY_ALARM)
.setContentTitle(getString(R.string.alarm_title))
.setContentText(getString(R.string.alarm_text))
.setSmallIcon(R.drawable.ic_alarm_black_24dp)
.setSound(alarm_sound, attributes);
Notification notif = builder.build();
notif.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_INSISTENT;
The problem is that if a text comes in while this notification is present (the notification is important, as it is attached to a wake up alarm) the text message sound completely stops the rest of the sound being played. Additionally my notification disappears before the text message notification which appears on top, so I never get to actually see my notification (which has buttons like dismiss alarm attached).
I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop
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.
I am developing a GPS-based app and have just started adding in my UX features such as notifications and progress bars but I'm stuck on using an ongoing notification.
As it is a GPS app, when user tracking is started, I set up an ongoing notification to show that they are being tracked but how do I stop this notification when they tap "stop tracking" in my app? Do I have to tell the NotifyManager something? I'm basically trying to get the functionality that music players have, as in the "playing" notification appears when the user presses play, but when they pause, that ongoing "playing" notification is destroyed.
Also, I've never worked with GPS before but should I be going about this in a Service so that the user won't stop being tracked if my app is taken out of memory by the OS? Or would that not happen?
I haven't done much with notifications but you might try this:
NotificationManager.cancel(id); // Where 'id' is the id of your notification
Replacing NotificationManager with the name of your instance of it, of course.
docs: http://developer.android.com/reference/android/app/NotificationManager.html#cancel%28int%29
Or this:
Notification.Builder.setAutoCancel(true);
http://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel%28boolean%29
Start
int id = 01;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.service_header))
.setContentText("Connect to: http://" + httpd.getip() + ":8080")
.setContentIntent(pendingIntent)
.setOngoing(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(01, mBuilder.build());
Cancel
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);