开发者

Updating the tickerText of a notification

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 sa开发者_Python百科me 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜