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()
Related
I have app, that send notifications to user time to time. Some of them are extremly important, so I've set them as ongoing, so user can't delete them by swiping, they are deleted when user deletes them in app (or when the app is force stopped).
When I create this notification, I add object with some data (name, text, id and datetime of creation of the notification) to static List, it works. But when there is some important ongoing notification and user closes the application (goes to the list of active app via the left hw button and swipe-closes it), the static List of notifications is cleared.
So how can I:
Save data (list of objects) until app is force-killed (so until the notification disappears)
OR
Get collection of active notifications created by application (need name and id,
and if possible even text)
Thanks
EDIT: If you know something about developing mobile apps in Java, you can help me by saying how would you do that in java, Xamarin is pretty similar to it, so if you do that, I think, I'd be able to find a way.
If you want to create a not-clear-able notification, for Api level below 11, set Notification.FLAG_NO_CLEAR.
For Api level above 11, set setOngoing to true, this will make notifications not be dismissed by user.
For example:
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("Sample Notification")
.SetContentText("Hello World! This is my first notification!")
.SetSmallIcon(Resource.Drawable.ic_notification)
.SetOngoing(true);
After setting it to true, make sure your app/service will handle the canceling of this notification.
Update:
Get collection of active notifications created by application (need name and id, and if possible even text)
NotificationManager notifyManager = (NotificationManager)GetSystemService(Context.NotificationService);
StatusBarNotification[] notificationList = notifyManager.GetActiveNotifications();
foreach (Android.Service.Notification.StatusBarNotification sbnin notificationList)
{
Debug.WriteLine("sbn.Id " + sbn.Id);
Debug.WriteLine("sbn.PostTime " + sbn.PostTime);
Debug.WriteLine("System.Diagnostics.Debug.WriteLine("notification = " + sbn.ToString());= " + sbn.ToString());
}
Save data (list of objects) until app is force-killed (so until the notification disappears)
As Samuh said, when you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens :
the class is unloaded
the JVM shuts down
the process dies
So your static List will exists until your app is force-killed. As you said,
user closes the application (goes to the list of active app via the left hw button and swipe-closes it), the static List of notifications is cleared.
So you have implement this feature, if this is not the effect your want, please elaborate a bit more.
If you want to save the list even when your app is closed, you can try to save your list in onstop and so on to a shared-preference or file.
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.
I'm developing Android IMAP client based on JavaMail-Android library. I use fetch method of IMAPFolder by providing MessageCountListener. When there is a new message on a folder, the MessageCountListener.messagesAdded is called with the IMAPMesssage object given. Unfortunatelly the provided snapshot does not have flags nor message ID set. When I try to call IMAPFolder.fetch with FetchProfile.Item.FLAGS set, the result is the same.
Why are the flags not set?
Regards
The listener only announces the presence of new messages. When you call Message.getFlags, it should fetch the flags for the new message. The "fetch" method can be used to pre-fetch the flags for a bunch of messages all at once, but isn't necessary.
How are you determining that the flags are not set?
If you turn on session debugging, what does the debug output show?
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