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.
Related
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
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
If flag "FLAG_NO_CLEAR" is used the notification gets not displayed on the Android Wear.
Does anyone know why or any workaround? I didn't find any information in the documentation.
I need the flag "FLAG_NO_CLEAR" on my notifications and have Action button for "dismiss", "snooze" etc.!
Notification flag FLAG_NO_CLEAR basically makes your notification "ongoing". Ongoing notifications posted from phone will NOT be displayed on the wearable device.
You have two solutions to your problem - both of them have advantages and disadvantages. Please read text below and decide which solution will solve your situation better:)
Solution 1 - use group:
You can make use of group feature of Android Wear framework. It's basically created to post many (grouped) notifications on wearable device and one summary notification on phone. But using this mechanism you can also post one ongoing notification on your phone and second notification only on wear. You will end up with one ongoing notification on your phone and one normal notification on your wearable device.
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// This notification will be shown only on phone
final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title phone")
.setContentText("Text phone")
.setOngoing(true)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(true);
// This notification will be shown only on watch
final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title wearable")
.setContentText("Text wearable")
.setOngoing(false)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(false);
notificationManager.notify(0, phoneNotificationBuilder.build());
notificationManager.notify(1, wearableNotificationBuilder.build());
This method will allow you to post an "alternative" notification for watch only, keeping your ongoing notification on phone. But (as mentioned earlier) the notification on watch cannot be ongoing - it has to be a normal notification. If you really want to have a true ongoing notification on watch you'll need to go for second solution.
Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html
Solution 2 - create wearable app:
Ongoing notifications from phone won't be shown on watch, but you can create wearable part of your Android application and post your notification directly on Android Wear. You can easily post an ongoing notification from there, but it won't be the same notification as is on phone. You will need to sync them between both devices.
Please read more about DataApi here:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
You can also take a look at my post where I've posted a code demonstrating how to use a DataApi in practise:
https://stackoverflow.com/a/24896043/3827276
Honestly something really weird changed in wear 1.5
so a solution would be to set an alarm
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent displayIntent = new Intent(this, WearNotif.class);
//displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("WeirdShit")
.setContentText("some random crap")
.extend(new Notification.WearableExtender()
.setDisplayIntent(displayPendingIntent))
.setSound(alarmSound)
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(25454, notification);
In my app, there is service running on background. I want to notify user that the service is running. But I need that user cannot delete the notification - by pressing clear button or by swipe it out, in notification bar
It means I need to show my notification above Notification area
This is possible but the way you implement it depends on the API level you develop for.
For API levels below 11, you can set Notification.FLAG_NO_CLEAR. This can be implemented like this:
// Create notification
Notification note = new Notification(R.drawable.your_icon, "Example notification", System.currentTimeMillis());
// Set notification message
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);
// THIS LINE IS THE IMPORTANT ONE
// This notification will not be cleared by swiping or by pressing "Clear all"
note.flags |= Notification.FLAG_NO_CLEAR;
For API levels above 11, or when using the Android Support Library, one can implement it like this:
Notification noti = new Notification.Builder(mContext)
.setContentTitle("Notification title")
.setContentText("Notification content")
.setSmallIcon(R.drawable.yourIcon)
.setLargeIcon(R.drawable.yourBigIcon)
.setOngoing(true) // Again, THIS is the important line
.build();
To Create Non removable notification just use setOngoing (true);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_service_launcher)
.setContentTitle("My title")
.setOngoing(true)
.setContentText("Small text with details");
Sound like Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT is what you are looking for.
To Create Non removable notification just use setOngoing (true);
To Create removable notification just use setOngoing (false);
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()