Added game name to turn notification (#5350)

This commit is contained in:
GGGuenni
2021-09-29 16:38:27 +02:00
committed by GitHub
parent 4c87d98aaa
commit 3220206bce
5 changed files with 13 additions and 9 deletions

View File

@ -2,7 +2,7 @@
<resources> <resources>
<string name="app_name">UnCiv</string> <string name="app_name">UnCiv</string>
<string name="Notify_YourTurn_Short">Unciv - Du bist am Zug!</string> <string name="Notify_YourTurn_Short">Unciv - Du bist am Zug!</string>
<string name="Notify_YourTurn_Long">Deine Freunde warten auf deinen Zug.</string> <string name="Notify_YourTurn_Long">Deine Freunde warten auf deinen Zug in [gameName].</string>
<string name="Notify_Error_Short">Ein Fehler ist aufgetreten</string> <string name="Notify_Error_Short">Ein Fehler ist aufgetreten</string>
<string name="Notify_Error_Long">Multiplayer Zug Benachrichtigungsdienst wurde beendet.</string> <string name="Notify_Error_Long">Multiplayer Zug Benachrichtigungsdienst wurde beendet.</string>
<string name="Notify_Persist_Short">Letzter online Zugcheck:</string> <string name="Notify_Persist_Short">Letzter online Zugcheck:</string>

View File

@ -2,7 +2,7 @@
<resources> <resources>
<string name="app_name">UnCiv</string> <string name="app_name">UnCiv</string>
<string name="Notify_YourTurn_Short">Unciv - C\'est à vous !</string> <string name="Notify_YourTurn_Short">Unciv - C\'est à vous !</string>
<string name="Notify_YourTurn_Long">Vos amis attendent votre action.</string> <string name="Notify_YourTurn_Long">Vos amis attendent votre action dans [gameName].</string>
<string name="Notify_Error_Long">Service de notification du tour multijoueur terminé</string> <string name="Notify_Error_Long">Service de notification du tour multijoueur terminé</string>
<string name="Notify_Error_Short">Une erreur est survenue</string> <string name="Notify_Error_Short">Une erreur est survenue</string>
<string name="Notify_Persist_Long_P4">Configurable dans le menu des options de Unciv</string> <string name="Notify_Persist_Long_P4">Configurable dans le menu des options de Unciv</string>

View File

@ -2,7 +2,7 @@
<resources> <resources>
<string name="app_name">UnCiv</string> <string name="app_name">UnCiv</string>
<string name="Notify_YourTurn_Short">Unciv - It\'s your turn!</string> <string name="Notify_YourTurn_Short">Unciv - It\'s your turn!</string>
<string name="Notify_YourTurn_Long">Your friends are waiting on your turn.</string> <string name="Notify_YourTurn_Long">Your friends are waiting for your turn in [gameName].</string>
<string name="Notify_Error_Short">An error has occurred</string> <string name="Notify_Error_Short">An error has occurred</string>
<string name="Notify_Error_Long">Multiplayer turn notifier service terminated.</string> <string name="Notify_Error_Long">Multiplayer turn notifier service terminated.</string>
<string name="Notify_Persist_Short">Last online turn check:</string> <string name="Notify_Persist_Short">Last online turn check:</string>

View File

@ -69,7 +69,7 @@ open class AndroidLauncher : AndroidApplication() {
if (UncivGame.Companion.isCurrentInitialized() if (UncivGame.Companion.isCurrentInitialized()
&& UncivGame.Current.isGameInfoInitialized() && UncivGame.Current.isGameInfoInitialized()
&& UncivGame.Current.settings.multiplayerTurnCheckerEnabled && UncivGame.Current.settings.multiplayerTurnCheckerEnabled
&& UncivGame.Current.gameInfo.gameParameters.isOnlineMultiplayer) { && GameSaver.getSaves(true).any()) {
MultiplayerTurnCheckWorker.startTurnChecker(applicationContext, UncivGame.Current.gameInfo, UncivGame.Current.settings) MultiplayerTurnCheckWorker.startTurnChecker(applicationContext, UncivGame.Current.gameInfo, UncivGame.Current.settings)
} }
super.onPause() super.onPause()

View File

@ -42,6 +42,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
private const val FAIL_COUNT = "FAIL_COUNT" private const val FAIL_COUNT = "FAIL_COUNT"
private const val GAME_ID = "GAME_ID" private const val GAME_ID = "GAME_ID"
private const val GAME_NAME = "GAME_NAME"
private const val USER_ID = "USER_ID" private const val USER_ID = "USER_ID"
private const val CONFIGURED_DELAY = "CONFIGURED_DELAY" private const val CONFIGURED_DELAY = "CONFIGURED_DELAY"
private const val PERSISTENT_NOTIFICATION_ENABLED = "PERSISTENT_NOTIFICATION_ENABLED" private const val PERSISTENT_NOTIFICATION_ENABLED = "PERSISTENT_NOTIFICATION_ENABLED"
@ -136,7 +137,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
} }
} }
fun notifyUserAboutTurn(applicationContext: Context) { fun notifyUserAboutTurn(applicationContext: Context, gameName: String) {
val pendingIntent: PendingIntent = val pendingIntent: PendingIntent =
Intent(applicationContext, AndroidLauncher::class.java).let { notificationIntent -> Intent(applicationContext, AndroidLauncher::class.java).let { notificationIntent ->
PendingIntent.getActivity(applicationContext, 0, notificationIntent, 0) PendingIntent.getActivity(applicationContext, 0, notificationIntent, 0)
@ -146,7 +147,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
val notification: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL_ID_INFO) val notification: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL_ID_INFO)
.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH) // people are waiting! .setPriority(NotificationManagerCompat.IMPORTANCE_HIGH) // people are waiting!
.setContentTitle(contentTitle) .setContentTitle(contentTitle)
.setContentText(applicationContext.resources.getString(R.string.Notify_YourTurn_Long)) .setContentText(applicationContext.resources.getString(R.string.Notify_YourTurn_Long).replace("[gameName]", gameName))
.setTicker(contentTitle) .setTicker(contentTitle)
// without at least vibrate, some Android versions don't show a heads-up notification // without at least vibrate, some Android versions don't show a heads-up notification
.setDefaults(DEFAULT_VIBRATE) .setDefaults(DEFAULT_VIBRATE)
@ -181,9 +182,9 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
if (currentGameInfo.currentPlayerCiv.playerId == settings.userId) { if (currentGameInfo.currentPlayerCiv.playerId == settings.userId) {
// May be useful to remind a player that he forgot to complete his turn. // May be useful to remind a player that he forgot to complete his turn.
notifyUserAboutTurn(applicationContext) notifyUserAboutTurn(applicationContext, gameNames[gameIds.indexOf(currentGameInfo.gameId)])
} else { } else {
val inputData = workDataOf(Pair(FAIL_COUNT, 0), Pair(GAME_ID, gameIds), val inputData = workDataOf(Pair(FAIL_COUNT, 0), Pair(GAME_ID, gameIds), Pair(GAME_NAME, gameNames),
Pair(USER_ID, settings.userId), Pair(CONFIGURED_DELAY, settings.multiplayerTurnCheckerDelayInMinutes), Pair(USER_ID, settings.userId), Pair(CONFIGURED_DELAY, settings.multiplayerTurnCheckerDelayInMinutes),
Pair(PERSISTENT_NOTIFICATION_ENABLED, settings.multiplayerTurnCheckerPersistentNotificationEnabled)) Pair(PERSISTENT_NOTIFICATION_ENABLED, settings.multiplayerTurnCheckerPersistentNotificationEnabled))
@ -226,6 +227,7 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
try { try {
val gameIds = inputData.getStringArray(GAME_ID)!! val gameIds = inputData.getStringArray(GAME_ID)!!
val gameNames = inputData.getStringArray(GAME_NAME)!!
var arrayIndex = 0 var arrayIndex = 0
// We only want to notify the user or update persisted notification once but still want // We only want to notify the user or update persisted notification once but still want
// to download all games to update the files hence this bool // to download all games to update the files hence this bool
@ -250,12 +252,14 @@ class MultiplayerTurnCheckWorker(appContext: Context, workerParams: WorkerParame
if (currentTurnPlayer.playerId == inputData.getString(USER_ID)!!) { if (currentTurnPlayer.playerId == inputData.getString(USER_ID)!!) {
foundGame = true foundGame = true
//As we do not need to look any further we can just break here
break
} }
arrayIndex++ arrayIndex++
} }
if (foundGame){ if (foundGame){
notifyUserAboutTurn(applicationContext) notifyUserAboutTurn(applicationContext, gameNames[arrayIndex])
with(NotificationManagerCompat.from(applicationContext)) { with(NotificationManagerCompat.from(applicationContext)) {
cancel(NOTIFICATION_ID_SERVICE) cancel(NOTIFICATION_ID_SERVICE)
} }