Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Install Referrer with Unity

Discussion in 'Android' started by Stelette, Sep 23, 2019.

  1. Stelette

    Stelette

    Joined:
    Nov 3, 2017
    Posts:
    17
    Hello, tell me how you can get install referrer in Unity3D . I made an android plugin in which I created a class inherited from broadcastreceiver . And in the onreceive method I send data, but the onreceive method is not called. Receiver has been added to the plugin Manifest and Unity manifest.Manifest:

    <receiver
    android:name="com.ahg.and.InstallReferrerReceiver"
    android:exported="true"
    android:permission="android.permission.INSTALL_PACKAGES">
    <intent-filter>
    <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
    </receiver>


    BroadcastReceiver:
    public class InstallReferrerReceiver extends BroadcastReceiver {
    [USER=530556]@override[/USER]
    public void onReceive(Context context, Intent intent) {
    String referrer = intent.getStringExtra("referrer");

    UnityPlayer.UnitySendMessage("Loader", "GetReferrer",referrer);
    }
    }

    Please tell me, for what reason is not called broadcastreceiver?
     
    Last edited: Sep 25, 2019
  2. Stelette

    Stelette

    Joined:
    Nov 3, 2017
    Posts:
    17
    Hello, I will write to those who are interested in this problem: in OnReceive you need to call non-unity methods ( since broadcastreceiver is called before the application starts) then I save string referrer through sharedpreferences , and then check it in unity at startup. I hope this helps someone.
     
    kmedved, lntoan123 and JeffDUnity3D like this.
  3. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15
    Hi @Stelette , i also stuck in this problem, I don't know how to receive Broadcast on c#. Did you have solution?
     
  4. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15
    OKay, I found solution for this problem.
     
  5. BigToe

    BigToe

    Joined:
    Nov 1, 2010
    Posts:
    204
    Would you mind sharing your solution?
     
    mahmoud93p likes this.
  6. lntoan123

    lntoan123

    Joined:
    Apr 1, 2019
    Posts:
    15


    First, you must know How to create library android native for unity. You can watch my link which i shared to your reference.
    After you understanded way to create library, You create library as following:

    package com.dzogame.installreferrer;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;

    import android.util.Log;

    import com.unity3d.player.UnityPlayer;


    public class InstallReferrerReceiver extends BroadcastReceiver {

    @override
    public void onReceive(Context context, Intent intent) {

    /************* Get referrer ************/
    String referrer = intent.getStringExtra("referrer");
    UnityPlayer.UnitySendMessage("DzoManager","SendReferer",referrer);
    Log.d("unity", "onReceive installReferrer native android: ");

    }

    }

    "DzoManager" is my Script which include "SendReferer" Method in Unity.
     
    BigToe likes this.
  7. Stelette

    Stelette

    Joined:
    Nov 3, 2017
    Posts:
    17
    Hello,I wrote the answer above,but I will write here in a little more detail.Calling UnityPlayer.UnitySendMessage will not work in this case, because OnReceive is called when installing,not launching the application (it turns out there is nowhere to send data,the application is not running).
    So I went the other way: Saving the value in SharedPreferences and reading it through PlayerPrefs when the game starts. Link to merge SharedPreferences and PlayerPrefs https://answers.unity.com/questions/1186147/playerprefs-and-sharedpreferences-in-android.html
    .Sorry that I didn't answer for a long time,I didn't visit for a very long time,and I didn't receive a notification about a message in this topic