Skip to content

Commit 5aa610c

Browse files
committed
call initWithContext on all entry points
We call OneSignal.initWithContext on most entry points but some were missed. Consistency is important as differences can lead to edge case bugs that are hard to reproduce and debug. One recent example of this was with ReceiveReceiptWorker, its inconsistency lead to a hard to debug issue that was uncovered by commit 893381b. In this commit an issue was discovered in NotificationGenerationWorker. It almost always runs after receiving a push, but not guaranteed. It was checking for OneSignal.isInitialized but instead should always call initWithContext as it can be an entry point to the app process. The ADMMessageHandlerJob may or may not have had an issue, but we matched up the initWithContext behavior to be safe.
1 parent 591098c commit 5aa610c

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/generation/impl/NotificationGenerationWorkManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ internal class NotificationGenerationWorkManager : INotificationGenerationWorkMa
6363

6464
class NotificationGenerationWorker(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
6565
override suspend fun doWork(): Result {
66-
if (!OneSignal.isInitialized) {
67-
return Result.failure()
66+
if (!OneSignal.initWithContext(applicationContext)) {
67+
return Result.success()
6868
}
6969

7070
val notificationProcessor: INotificationGenerationProcessor = OneSignal.getService()

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/receivereceipt/impl/ReceiveReceiptWorkManager.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ internal class ReceiveReceiptWorkManager(
7272
}
7373

7474
class ReceiveReceiptWorker(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {
75-
private var receiveReceiptProcessor: IReceiveReceiptProcessor = OneSignal.getService()
76-
7775
override suspend fun doWork(): Result {
78-
val inputData = inputData
76+
if (!OneSignal.initWithContext(applicationContext)) {
77+
return Result.success()
78+
}
79+
7980
val notificationId = inputData.getString(OS_NOTIFICATION_ID)!!
8081
val appId = inputData.getString(OS_APP_ID)!!
8182
val subscriptionId = inputData.getString(OS_SUBSCRIPTION_ID)!!
83+
84+
val receiveReceiptProcessor = OneSignal.getService<IReceiveReceiptProcessor>()
8285
receiveReceiptProcessor.sendReceiveReceipt(appId, subscriptionId, notificationId)
8386
return Result.success()
8487
}

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/services/ADMMessageHandler.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import com.onesignal.notifications.internal.registration.impl.IPushRegistratorCa
1313
class ADMMessageHandler : ADMMessageHandlerBase("ADMMessageHandler") {
1414
override fun onMessage(intent: Intent) {
1515
val context = applicationContext
16+
if (!OneSignal.initWithContext(context)) {
17+
return
18+
}
19+
1620
val bundle = intent.extras
1721

1822
val bundleProcessor = OneSignal.getService<INotificationBundleProcessor>()

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/services/ADMMessageHandlerJob.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ class ADMMessageHandlerJob : ADMMessageHandlerJobBase() {
1414
context: Context?,
1515
intent: Intent?,
1616
) {
17-
val bundle = intent?.extras
18-
17+
if (context == null) {
18+
return
19+
}
20+
if (!OneSignal.initWithContext(context.applicationContext)) {
21+
return
22+
}
1923
val bundleProcessor = OneSignal.getService<INotificationBundleProcessor>()
2024

25+
val bundle = intent?.extras
26+
2127
bundleProcessor.processBundleFromReceiver(context!!, bundle!!)
2228
}
2329

0 commit comments

Comments
 (0)