Pin Notification to top of notification area

前端 未结 8 1689
有刺的猬
有刺的猬 2020-12-09 05:35

I have a Notification which is refreshed (i.e. sent) every three seconds. I\'ve set the FLAG_ONGOING_EVENT flag and the FLAG_NO_CLEAR flag so that is always shown. The probl

相关标签:
8条回答
  • 2020-12-09 05:50

    It's been some time since I've done code for Android, but if I remember correctly, whenever you call NotificationManager.notify(), it's because you want to alert the user by having an icon move or flash. However, there are ways of modifying the actual notifications you're sending.

    You can also call .cancel() on it if that helps.

    Here's the Android API documentation for help, although you've probably already seen them:

    Notification

    NotificationManager

    Hope that helped!

    0 讨论(0)
  • 2020-12-09 05:50

    Just a note for other people that run into this issue -

    Vaiden's answer fixed my issue where my ongoing notification kept jumping. It only happened to our app on ICS 4.0.3 for some reason, I think it's due to the fact that ICS handles notifications differently from the older versions. Also to note - on tablets, there are not "Ongoing" vs normal notification sections like on older versions (Gingerbread). The order is kept though, just not categorized. Not sure if it applies to phones on ICS.

    0 讨论(0)
  • 2020-12-09 05:51

    You shouldn't keep sending the notification. Just use the ongoing event flag, like you already have in your code. Other notifications may come in and obscure yours, but that's just the way Android works.

    0 讨论(0)
  • 2020-12-09 05:51

    By adding setSortKey(String sortKey) to your notification builder you can have your notifications stay in a constant lexicographical/alphabetical ordering.

    0 讨论(0)
  • 2020-12-09 05:56

    There is a solution that does exactly what you want.

    In the Notification class, there is a public field called when. According to the API:

    The timestamp for the notification. The icons and expanded views are sorted by this key.

    The default behaviour (for code and coders alike :) is to give when the timestamp of the notification:

    notification.when = System.currentTimeMillis();
    

    So - in order to maintain the correct order of notifications, all you need to do is give it a preset timestamp instead:

    notification.when = previousTimestamp;
    

    When you do this with all of your notifications, they maintain their order. I had the same problem as you and solved it this way.

    0 讨论(0)
  • 2020-12-09 05:56

    You say your notification displays a list of currently running apps. Why do you resend the notification every 3 seconds? Why not resend the notification only when the list of apps has changed. This doesn't directly solve your problem, but it drastically reduces it.

    0 讨论(0)
提交回复
热议问题