Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
Kalo2412 opened this issue Jul 31, 2024 · 4 comments
Open
1 task done

Comments

@Kalo2412
Copy link

Kalo2412 commented Jul 31, 2024

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
@jkasten2
Copy link
Member

jkasten2 commented Jul 31, 2024

@Kalo2412 to avoid the java.lang.Exception: Must call 'initWithContext' before use error and to prevent other initialization issues you should call OneSignal.initWithContext in your Application.onCreate instead of your Activity.onCreate.

Note that OneSignal.Notifications.requestPermission() only prompts on Android 13 and newer devices, as notifications are enabled by default on older devices. It's possible for all versions of Android to turn off notifications so calling OneSignal.Notifications.requestPermission(true) will take them to the settings if they turned it off.

@Kalo2412
Copy link
Author

Kalo2412 commented Aug 1, 2024

I have now moved the logic in the MyApplication

**
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()

    // Initialize OneSignal
    OneSignal.Debug.logLevel = LogLevel.VERBOSE
    OneSignal.initWithContext(this)


    CoroutineScope(Dispatchers.IO).launch {
        OneSignal.Notifications.requestPermission(true)
    }
}

}
**

I still get this error: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.ts.org, PID: 17941
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.MyApplication$onCreate$1.invokeSuspend(MainApplication.kt:21)
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}@c16b1d9, Dispatchers.IO]

Device is on Android 14

@Kalo2412
Copy link
Author

Kalo2412 commented Aug 13, 2024

@jkasten2
What is the difference between this two methods

@kotlin.jvm.JvmStatic public final fun initWithContext(context: android.content.Context): kotlin.Boolean { /* compiled code */ }


@kotlin.jvm.JvmStatic public final fun initWithContext(context: android.content.Context, appId: kotlin.String): kotlin.Unit { /* compiled code */ }

Why should some one call initWithContext without to pass the appID if so can we pass the appid later in the application in some service class

Furthermore I do not find any docs page for the android sdk for the link provided in the README I receive page not found https://documentation.onesignal.com/docs/android-native-sdk

@Kalo2412
Copy link
Author

Any progress here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants