Skip to content

Commit 3578d69

Browse files
author
AR Abdul Azeez
committed
cleanup after PR
1 parent 252fb7b commit 3578d69

5 files changed

Lines changed: 43 additions & 56 deletions

File tree

Examples/OneSignalDemoV2/BUILDING_THE_APP.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,11 @@ Push Section:
176176
- Section title: "Push" with info icon for tooltip
177177
- Push Subscription ID display (readonly, ellipsize middle)
178178
- Enabled toggle switch (controls optIn/optOut)
179+
- Notification permission is automatically requested when MainActivity loads
179180
- PROMPT PUSH button:
180-
- Only visible when notification permission is NOT granted
181+
- Only visible when notification permission is NOT granted (fallback if user denied)
181182
- Requests notification permission when clicked
182-
- Hide after permission is granted
183+
- Hidden once permission is granted
183184
```
184185

185186
### Prompt 2.3 - Send Push Notification Section

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/application/MainApplication.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ class MainApplication : MultiDexApplication() {
6767
// Set up all OneSignal listeners
6868
setupOneSignalListeners()
6969

70-
// Note: Permission is requested via the "PROMPT PUSH" button in MainActivity
71-
// when the user has not yet granted notification permission.
72-
// This avoids showing the prompt before the user sees the app UI.
70+
// Note: Notification permission is automatically requested when MainActivity loads.
71+
// This ensures the prompt appears after the user sees the app UI.
7372
}
7473

7574
private fun setupOneSignalListeners() {

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/ui/main/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class MainActivity : AppCompatActivity() {
5858
setupRecyclerViews()
5959
setupClickListeners()
6060
setupObservers()
61+
62+
// Automatically prompt for notification permission when activity loads
63+
viewModel.promptPush()
6164
}
6265

6366
private fun setupToolbar() {

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/util/SharedPreferenceUtil.kt

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.onesignal.sdktest.util
22

33
import android.content.Context
44
import android.content.SharedPreferences
5-
import org.json.JSONObject
65

76
object SharedPreferenceUtil {
87

@@ -12,7 +11,6 @@ object SharedPreferenceUtil {
1211
private const val USER_EXTERNAL_USER_ID_SHARED_PREF = "USER_EXTERNAL_USER_ID_SHARED_PREF"
1312
private const val LOCATION_SHARED_PREF = "LOCATION_SHARED_PREF"
1413
private const val IN_APP_MESSAGING_PAUSED_PREF = "IN_APP_MESSAGING_PAUSED_PREF"
15-
private const val TRIGGERS_PREF = "TRIGGERS_PREF"
1614

1715
private fun getSharedPreference(context: Context): SharedPreferences {
1816
return context.getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE)
@@ -43,20 +41,6 @@ object SharedPreferenceUtil {
4341
return getSharedPreference(context).getBoolean(IN_APP_MESSAGING_PAUSED_PREF, true)
4442
}
4543

46-
fun getCachedTriggers(context: Context): Map<String, String> {
47-
val json = getSharedPreference(context).getString(TRIGGERS_PREF, null) ?: return emptyMap()
48-
return try {
49-
val jsonObject = JSONObject(json)
50-
val result = mutableMapOf<String, String>()
51-
jsonObject.keys().forEach { key ->
52-
result[key] = jsonObject.getString(key)
53-
}
54-
result
55-
} catch (e: Exception) {
56-
emptyMap()
57-
}
58-
}
59-
6044
fun cacheOneSignalAppId(context: Context, appId: String) {
6145
getSharedPreference(context).edit().putString(OS_APP_ID_SHARED_PREF, appId).apply()
6246
}
@@ -76,12 +60,4 @@ object SharedPreferenceUtil {
7660
fun cacheInAppMessagingPausedStatus(context: Context, paused: Boolean) {
7761
getSharedPreference(context).edit().putBoolean(IN_APP_MESSAGING_PAUSED_PREF, paused).apply()
7862
}
79-
80-
fun cacheTriggers(context: Context, triggers: Map<String, String>) {
81-
val jsonObject = JSONObject()
82-
triggers.forEach { (key, value) ->
83-
jsonObject.put(key, value)
84-
}
85-
getSharedPreference(context).edit().putString(TRIGGERS_PREF, jsonObject.toString()).apply()
86-
}
8763
}

Examples/OneSignalDemoV2/app/src/main/java/com/onesignal/sdktest/util/TooltipHelper.kt

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package com.onesignal.sdktest.util
22

33
import android.content.Context
44
import androidx.appcompat.app.AlertDialog
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.launch
8+
import kotlinx.coroutines.withContext
59
import org.json.JSONObject
610

711
/**
@@ -15,43 +19,47 @@ object TooltipHelper {
1519
private var initialized = false
1620

1721
/**
18-
* Initialize the tooltip helper by loading content from assets.
22+
* Initialize the tooltip helper by loading content from assets on a background thread.
1923
* Call this once during app startup (e.g., in Application.onCreate()).
2024
*/
2125
fun init(context: Context) {
2226
if (initialized) return
2327

24-
try {
25-
val json = context.assets.open("tooltip_content.json").bufferedReader().use { it.readText() }
26-
val jsonObject = JSONObject(json)
27-
28-
val tooltipMap = mutableMapOf<String, TooltipData>()
29-
30-
jsonObject.keys().forEach { key ->
31-
val tooltipJson = jsonObject.getJSONObject(key)
32-
val title = tooltipJson.getString("title")
33-
val description = tooltipJson.getString("description")
28+
CoroutineScope(Dispatchers.IO).launch {
29+
try {
30+
val json = context.assets.open("tooltip_content.json").bufferedReader().use { it.readText() }
31+
val jsonObject = JSONObject(json)
32+
33+
val tooltipMap = mutableMapOf<String, TooltipData>()
3434

35-
val options = if (tooltipJson.has("options")) {
36-
val optionsArray = tooltipJson.getJSONArray("options")
37-
(0 until optionsArray.length()).map { i ->
38-
val optionJson = optionsArray.getJSONObject(i)
39-
TooltipOption(
40-
name = optionJson.getString("name"),
41-
description = optionJson.getString("description")
42-
)
35+
jsonObject.keys().forEach { key ->
36+
val tooltipJson = jsonObject.getJSONObject(key)
37+
val title = tooltipJson.getString("title")
38+
val description = tooltipJson.getString("description")
39+
40+
val options = if (tooltipJson.has("options")) {
41+
val optionsArray = tooltipJson.getJSONArray("options")
42+
(0 until optionsArray.length()).map { i ->
43+
val optionJson = optionsArray.getJSONObject(i)
44+
TooltipOption(
45+
name = optionJson.getString("name"),
46+
description = optionJson.getString("description")
47+
)
48+
}
49+
} else {
50+
null
4351
}
44-
} else {
45-
null
52+
53+
tooltipMap[key] = TooltipData(title, description, options)
4654
}
4755

48-
tooltipMap[key] = TooltipData(title, description, options)
56+
withContext(Dispatchers.Main) {
57+
tooltips = tooltipMap
58+
initialized = true
59+
}
60+
} catch (e: Exception) {
61+
android.util.Log.e("TooltipHelper", "Failed to load tooltip content", e)
4962
}
50-
51-
tooltips = tooltipMap
52-
initialized = true
53-
} catch (e: Exception) {
54-
android.util.Log.e("TooltipHelper", "Failed to load tooltip content", e)
5563
}
5664
}
5765

0 commit comments

Comments
 (0)