OneSignal/OneSignal-Android-SDK

[Bug]: Subscription Recovery Fails When Closing App Programmatically

Closed this issue · 6 comments

What happened?

When manually closing the app from the recent tasks list (Example A), I observe the following logs:

[main] initWithContext(context: com.mugler.sitemanTicketSystem.MainActivity@d4f6894, appId: APP_ID)
[main] initWithContext: SDK initializing


and many more logs after it...

When closing the app using activity.finish() or activity.finishAndRemoveTask() (Example B), the logs are different:

[main] ApplicationService.onActivityCreated(0,APP_CLOSE): com.mugler.sitemanTicketSystem.MainActivity@8e65b63
[main] initWithContext(context: com.mugler.sitemanTicketSystem.MainActivity@8e65b63, appId: APP_ID)
[main] initWithContext: SDK already initialized


and just a few more

This different behavior is causing a new bug.

I have a successful subscription, as seen on the OneSignal Dashboard. When I delete my subscription on the dashboard for testing, and then call finish(), I see the logs from Example B. However, my subscription is not recovered because the logs indicate "SDK already initialized."

If I manually remove the app from the recent tasks list, I get the logs from Example A, and my old subscription is correctly recovered.

Steps to reproduce?

- Ensure a successful subscription is visible on the OneSignal Dashboard.
- Delete the subscription from the OneSignal Dashboard for testing purposes.
- Close the app using activity.finish() or activity.finishAndRemoveTask().
- Observe the logs showing "SDK already initialized".
- Note that the subscription is not recovered.

What did you expect to happen?

I expected the subscription to be recovered correctly, with the logs indicating the SDK initialization process starting anew, as it does when manually removing the app from the recent tasks list.

OneSignal Android SDK version

5.1.18

Android version

14

Specific Android models

Samsung A54

Relevant log output

No response

Code of Conduct

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

Hi @michael-winkler, thanks for reaching out.

To clarify the two scenarios, in Example A, you are swiping away (quitting) the app from the recent tasks list, and then reopening the app and seeing those logs? Or are you putting the app to the background instead of killing the app?

And in Example B, is the app process killed?

Basically, it all depends on if the app process is killed or just in the background. The SDK will initialize the process anew if it is opened from an app killed state.

Data from the server will refresh on cold starts mentioned above or if the app is put in the background for 30-60 seconds and opened.

Yes I swipe it away from the list in example a.

Example b:
I dont know if the process is killed. I just call finish().

Is there any way for us as developer to let the sdk make a full init by ourself?

Also, why is the init of the sdk still cached after closing the App with finish()?

Looking at Activity.finish(), "Call this when your activity is done and should be closed" seems to indicate only Activity level changes and not application level, meaning the app process will still be running and in memory until the OS decides to kill it.

Is there any way for us as developer to let the sdk make a full init by ourself?

If the app process is killed or no longer in memory. What are you trying to achieve with doing another init? Perhaps there are some other workarounds I can suggest.

Also, why is the init of the sdk still cached after closing the App with finish()?

The app still remains in memory so the SDK remains in memory.

Yeah makes now sense. Thank you.

But the problem is that the sdk should also check if the subscription on backend still exist on onesignal backend no matter if the sdk is still initialized or not?

And the next question:
Can this be a problem too if the notification permission was changed or does the sdk still notice this change?

@nan-li A answer would be great 🙂

But the problem is that the sdk should also check if the subscription on backend still exist on onesignal backend no matter if the sdk is still initialized or not?

A server refresh is done on every cold start of the app and after the app being backgrounded (not killed) for more than 30-60 seconds. This is when a subscription that has been deleted via the REST API or dashboard will be known by the SDK. If you do activity.finish() and the app is no longer in the foreground, wait about a minute to be safe and re-open the app. You should see some server calls to get the user state from the server.

And the next question:
Can this be a problem too if the notification permission was changed or does the sdk still notice this change?

Do you mean the notification permission was changed via REST API? In this case, no, the SDK has the final word on notification permission since it must originate from the device.

If you mean when notification permission is changed in the app / device, this data will be synced to the server fairly quickly, typically within 5 seconds.