Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 445deda

Browse files
committed
fix ServiceNotification code samples
1 parent cb9f9bf commit 445deda

6 files changed

Lines changed: 153 additions & 30 deletions

File tree

docs/topl-service/io.matthewnelson.topl_service.notification/-service-notification/-builder/-init-.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ is brought to the Foreground when the user removes your task from the recent app
1212
in order to properly shut down Tor and clean up w/o being killed by the OS.
1313

1414
``` kotlin
15-
// private fun generateTorServiceNotificationBuilder(): ServiceNotification.Builder {
15+
// private fun generateTorServiceNotificationBuilder(context: Context): ServiceNotification.Builder {
1616
return ServiceNotification.Builder(
1717
channelName = "TOPL-Android Demo",
1818
channelDescription = "TorOnionProxyLibrary-Android Demo",
1919
channelID = "TOPL-Android Demo",
2020
notificationID = 615
2121
)
22-
// Only needed if you are passing a bundle, or changing request code to something other than 0
23-
.setContentIntentData(bundle = null, requestCode = 21)
24-
2522
.setImageTorNetworkingEnabled(drawableRes = R.drawable.tor_stat_network_enabled)
2623
.setImageTorNetworkingDisabled(drawableRes = R.drawable.tor_stat_network_disabled)
2724
.setImageTorDataTransfer(drawableRes = R.drawable.tor_stat_network_dataxfer)
@@ -31,6 +28,34 @@ in order to properly shut down Tor and clean up w/o being killed by the OS.
3128
.enableTorRestartButton(enable = true)
3229
.enableTorStopButton(enable = true)
3330
.showNotification(show = true)
31+
32+
// Set the notification's contentIntent for when the user clicks the notification
33+
.also { builder ->
34+
context.applicationContext.packageManager
35+
?.getLaunchIntentForPackage(context.applicationContext.packageName)
36+
?.let { intent ->
37+
38+
// Set in your manifest for the launch activity so the intent won't launch
39+
// a new activity over top of your already created activity if the app is
40+
// open when the user clicks the notification:
41+
//
42+
// android:launchMode="singleInstance"
43+
//
44+
// For more info on launchMode and Activity Intent flags, see:
45+
//
46+
// https://medium.com/swlh/truly-understand-tasks-and-back-stack-intent-flags-of-activity-2a137c401eca
47+
48+
builder.setContentIntent(
49+
PendingIntent.getActivity(
50+
context.applicationContext,
51+
0, // Your desired request code
52+
intent,
53+
0 // flags
54+
// can also include a bundle if desired
55+
)
56+
)
57+
}
58+
}
3459
// }
3560
```
3661

docs/topl-service/io.matthewnelson.topl_service.notification/-service-notification/-builder/index.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ is brought to the Foreground when the user removes your task from the recent app
1212
in order to properly shut down Tor and clean up w/o being killed by the OS.
1313

1414
``` kotlin
15-
// private fun generateTorServiceNotificationBuilder(): ServiceNotification.Builder {
15+
// private fun generateTorServiceNotificationBuilder(context: Context): ServiceNotification.Builder {
1616
return ServiceNotification.Builder(
1717
channelName = "TOPL-Android Demo",
1818
channelDescription = "TorOnionProxyLibrary-Android Demo",
1919
channelID = "TOPL-Android Demo",
2020
notificationID = 615
2121
)
22-
// Only needed if you are passing a bundle, or changing request code to something other than 0
23-
.setContentIntentData(bundle = null, requestCode = 21)
24-
2522
.setImageTorNetworkingEnabled(drawableRes = R.drawable.tor_stat_network_enabled)
2623
.setImageTorNetworkingDisabled(drawableRes = R.drawable.tor_stat_network_disabled)
2724
.setImageTorDataTransfer(drawableRes = R.drawable.tor_stat_network_dataxfer)
@@ -31,6 +28,34 @@ in order to properly shut down Tor and clean up w/o being killed by the OS.
3128
.enableTorRestartButton(enable = true)
3229
.enableTorStopButton(enable = true)
3330
.showNotification(show = true)
31+
32+
// Set the notification's contentIntent for when the user clicks the notification
33+
.also { builder ->
34+
context.applicationContext.packageManager
35+
?.getLaunchIntentForPackage(context.applicationContext.packageName)
36+
?.let { intent ->
37+
38+
// Set in your manifest for the launch activity so the intent won't launch
39+
// a new activity over top of your already created activity if the app is
40+
// open when the user clicks the notification:
41+
//
42+
// android:launchMode="singleInstance"
43+
//
44+
// For more info on launchMode and Activity Intent flags, see:
45+
//
46+
// https://medium.com/swlh/truly-understand-tasks-and-back-stack-intent-flags-of-activity-2a137c401eca
47+
48+
builder.setContentIntent(
49+
PendingIntent.getActivity(
50+
context.applicationContext,
51+
0, // Your desired request code
52+
intent,
53+
0 // flags
54+
// can also include a bundle if desired
55+
)
56+
)
57+
}
58+
}
3459
// }
3560
```
3661

docs/topl-service/io.matthewnelson.topl_service/-tor-service-controller/-builder/-init-.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ The [TorServiceController.Builder](index.md) is where you get to customize how [
88
for your application. Call it in `Application.onCreate` and follow along.
99

1010
``` kotlin
11-
// private fun generateTorServiceNotificationBuilder(): ServiceNotification.Builder {
11+
// private fun generateTorServiceNotificationBuilder(context: Context): ServiceNotification.Builder {
1212
return ServiceNotification.Builder(
1313
channelName = "TOPL-Android Demo",
1414
channelDescription = "TorOnionProxyLibrary-Android Demo",
1515
channelID = "TOPL-Android Demo",
1616
notificationID = 615
1717
)
18-
// Only needed if you are passing a bundle, or changing request code to something other than 0
19-
.setContentIntentData(bundle = null, requestCode = 21)
20-
2118
.setImageTorNetworkingEnabled(drawableRes = R.drawable.tor_stat_network_enabled)
2219
.setImageTorNetworkingDisabled(drawableRes = R.drawable.tor_stat_network_disabled)
2320
.setImageTorDataTransfer(drawableRes = R.drawable.tor_stat_network_dataxfer)
@@ -27,6 +24,34 @@ for your application. Call it in `Application.onCreate` and follow along.
2724
.enableTorRestartButton(enable = true)
2825
.enableTorStopButton(enable = true)
2926
.showNotification(show = true)
27+
28+
// Set the notification's contentIntent for when the user clicks the notification
29+
.also { builder ->
30+
context.applicationContext.packageManager
31+
?.getLaunchIntentForPackage(context.applicationContext.packageName)
32+
?.let { intent ->
33+
34+
// Set in your manifest for the launch activity so the intent won't launch
35+
// a new activity over top of your already created activity if the app is
36+
// open when the user clicks the notification:
37+
//
38+
// android:launchMode="singleInstance"
39+
//
40+
// For more info on launchMode and Activity Intent flags, see:
41+
//
42+
// https://medium.com/swlh/truly-understand-tasks-and-back-stack-intent-flags-of-activity-2a137c401eca
43+
44+
builder.setContentIntent(
45+
PendingIntent.getActivity(
46+
context.applicationContext,
47+
0, // Your desired request code
48+
intent,
49+
0 // flags
50+
// can also include a bundle if desired
51+
)
52+
)
53+
}
54+
}
3055
// }
3156
```
3257

@@ -44,7 +69,7 @@ for your application. Call it in `Application.onCreate` and follow along.
4469
// private fun setupTorServices(application: Application, torConfigFiles: TorConfigFiles ) {
4570
TorServiceController.Builder(
4671
application = application,
47-
torServiceNotificationBuilder = generateTorServiceNotificationBuilder(),
72+
torServiceNotificationBuilder = generateTorServiceNotificationBuilder(application),
4873
backgroundManagerPolicy = generateBackgroundManagerPolicy(),
4974
buildConfigVersionCode = BuildConfig.VERSION_CODE,
5075

docs/topl-service/io.matthewnelson.topl_service/-tor-service-controller/-builder/index.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ The [TorServiceController.Builder](./index.md) is where you get to customize how
88
for your application. Call it in `Application.onCreate` and follow along.
99

1010
``` kotlin
11-
// private fun generateTorServiceNotificationBuilder(): ServiceNotification.Builder {
11+
// private fun generateTorServiceNotificationBuilder(context: Context): ServiceNotification.Builder {
1212
return ServiceNotification.Builder(
1313
channelName = "TOPL-Android Demo",
1414
channelDescription = "TorOnionProxyLibrary-Android Demo",
1515
channelID = "TOPL-Android Demo",
1616
notificationID = 615
1717
)
18-
// Only needed if you are passing a bundle, or changing request code to something other than 0
19-
.setContentIntentData(bundle = null, requestCode = 21)
20-
2118
.setImageTorNetworkingEnabled(drawableRes = R.drawable.tor_stat_network_enabled)
2219
.setImageTorNetworkingDisabled(drawableRes = R.drawable.tor_stat_network_disabled)
2320
.setImageTorDataTransfer(drawableRes = R.drawable.tor_stat_network_dataxfer)
@@ -27,6 +24,34 @@ for your application. Call it in `Application.onCreate` and follow along.
2724
.enableTorRestartButton(enable = true)
2825
.enableTorStopButton(enable = true)
2926
.showNotification(show = true)
27+
28+
// Set the notification's contentIntent for when the user clicks the notification
29+
.also { builder ->
30+
context.applicationContext.packageManager
31+
?.getLaunchIntentForPackage(context.applicationContext.packageName)
32+
?.let { intent ->
33+
34+
// Set in your manifest for the launch activity so the intent won't launch
35+
// a new activity over top of your already created activity if the app is
36+
// open when the user clicks the notification:
37+
//
38+
// android:launchMode="singleInstance"
39+
//
40+
// For more info on launchMode and Activity Intent flags, see:
41+
//
42+
// https://medium.com/swlh/truly-understand-tasks-and-back-stack-intent-flags-of-activity-2a137c401eca
43+
44+
builder.setContentIntent(
45+
PendingIntent.getActivity(
46+
context.applicationContext,
47+
0, // Your desired request code
48+
intent,
49+
0 // flags
50+
// can also include a bundle if desired
51+
)
52+
)
53+
}
54+
}
3055
// }
3156
```
3257

@@ -44,7 +69,7 @@ for your application. Call it in `Application.onCreate` and follow along.
4469
// private fun setupTorServices(application: Application, torConfigFiles: TorConfigFiles ) {
4570
TorServiceController.Builder(
4671
application = application,
47-
torServiceNotificationBuilder = generateTorServiceNotificationBuilder(),
72+
torServiceNotificationBuilder = generateTorServiceNotificationBuilder(application),
4873
backgroundManagerPolicy = generateBackgroundManagerPolicy(),
4974
buildConfigVersionCode = BuildConfig.VERSION_CODE,
5075

sampleapp/src/main/java/io/matthewnelson/sampleapp/App.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,7 @@ class App: Application() {
194194
val prefs = Prefs.createUnencrypted(PREFS_NAME, this)
195195

196196
packageManager?.getLaunchIntentForPackage(packageName)?.let { intent ->
197-
contentIntent = PendingIntent.getActivity(
198-
this.applicationContext,
199-
0,
200-
intent,
201-
0
202-
)
197+
contentIntent = PendingIntent.getActivity(this.applicationContext, 0, intent, 0)
203198
}
204199

205200
val serviceNotificationBuilder = generateTorServiceNotificationBuilder(

sampleapp/src/main/java/io/matthewnelson/sampleapp/topl_android/CodeSamples.kt

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@
7272
package io.matthewnelson.sampleapp.topl_android
7373

7474
import android.app.Application
75+
import android.app.PendingIntent
7576
import android.content.Context
77+
import android.content.Intent
7678
import androidx.core.app.NotificationCompat
79+
import io.matthewnelson.sampleapp.App
7780
import io.matthewnelson.sampleapp.BuildConfig
7881
import io.matthewnelson.sampleapp.R
7982
import io.matthewnelson.topl_core_base.TorConfigFiles
@@ -88,17 +91,14 @@ import java.io.File
8891
* */
8992
class CodeSamples {
9093

91-
private fun generateTorServiceNotificationBuilder(): ServiceNotification.Builder {
92-
// private fun generateTorServiceNotificationBuilder(): ServiceNotification.Builder {
94+
private fun generateTorServiceNotificationBuilder(context: Context): ServiceNotification.Builder {
95+
// private fun generateTorServiceNotificationBuilder(context: Context): ServiceNotification.Builder {
9396
return ServiceNotification.Builder(
9497
channelName = "TOPL-Android Demo",
9598
channelDescription = "TorOnionProxyLibrary-Android Demo",
9699
channelID = "TOPL-Android Demo",
97100
notificationID = 615
98101
)
99-
// Only needed if you are passing a bundle, or changing request code to something other than 0
100-
.setContentIntentData(bundle = null, requestCode = 21)
101-
102102
.setImageTorNetworkingEnabled(drawableRes = R.drawable.tor_stat_network_enabled)
103103
.setImageTorNetworkingDisabled(drawableRes = R.drawable.tor_stat_network_disabled)
104104
.setImageTorDataTransfer(drawableRes = R.drawable.tor_stat_network_dataxfer)
@@ -108,6 +108,34 @@ class CodeSamples {
108108
.enableTorRestartButton(enable = true)
109109
.enableTorStopButton(enable = true)
110110
.showNotification(show = true)
111+
112+
// Set the notification's contentIntent for when the user clicks the notification
113+
.also { builder ->
114+
context.applicationContext.packageManager
115+
?.getLaunchIntentForPackage(context.applicationContext.packageName)
116+
?.let { intent ->
117+
118+
// Set in your manifest for the launch activity so the intent won't launch
119+
// a new activity over top of your already created activity if the app is
120+
// open when the user clicks the notification:
121+
//
122+
// android:launchMode="singleInstance"
123+
//
124+
// For more info on launchMode and Activity Intent flags, see:
125+
//
126+
// https://medium.com/swlh/truly-understand-tasks-and-back-stack-intent-flags-of-activity-2a137c401eca
127+
128+
builder.setContentIntent(
129+
PendingIntent.getActivity(
130+
context.applicationContext,
131+
0, // Your desired request code
132+
intent,
133+
0 // flags
134+
// can also include a bundle if desired
135+
)
136+
)
137+
}
138+
}
111139
// }
112140
}
113141

@@ -125,7 +153,7 @@ class CodeSamples {
125153
// private fun setupTorServices(application: Application, torConfigFiles: TorConfigFiles ) {
126154
TorServiceController.Builder(
127155
application = application,
128-
torServiceNotificationBuilder = generateTorServiceNotificationBuilder(),
156+
torServiceNotificationBuilder = generateTorServiceNotificationBuilder(application),
129157
backgroundManagerPolicy = generateBackgroundManagerPolicy(),
130158
buildConfigVersionCode = BuildConfig.VERSION_CODE,
131159

0 commit comments

Comments
 (0)