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

intent.setFlags recommend #663

Closed
cuichanghao opened this issue Nov 9, 2018 · 5 comments
Closed

intent.setFlags recommend #663

cuichanghao opened this issue Nov 9, 2018 · 5 comments

Comments

@cuichanghao
Copy link

cuichanghao commented Nov 9, 2018

sometime onesignal will not start activity if activity backstack already existed.
after 4 hour research i found this is intent flag problem.

    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

How to reproduce:
1.kill application.
2.touch onesignal notification.
3.start some activity.( with (applicationContext) )。and manually start some your other activity.
4.touch onesignal other notification.
4.just bring up previous activity, not my specify activity with parameter. ( with (applicationContext) )

https://developer.android.com/reference/android/content/Intent.html?hl=ja#FLAG_ACTIVITY_NEW_TASK

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

so solution is

  1. clear top when applicationContext's startActivity
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK

or

  1. if current some Activity in the front, get that context, and startActivity.
    you can get Activity Context with registerActivityLifecycleCallbacks.

App.kt

    override fun onCreate() {
        super.onCreate()
        setupActivityListener() 
    }

private fun setupActivityListener() {
        registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
            override fun onActivityStarted(activity: Activity) {}
            override fun onActivityResumed(activity: Activity) {
                activeActivity = activity
            }

            override fun onActivityPaused(activity: Activity) {
                activeActivity = null
            }

            override fun onActivityStopped(activity: Activity) {}
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {}
            override fun onActivityDestroyed(activity: Activity) {}
        })
    }
@jkasten2
Copy link
Member

jkasten2 commented Nov 9, 2018

@cuichanghao Thanks for the suggestion, we will look into using an Activity, if available to resume the app on the last activity instead bring / launch the main Activity. However this won't be the right behavior for every app or notification. To control the behavior you can add the following <meta-data> tag to prevent the OneSignal SDK from opening any actives, so you can open your own from OneSignal's NotificationOpenedHandler.

<application ...>
   <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>

https://documentation.onesignal.com/docs/android-customizations#section-changing-the-open-action-of-a-notification

@rgomezp
Copy link
Contributor

rgomezp commented Apr 3, 2019

Closing issue. Note: request still under consideration

@rgomezp rgomezp closed this as completed Apr 3, 2019
@aniketbhoite
Copy link

Facing the same behavior. Is there any workaround to it? And When should we expect to be added?

@rgomezp
Copy link
Contributor

rgomezp commented Jun 12, 2019

Hello,
Take a look at jkasten2's response above.
Best,
Rodrigo

@jkasten2
Copy link
Member

jkasten2 commented Feb 5, 2021

We are addressing this issue in PRs #1276 and #1277. In summary the OneSignal default notification open behavior will now always resume your app and won't affect your backstace. By "resume" it means the exact behavior an Android Launcher does, bring the last Activity the user had open in your app.

If you need more details on the resume behavior I recommend reading PR #1277 as it will most likely cover your question on it but feel free to reply here or open a new more specific issue.

This will be rolled into the next update which will most likely be 4.1.1 but it could be 4.2.0.

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

No branches or pull requests

4 participants