Read Android intent extra data on Unity app launch

Viewed 10219

I am launching an Unity application from another Android application using a custom implicit intent. This is working fine, but I cannot figure out how to read the intent extra data in Unity?

ANDROID INTENT TO LAUNCH UNITY APP

i=new Intent();
i.setAction("com.company.unityapp.MyMethod");
i.putExtra("KEY","This is the message string");
startActivity(i);

UNITY APP AndroidManifest.xml

<intent-filter>
     <action android:name="com.company.unityapp.MyMethod" />
     <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

I have a GameObject in my scene with a script attached. Inside the start method I have this code to try and read the extra data that was passed along with the intent

AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
bool hasExtra = intent.Call<bool> ("hasExtra", "arguments");

if (hasExtra) {
 AndroidJavaObject extras = intent.Call<AndroidJavaObject> ("getExtras");
 arguments = extras.Call<string> ("getString", "arguments");
}

This is not working and arguments is always empty. Any help would be appreciated.

2 Answers

You don't need a plugin to achieve this. Do your intent from Android like this:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.game");
launchIntent.putExtra("my_text", "Some data params");
if(launchIntent != null){
    startActivity(launchIntent);
}else{
    Log.d("Unity", "Couldnt start unity game");
}

Then in your unity Monobehaviour Class, receive it like this

private void Awake () {
    getIntentData ();
}

private bool getIntentData () {
#if (!UNITY_EDITOR && UNITY_ANDROID)
    return CreatePushClass (new AndroidJavaClass ("com.unity3d.player.UnityPlayer"));
#endif
    return false;
}

public bool CreatePushClass (AndroidJavaClass UnityPlayer) {
#if UNITY_ANDROID
    AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
    AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject> ("getIntent");
    AndroidJavaObject extras = GetExtras (intent);

    if (extras != null) {
        string ex = GetProperty (extras, "my_text");
        return true;
    }
#endif
    return false;
}

private AndroidJavaObject GetExtras (AndroidJavaObject intent) {
    AndroidJavaObject extras = null;

    try {
        extras = intent.Call<AndroidJavaObject> ("getExtras");
    } catch (Exception e) {
        Debug.Log (e.Message);
    }

    return extras;
}

private string GetProperty (AndroidJavaObject extras, string name) {
    string s = string.Empty;

    try {
        s = extras.Call<string> ("getString", name);
    } catch (Exception e) {
        Debug.Log (e.Message);
    }

    return s;
}

Credit: https://wenrongdev.com/get-android-intent-data-for-unity/

(updated) https://wenrongdev.com/posts/get-android-intent-data-for-unity/

Related