pusher / push-notifications-android

Android SDK for Pusher Beams
https://www.pusher.com/beams
MIT License
21 stars 21 forks source link

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked #135

Closed sebj closed 1 year ago

sebj commented 1 year ago

We're seeing an infrequent crash show up in Sentry with the following RuntimeException after adding this library (version 1.9.0) to our app:

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked

PushNotifications.start(..) is currently called as part of onCreate in our Application subclass. The relevant part of the stack trace is as follows:

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked
    at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:572)
    at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:557)
    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:217)
    at com.pusher.pushnotifications.internal.InstanceDeviceStateStore.<init>(DeviceStateStore.kt:83)
    at com.pusher.pushnotifications.internal.InstanceDeviceStateStore$Companion.obtain$pushnotifications_release(DeviceStateStore.kt:55)
    at com.pusher.pushnotifications.PushNotificationsInstance.<init>(PushNotificationsInstance.kt:102)
    at com.pusher.pushnotifications.PushNotifications.start(PushNotifications.java:32)
    at com.daybridge.android.DaybridgeApplication.initializeNotifications(DaybridgeApplication.kt:87)
    at com.daybridge.android.DaybridgeApplication.onCreate(DaybridgeApplication.kt:46)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1269)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6785)
    at android.app.ActivityThread.-$$Nest$mhandleBindApplication
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2134)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7893)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
benw-pusher commented 1 year ago

This comes when the device is in Direct Boot Mode and the user has not yet unlocked the device. Once the device is unlocked the storage is unencrypted and available for use. I'll look at if there is anything we can do to assist in this scenario.

benw-pusher commented 1 year ago

Hi @sebj, we've taken a look into this and we'd need to make some changes to be able to support Direct Boot aware apps and this isn't something we are able to commit to at this time. I've added it to our internal tracking for consideration in a future iteration.

sebj commented 1 year ago

Thanks for the update @benw-pusher