Skip to content

Commit a2329d0

Browse files
committed
Add launch app intent to updater notification
1 parent d39b4e3 commit a2329d0

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed
Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.paulcoding.hviewer.worker
22

33
import android.app.NotificationManager
4+
import android.app.PendingIntent
45
import android.content.Context
6+
import android.content.Intent
57
import androidx.core.app.NotificationCompat
68
import androidx.work.CoroutineWorker
79
import androidx.work.Data
810
import androidx.work.WorkerParameters
911
import com.paulcoding.hviewer.CHECK_FOR_UPDATE_CHANNEL
12+
import com.paulcoding.hviewer.MainActivity
1013
import com.paulcoding.hviewer.R
1114
import com.paulcoding.hviewer.network.Github
1215
import com.paulcoding.hviewer.network.SiteConfigsState
@@ -18,39 +21,46 @@ class UpdateScriptsWorker(
1821
override suspend fun doWork(): Result {
1922
try {
2023
val result = Github.checkVersionOrUpdate()
21-
22-
val notificationManager = context.getSystemService(NotificationManager::class.java)
23-
val notificationBuilder = NotificationCompat.Builder(context, CHECK_FOR_UPDATE_CHANNEL)
24-
.setSmallIcon(R.drawable.ic_launcher_foreground)
25-
.setAutoCancel(true)
26-
27-
when (result) {
28-
is SiteConfigsState.Updated -> {
29-
notificationBuilder
30-
.setContentTitle(context.getString(R.string.scripts_updated))
31-
.setContentText(
32-
context.getString(
33-
R.string.version_,
34-
result.newVersion.toString()
35-
)
36-
)
37-
notificationManager.notify(1, notificationBuilder.build())
38-
}
39-
40-
is SiteConfigsState.NewConfigsInstall -> {
41-
notificationBuilder
42-
.setContentTitle(context.getString(R.string.scripts_installed))
43-
.setContentText(context.getString(R.string.from_repo_, result.repoUrl))
44-
notificationManager.notify(1, notificationBuilder.build())
45-
}
46-
47-
else -> {}
48-
}
24+
notify(context, result)
4925
return Result.success()
5026
} catch (e: Exception) {
5127
e.printStackTrace()
5228
val data = Data.Builder().putString("error", e.message).build()
5329
return Result.failure(data)
5430
}
5531
}
32+
33+
private fun notify(context: Context, result: SiteConfigsState) {
34+
val intent = Intent(context, MainActivity::class.java)
35+
val pendingIntent =
36+
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
37+
val notificationManager = context.getSystemService(NotificationManager::class.java)
38+
val notificationBuilder = NotificationCompat.Builder(context, CHECK_FOR_UPDATE_CHANNEL)
39+
.setSmallIcon(R.drawable.ic_launcher_foreground)
40+
.setContentIntent(pendingIntent)
41+
.setAutoCancel(true)
42+
43+
when (result) {
44+
is SiteConfigsState.Updated -> {
45+
notificationBuilder
46+
.setContentTitle(context.getString(R.string.scripts_updated))
47+
.setContentText(
48+
context.getString(
49+
R.string.version_,
50+
result.newVersion.toString()
51+
)
52+
)
53+
notificationManager.notify(1, notificationBuilder.build())
54+
}
55+
56+
is SiteConfigsState.NewConfigsInstall -> {
57+
notificationBuilder
58+
.setContentTitle(context.getString(R.string.scripts_installed))
59+
.setContentText(context.getString(R.string.from_repo_, result.repoUrl))
60+
notificationManager.notify(1, notificationBuilder.build())
61+
}
62+
63+
else -> {}
64+
}
65+
}
5666
}

0 commit comments

Comments
 (0)