Updating the tickerText of a notification - android

I’m having a weird problem with notifications.
I have created an on going notification, and I want to sometimes update the notification and display a new ticker text.
When I create the notification, the ticker text is correctly displayed.
When I update the notification the first time (with the code below), the new ticker text is correctly displayed.
When I update the notification the second time, with exactly the same code and the same arguments as the first time (in particular the same new ticker text), the ticker text is not displayed.
Where is the problem?
Code:
mNotification.tickerText = newTickerText;
mNotification.when = System.currentTimeMillis();
mNotification.setLatestEventInfo(activity, title, message, pIntent); // I don’t think these arguments are relevant, but I can post more code if you want
nm.notify(42, mNotification); // nm is the NotificationManager
Edit:
Ok, I found the cause, Android does not want to display the ticker text again if it is the same as before.
Is there a workaround for that?
Edit #2:
My application is a multiplayer game, I have to keep a network socket open during the time the user is connected.
But I do not want to abruptly disconnect the user when he receives a new SMS and wants to read it (for example). What I want is to have the on going notification saying something like "You will be disconnected in 30 seconds".
I have the problem above when the user is connected, leave the application, come back less than 30 seconds after, and then leave the application again. I repeatedly flash the same message only if the user repeatedly leaves and come back to my application (which shouldn’t occur often, I guess)
Do you think this could harass the user? What should I do instead?

Is there a workaround for that?
Have meaningfully different text that the user will value. Do not harass the user by repeatedly flashing the same message.
UPDATE based on edit
What I want is to have the on going notification saying something like "You will be disconnected in 30 seconds".
That's fine.
I have the problem above when the user is connected, leave the application, come back less than 30 seconds after, and then leave the application again.
The user came back to your activity, which is where you should be canceling the Notification, in which case you will have no problem re-displaying the same ticker message.

You could have two different ongoing notification codes - one for when user is using application, one when user is left. Then when user starts your application, you cancel your "inactive" notification and display "active" notification. And when user leaves again, just cancel "active" notification and display "inactive". This way you should always get ticker when displaying "inactive" notification. If both notifications have the same icon, user might not even notice. When connection terminates, just hide both notifications, so user will know he's offline.

Related

setFullScreenIntent still posting notification to the status bar

As per Android docs for setFullScreenIntent:
An intent to launch instead of posting the notification to the status bar.
Reality: Fullscreen intent is launched....but notification also gets posted to the status bar.
The screen is locked, so the user is not using the device.
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setContentTitle(timer.title)
.setContentText("Description!")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setFullScreenIntent(pendingIntent, true) // tried with false too.
Any ideas why this doesn't work according to the docs?
I know I can remove the notification after it appears but the docs mention clearly that it should not appear.
This is reproducible both on a real device (Android 11) and on an emulator (Android 12).
I also notice the same thing with some other apps from the Play Store. They seem to use such notification (I can't know for sure but they appear on the lockscreen as fullscreen at least).
imho thats some old mistake in docs. I've never read this methods doc so carefully, but my experience says that full screen intent was/is always show Activity ADDITIONALLY to always-present notification. or will not, then freshly posted notification will appear as heads up, e.g. when user is using unlocked device at the moment of notification posting
also docs shows some use cases for such feature
Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock
considering call: user didn't make any touch in GUI, instead just pressed home. Phone is ringing, how to close/reject/get back to managing Activity without Notification left? such Activity won't stay in recents history (noHistory), you may even press this home button accidentally and you don't even know in which app looking for sound source
imho this notification should also appear with started Activity, just like it happens. It isn't even visible on screen when user starts to use your extremely high-priority Activity. When job is done and Activity is finishing then Notification may also disappear, but when user won't take any decision and it is still crucial to pick some option - he need a fast option to get back to (even accidentally) "homed"/closed Activity

Show notification only if previous one has been canceled

I'm creating a very simple application for myself. Basically, it gives me a certain notification exactly every 2 hours.
The problem is when I don't "check" the notification for 2 hours and the next notification is supposed to come around. It sends a notification even though another notification already exists.
I'm simply sending notifications with NotificationManager.
Is there a way to check if previous notification already exists and only send another one if it doesn't?
Assuming you are using the same notification id (so that only one notification appears in the notification tray), you can use setOnlyAlertOnce(true) to ensure that sound/vibrate only plays the very first time a notification is posted and not when an existing notification is updated.

notification bar + push notification clarifications

In my app I am applying push notifications and making them using the notificationCompat class.
I am assigning each notification a unique ID therefore, each one is visible seperately in the notification bar.
When the user clicks on a notification, using a pending intent, I am directing the user towards a certain activity, where he can see all the previous notifications. ( I read them from mysql db ).
question 1 : I want all the notifications in the status bar get cleared if the user clicks only on one of them. Can be a any random one (given it is from my app). the setAutoCancel() method, only removes the one pressed, and I cant seem to know where I would implement the cancelAll() method.
Answer :
#Override
protected void onResume()
{
super.onResume();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
question 2 : Since each notification has unique ID, sending several notifications from an individual app, and in case of not checking from the user, might result in several icons to appear in the notification bar. Android, starts collecting them and showing badge numbers on them once they start taking too much space and the system realizes there is no enough space for them.
Is there any method to combine them all from the beginning ? That is, whenever I send a notification, and it is not checked, the second one should be added and a single icon should appear on the bar with the number 2 or + , whatever the system uses.
I'd like to combine question 1 and 2 into a single answer: Do not show multiple notifications! Show one notification and update it when you have more than one unchecked notification. As a user I would get really annoyed if my notification bar was spammed with notifications from a single app. Think of the GMail app and it's notifications if you would get one per incoming e-mail.
On pre 4.1 devices you'll have to make do with the Normal View. If you have only one unchecked message you could show the contents of the message immediately in the notification, and if you have more than one you could do something like the GMail notification and show something like "5 unchecked messages".
On 4.1+ devices you can use the Big View to show all of them at once.

Prevent multiple notification sounds android

I am working on a chat app in Android using c2dm (gcm). Every received message raises a notification if my app is not able to show it directly. The problem is when the user is off the grid, in which case c2dm messages stack up and get delivered all at once when he is back online. A cacaphony of notification sounds is the result from adding all the notifications in a 1 second period. How to prevent this? Desired behaviour is a that the notification sound is played once, while the tickertext and notification content are up to date with the last received message
I ended up using an alarm wich was set 1 second into the future. I set multiple alarms with the same intent, the earlier ones are automatically deleted. So when a second one comes in in the 1 second interval it deletes the first.
Utilize the collapse_key to only process the last "notification" of the stack
For more info, see https://developers.google.com/cloud-messaging/concept-options#collapsible_and_non-collapsible_messages

Android: Can I check if my app's notification is still in the status bar?

My app pops up a message count notification and sets it as "ongoing". There is a timer that re-sends the notification every 5 minutes, and the notification has the flag Notification.FLAG_ONGOING_EVENT , so that it can only get cleared by my app. And my app is set up to only use sound and vibrate if the count changes.
So, the idea is that if my app changes the count, it sends out an updated notification, which replaces the old one, and plays a sound and vibrates. But if the 5 minute timer tries to update the notification, but the count didn't change, the notification is still sent. but without sound or vibrate. This is done in case the notification somehow got cleared, I want it to pop back up, but if it's still there, I don't want the user to be re-notified.
I also save the message count so that it's remembered if the app is closed and then re-opened. The problem is that when that happens (if it's force-closed, for example), the old notification stays in the bar, but the newly opened app has no idea that's the case. So I'd like to be able to somehow poll the notification service to see if that original notification is still showing, but I can't find any API to do this. Is it possible?
Thanks.
So I'd like to be able to somehow poll the notification service to see if that original notification is still showing, but I can't find any API to do this. Is it possible?
No.
However, you can use deleteIntent to find out if the user cleared the Notification. Either that, or track down the cause of your "somehow got cleared" issue.

Categories

Resources