Removing a notification using a String tag and int id?

I want to send a notification to the notification tray. This is for a multiplayer game, and I want to notify the user when it is their turn to make a move. They can participate in several games at the same time, and I will send one notification per game when / if it is their movement. Each game has a unique identifier, so I wanted to send notifications, for example:

int TYPE_NOTIF_YOUR_TURN = 0; String gameId1 = "abc"; String gameId2 = "xyz"; mgr.notify(gameId1, TYPE_NOTIF_YOUR_TURN, notification); mgr.notify(gameId2, TYPE_NOTIF_YOUR_TURN, notification); 

Now, if I need to cancel one of the notifications, can I look with the tag (in my case, gameId) ?:

 // only remove the second notification: mgr.cancel(gameId2, TYPE_NOTIF_YOUR_TURN); 

How it works?

thanks

+4
source share
1 answer

Yes, exactly how it works.

However, on the side, note that String tags will be unique to your application package. Example...

 String gameId1 = "com.mycompany.mypackage.gameid.abc"; String gameId2 = "com.mycompany.mypackage.gameid.xyz"; 

The rationale for this is that when canceling, the lines and int must match to identify the notification for cancellation. Many applications are likely to use int 1, 2, 3, etc. Thus, the simpler the string tag, the more likely it is that another application (using simple tags) may accidentally cancel one of your notifications.

In general, this seems unlikely, but unique tags avoid it. It also seems likely that this would be recommended, although I did not know much about this.

+4
source

Source: https://habr.com/ru/post/1341888/


All Articles