Skip to content

[question]: I am not able to ask for notification permission on app start #2159

@Kalo2412

Description

@Kalo2412

How can we help?

Description:

I have an Android Jetpack Compose App with indeed has a login page -> because of that I need to wait for a user to login and than retrieve the credentials together with the appID

Therefore here is my code

class MainActivity : ComponentActivity() {
                private val Context.dataStore by preferencesDataStore(name = "settings")
                override fun onCreate(savedInstanceState: Bundle?) {
                    super.onCreate(savedInstanceState)
                    val environmentManager = EnvironmentManager()
                    val dataStorageRepository = DataStorageRepository(dataStore)
                    val context = applicationContext
            
                    // Should be removed when releasing
                    OneSignal.Debug.logLevel = LogLevel.VERBOSE
                    OneSignal.initWithContext(context)
            
            
                    CoroutineScope(Dispatchers.IO).launch {
                        OneSignal.Notifications.requestPermission(true)
                    }
            
                    setContent {
                        ....
                    }
                }
            }

The problem is that as soon as I start the app I get in the logcat those errors

FATAL EXCEPTION: DefaultDispatcher-worker-3
Process: com.ts.org, PID: 32008
java.lang.Exception: Must call 'initWithContext' before use
		at com.onesignal.internal.OneSignalImp.getNotifications(OneSignalImp.kt:99)
		at com.onesignal.OneSignal.getNotifications(OneSignal.kt:62)
		at com.ts.org.app.host.MainActivity$onCreate$1.invokeSuspend(MainActivity.kt:53)
		at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
		at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
		at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
		at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
		at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
		at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
		at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
		at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
		Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@bb53738, Dispatchers.IO]

Furthermore, when a user is logged in - in another class I do this

fun subscribe(creds: ...., appID: String) {
        OneSignal.Debug.logLevel = LogLevel.VERBOSE
        OneSignal.initWithContext(context, appID)


        CoroutineScope(Dispatchers.IO).launch {
            val accepted = OneSignal.Notifications.requestPermission(true)
            if (accepted) {
                Log.i(TAG, "User accepted notifications")
                OneSignal.login(creds.userID)
            }
        }
    }

In the other class I never get the popup dialog for accepting the permissions for push notifications.
Version: 5.1.18
Question: What is wrong how can I proceed?

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions