How to automatically open app when receive push notification?

后端 未结 3 622
长发绾君心
长发绾君心 2021-01-13 19:22

I want to automatically open app when receive push notification. I\'ve tried but it still does not work as I expected. This code below is work when the app is active or in M

3条回答
  •  离开以前
    2021-01-13 19:56

    Firstly, the concept of "application" in Android is slightly an extended one.

    An application - technically a process - can have multiple activities, services, content providers and/or broadcast listeners. If at least one of them is running, the application is up and running (the process).

    So, what you have to identify is how do you want to "start the application".

    Ok... here's what you can try out:

    1. Create an intent with action=MAIN and category=LAUNCHER
    2. Get the PackageManager from the current context using context.getPackageManager
    3. packageManager.queryIntentActivity(, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(, 0) to get the first activity with main/launcher
    4. Get the ActivityInfo you're interested in
    5. From the ActivityInfo, get the packageName and name
    6. Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    7. Finally, context.startActivity(newIntent)

提交回复
热议问题