I am writing NotificationListenerService ,
where I want to get the details of notification sent to status bar.
But only thing we get is Ticket text , which is null in some cases.
Correct, ticker text is not a required field when building a notification. In fact, the only required notification contents are:
A small icon - returned by icon
A title - returned by extras.getCharSequence(Notification.EXTRA_TITLE)
Detail text - returned by extras.getCharSequence(Notification.EXTRA_TEXT)
Unfortunately, the extras bundle is only available on Android 4.4 (KitKat) devices - previous versions do not have any easy access to this data - you only have access to the RemoteViews which you'd need to inflate and parse manually (definitely not recommended).
Ticker text is optional, and may be null in some cases.
You haven't posted any code, but are you using the onNotificationPosted(StatusBarNotification) method from NotificationListenerService? That was implemented in API 18 (Android 4.3), so it should work for you. The documentation states that the full Notification object should be returned, which should give you more than just the ticker text.
Related
I get some data Jsonarray from server every 60 seconds and I use jsonarray to build notification in for-loop;
However I don't know how to know if notification is active than pass next one to build the notification
is there any way to do it ?
NotificationManager can keep track of it for you.
getActiveNotifications:
added in API level 23
StatusBarNotification[] getActiveNotifications ()
Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancel(String, int)ed by the app. Each notification is embedded in a StatusBarNotification object, including the original tag and id supplied to notify() (via getTag() and getId()) as well as a copy of the original Notification object (via getNotification()).
https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()
I have a few notifications running on my app built with NotificationCompat
Depending on the type, they show background bitmaps, or list of text, also most of them have 1 or 2 actions using the code:
builder.addAction(R.drawable.ic_notification, text, pendingIntent)
some of those actions makes sense for a watch (for example: "like" or "reply") and some doesn't (for example: "view album").
I thought I could use the setLocalOnly(boolean) method for it, but I found out that it is applied to the whole notification, not just to individual actions.
I've also been checking on NotificationCompat.Action and NotificationCompat.Action.WearableExtender but couldn't find anything that would be relevant.
So the question:
is there a way to make the notification show on the watch, but only with some of the actions but not others?
Please see docs in the paragraph "Specify wearable-only actions".
I've implemented a notification listener to look out for a Gmail notification.
I want to collect the expanded text (bigtext) from the notification as shown in the notification below:
See "----Forwarded message---", etc. which only appears when the user expands the notification to show the action buttons.
This string value does not appear in the notification's "EXTRAS" data...
http://developer.android.com/reference/android/app/Notification.html
After viewing the above link, I further investigate the bundle (EXTRAS) data. When you debug it and look at the variable, you can find that all information regards the notification are stored in the bundle and you can get the detail by
notifyBundle.extras.getCharSequence("android.textLines") for multi line notification and
notifyBundle.extras.getString("android.text") for single line notification.
For more information, look at the variable in eclipse debugging mode
Image of variables in bundle for single line notification
Image of variables in bundle for multi line notification
Note: The extra bundle only available in API19 (Kitkat). I've never tried in device which lower than API19.
I ran into a similar problem with Gmail running on Android 7.
Eventually, I've realized (with some help from another thread) that he solution was different from the existing answers here - what you're looking for can be accessed in a different way:
Bundle extras = statusBarNotification.getNotification().extras;
extras.get(Notification.EXTRA_BIG_TEXT) == null ? null : extras.get(Notification.EXTRA_BIG_TEXT).toString();
This way you will always get either a String or null, even if the value you're looking for isn't originally a string. This is done because calling getString(Notification.EXTRA_BIG_TEXT) directly would return null instead, at least in some cases.
If there are any other values you're not sure where they might be stored, you can try iterating through the entire extras bundle, as explained here.
Finally, I am able to solve this issue by using this code.
ArrayList<StatusBarNotification> groupedNotifications = new ArrayList<>();
for(StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if(statusBarNotification.getNotification().getGroup().equals(NOTIFICATION_GROUP)) {
groupedNotifications.add(statusBarNotification);
}
}
CharSequence stackNotificationMultiLineText[] = groupedNotifications.get(ZEROTH_INDEX).getNotification().extras.getCharSequenceArray(NotificationCompat.EXTRA_TEXT_LINES);
If you try to use getCharSequence("android.textLines"), it returns you null because NotificationCompat.EXTRA_TEXT_LINES returns you the array of CharSequence and not a single CharSequence object.
I found a shorter way of accessing the expanded notification. This works in API 24.
If you are getting null while accessing getCharSequence("android.textLines"), this is because it actually returns an array of CharSequence as rightly pointed out by Puneet above.
So rather access them like this:
if (extras.getCharSequenceArray("android.textLines") != null)
text = Arrays.toString(extras.getCharSequenceArray("android.textLines"));
my app searches for new articles and sends a notification like "5 new articles". However when i send another one, i want to have it update the text to lets say there were 3 new so something like "8 new articles" BUT "3 new articles" if the user has dismissed that previous notification. I hope you get it.
Is there a way to know that notification was dismissed so i can reset the count?
Thanks !
This is a rather belated answer but I was looking to find out how to do this myself so perhaps it'll be useful to others. In API level 18 the following service was introduced which should make this straightfoward as you can now get a callback whenever a notification is added or removed:
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
In particular for the original question see the onNotificationRemoved method
The other option mentioned by jpop above is to use the builder's setDeleteIntent method when creating the notification, which will give a callback when the notification is deleted. This would then require you to maintain the state of the existing raised notifications somewhere else as it only tells you when something is added, not removed.
If your app has a database you can do this: Create a table that has some fields like id as int, article name as string, and isdismissed as boolean. So, each time you want to send a notification, you should count the records that the isdismissed field equals false.
In the other hand, each time user select a notification, the related isdismissed field must be equal true.
In addition, this sample from developer.android.com maybe can help you:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
the listener don't work since android Nougat, a new access rule have been added
on android device : parameters -> applications -> gear menu -> spécial access -> notifications access
use : catch on swipe to dismiss event
I wanted to show the numbers in a application shortcut icon(without developing the widget), i would like to know how that number can be updated programmatically whenever i receive a service notification saying "n" new items added to the user. I am able to show the notification message whenever i get new items, I want to add this items count to the application shortcut icon(some thing similar to showing the new message count in the messages shortcut).
Thanks
Praneeth
Use Notification.Builder class and set the number of the notifications using setNumber(int number) member function.
I know you don't want to use Widgets but I'm pretty sure the existing message count is based on a Widget.
Here's an example of how to get one up and running - Widget Tutorial