4

How can I know that my app was opened by Google Assistant, instead of just normally launched. I don't need App Actions. I just want to know, that yes, my app was opened with "Ok Google -> Open appname" instead of pressing on the icon, or resuming it from recents. If there an intent/any data in the bundle that I can check for that?

This is my intent when I do "Open appname"

Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.xelion.android cmp=com.xelion.android/.activity.InitializationActivity (has extras) }

And it has extras, but don't know what:

Bundle[mParcelledData.dataSize=220]

EDIT:

I found out that this will be the flag for opening with google Assistant:

intent.flags == 0x10000000

But my problem is that this also will run when I build the app from machine or update it, Any idea how to avoid that?

EDIT2:

I have also tried:

private fun getReferrerCompatible(activity: Activity): Uri? {
    val intent = activity.intent
    val referrerUri: Uri? = intent.getParcelableExtra(Intent.EXTRA_REFERRER)
    if (referrerUri != null) {
        return referrerUri
    }
    val referrer = intent.getStringExtra(REFERRER_NAME)
    if (referrer != null) {
        // Try parsing the referrer URL; if it's invalid, return null
        try {
            return Uri.parse(referrer)
        } catch (e: ParseException) {
            return null
        }

    }
    return null
}

But I still get NULL as referrer

I am trying the : intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT or getReferrerCompatible() from the onCreate. Should it be later? like onResume?

rosu alin
  • 5,674
  • 11
  • 69
  • 150

2 Answers2

1

When opened through Google Assistant, the android.intent.extra.REFERRER_NAME will be android-app://com.google.android.googlequicksearchbox/https/www.google.com

val KEY_REF_NAME = "android.intent.extra.REFERRER_NAME"
val REG_G_ASSISTANT = "android-app://com.google.android.googlequicksearchbox/https/www.google.com"

if (intent.extras?.get(KEY_REF_NAME) == REG_G_ASSISTANT) {
    // APP OPENED THROUGH GOOGLE ASSISTANT
} else {
    // APP OPENED THROUGH DEFAULT LAUNCHER
}

theapache64
  • 10,926
  • 9
  • 65
  • 108
  • On Huawei Mate 10 Pro: `intent.extras.get("android.intent.extra.REFERRER_NAME")` returns NULL – rosu alin Nov 14 '19 at 12:28
  • 1
    I checked this with `OnePlus 6` and `Moto XStyle`, `Samsung Galaxy On8`.All of them return the same referrer name. Can you loop through the `intent.extras`, and check what all params are available in `Huawei` ? – theapache64 Nov 14 '19 at 12:29
  • intent is : activity.intent in the case of a fragment, for the code you written, correct? – rosu alin Nov 14 '19 at 12:31
  • yes , you're correct. Can you try with another device ? – theapache64 Nov 14 '19 at 12:32
  • Hhmm, weird, also on Pixel 3 I get null. I edited my code to include something else – rosu alin Nov 14 '19 at 12:48
  • Can you send a sample project with this issue to theapache64@gmail.com ? – theapache64 Nov 14 '19 at 12:51
  • Check this: https://github.com/allegro/slinger/blob/master/slinger/src/main/java/pl/allegro/android/slinger/ReferrerMangler.java The Lollypop version works. But it is `"android-app://com.google.android.googlequicksearchbox"` If you edit your answer to reflect and work with all API's, then I will accept it – rosu alin Nov 14 '19 at 13:00
0

Based on the response that theapache64 gave and this link: https://github.com/allegro/slinger/blob/master/slinger/src/main/java/pl/allegro/android/slinger/ReferrerMangler.java

Because the intent was returning null on Android 10, and due to my min SDK being 23 (I do not need to implement logic for under M), I have done the following code:

  val REG_G_ASSISTANT = "com.google.android.googlequicksearchbox"
  if (referrer != null && referrer.toString().contains(REG_G_ASSISTANT)) {
      //code to do
  }

This being Kotlin, and being in an activity. The equivalent of referrer in .java would be:

activity.getReferrer(); 

In case you run an OS under 23, the referrer can be taken like this:

val KEY_REF_NAME = "android.intent.extra.REFERRER_NAME"
intent.extras?.get(KEY_REF_NAME)

Being that theapache64 tried on a OnePlus6, I assume this should work until API level 28 (Pie) on some devices. But to be sure, I would recommend using the activity.getReferrer()

rosu alin
  • 5,674
  • 11
  • 69
  • 150