Skip to content

Commit d9f4023

Browse files
committed
[User Model] Safeguard against activities/services/receivers being called before initialization.
* Update `IOneSignal.initWithContext` interface to return whether initialization was successful or not. * Implementation fails initialization if no app ID is provided and no app ID has been saved. * Update all activities/services/receivers to check and not proceed when initialization unsuccessful.
1 parent 3ad1b97 commit d9f4023

File tree

14 files changed

+53
-24
lines changed

14 files changed

+53
-24
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/IOneSignal.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ interface IOneSignal {
8080
*
8181
* @param context The Android context the SDK should use.
8282
* @param appId The application ID the OneSignal SDK is bound to.
83+
*
84+
* @return true if the SDK could be successfully initialized, false otherwise.
8385
*/
84-
fun initWithContext(context: Context, appId: String?)
86+
fun initWithContext(context: Context, appId: String?) : Boolean
8587

8688
/**
8789
* Login to OneSignal under the user identified by the [externalId] provided. The act of

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/OneSignal.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ object OneSignal {
192192
* THIS IS AN INTERNAL INTERFACE AND SHOULD NOT BE USED DIRECTLY.
193193
*/
194194
@JvmStatic
195-
fun initWithContext(context: Context) {
196-
oneSignal.initWithContext(context, null)
195+
fun initWithContext(context: Context) : Boolean {
196+
return oneSignal.initWithContext(context, null)
197197
}
198198

199199
/**

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/activities/PermissionsActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class PermissionsActivity : Activity() {
2222

2323
override fun onCreate(savedInstanceState: Bundle?) {
2424
super.onCreate(savedInstanceState)
25-
OneSignal.initWithContext(this)
25+
26+
if(!OneSignal.initWithContext(this)) {
27+
return
28+
}
2629

2730
_requestPermissionService = OneSignal.getService()
2831
_preferenceService = OneSignal.getService()

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/services/SyncJobService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ import com.onesignal.debug.internal.logging.Logging
3838
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
3939
class SyncJobService : JobService() {
4040
override fun onStartJob(jobParameters: JobParameters): Boolean {
41-
OneSignal.initWithContext(this)
41+
if(!OneSignal.initWithContext(this)) {
42+
return false
43+
}
4244

4345
var backgroundService = OneSignal.getService<IBackgroundManager>()
4446

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/services/SyncService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ import com.onesignal.debug.internal.logging.Logging
3636

3737
class SyncService : Service() {
3838
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
39-
OneSignal.initWithContext(this)
39+
if(!OneSignal.initWithContext(this)) {
40+
return START_STICKY
41+
}
4042

4143
var backgroundService = OneSignal.getService<IBackgroundManager>()
4244

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
135135
_services = serviceBuilder.build()
136136
}
137137

138-
override fun initWithContext(context: Context, appId: String?) {
138+
override fun initWithContext(context: Context, appId: String?) : Boolean {
139139
Logging.log(LogLevel.DEBUG, "initWithContext(context: $context, appId: $appId)")
140140

141141
// do not do this again if already initialized
142142
if (isInitialized) {
143-
return
143+
return true
144144
}
145145

146146
// start the application service. This is called explicitly first because we want
@@ -156,6 +156,14 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
156156
_configModel = _services.getService<ConfigModelStore>().model
157157
_sessionModel = _services.getService<SessionModelStore>().model
158158

159+
// initWithContext is called by our internal services/receivers/activites but they do not provide
160+
// an appId (they don't know it). If the app has never called the external initWithContext
161+
// prior to our services/receivers/activities we will blow up, as no appId has been established.
162+
if (appId == null && !_configModel!!.hasProperty(ConfigModel::appId.name)) {
163+
Logging.warn("initWithContext called without providing appId, and no appId has been established!")
164+
return false
165+
}
166+
159167
var forceCreateUser = false
160168
// if the app id was specified as input, update the config model with it
161169
if (appId != null) {
@@ -239,6 +247,8 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
239247
_startupService!!.start()
240248

241249
isInitialized = true
250+
251+
return true
242252
}
243253

244254
override fun login(externalId: String, jwtBearerToken: String?) {

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/activities/NotificationOpenedActivityBase.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ import com.onesignal.notifications.internal.open.INotificationOpenedProcessor
3636
abstract class NotificationOpenedActivityBase : Activity() {
3737
override fun onCreate(savedInstanceState: Bundle?) {
3838
super.onCreate(savedInstanceState)
39-
OneSignal.initWithContext(applicationContext)
39+
if(!OneSignal.initWithContext(applicationContext)) {
40+
return
41+
}
4042

4143
var self = this
4244

@@ -50,7 +52,9 @@ abstract class NotificationOpenedActivityBase : Activity() {
5052

5153
override fun onNewIntent(intent: Intent) {
5254
super.onNewIntent(intent)
53-
OneSignal.initWithContext(applicationContext)
55+
if(!OneSignal.initWithContext(applicationContext)) {
56+
return
57+
}
5458

5559
var self = this
5660
suspendifyOnThread {

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/activities/NotificationOpenedActivityHMS.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ class NotificationOpenedActivityHMS : Activity() {
7272
}
7373

7474
private fun processOpen(intent: Intent?) {
75-
OneSignal.initWithContext(applicationContext)
75+
if (!OneSignal.initWithContext(applicationContext)) {
76+
return
77+
}
7678

7779
var notificationPayloadProcessorHMS = OneSignal.getService<INotificationOpenedProcessorHMS>()
7880
val self = this

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/bridges/OneSignalHmsEventBridge.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ object OneSignalHmsEventBridge {
5353
}
5454

5555
fun onMessageReceived(context: Context, message: RemoteMessage) {
56-
OneSignal.initWithContext(context)
56+
if(!OneSignal.initWithContext(context)) {
57+
return
58+
}
5759

5860
var time = OneSignal.getService<ITime>()
5961
val bundleProcessor = OneSignal.getService<INotificationBundleProcessor>()

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/restoration/impl/NotificationRestoreWorkManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ internal class NotificationRestoreWorkManager : INotificationRestoreWorkManager
4545
override suspend fun doWork(): Result {
4646
val context = applicationContext
4747

48-
if (!OneSignal.isInitialized) {
49-
OneSignal.initWithContext(context)
48+
if (!OneSignal.initWithContext(context)) {
49+
return Result.success();
5050
}
5151

5252
if (!NotificationHelper.areNotificationsEnabled(context)) {

0 commit comments

Comments
 (0)