Last active
February 13, 2020 08:08
-
-
Save liorkup/93bcfb5a5ea1558e8fc9fefd22760392 to your computer and use it in GitHub Desktop.
InstanceId solution for LadyM (License: https://gist.github.com/liorkup/019cec4f23edfe39a444571206ea2ae7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//InstanceId solution | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if (prefs.getBoolean("firstrun", true)) { | |
// Do first run stuff here then set 'firstrun' as false | |
// using the following line to edit/commit prefs | |
prefs.edit().putBoolean("firstrun", false).commit(); | |
@SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() { | |
@Override | |
protected String doInBackground(Void... params) { | |
AdvertisingIdClient.Info idInfo = null; | |
try { | |
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext()); | |
} catch (GooglePlayServicesNotAvailableException e) { | |
e.printStackTrace(); | |
} catch (GooglePlayServicesRepairableException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
String advertId = null; | |
try{ | |
storeIdInServer(mFirebaseAnalytics.getFirebaseInstanceId(), idInfo.getId()); | |
}catch (NullPointerException e){ | |
e.printStackTrace(); | |
} | |
return advertId; | |
} | |
private void storeIdInServer(String firebaseInstanceId, String advertisingId) { | |
//todo | |
System.out.println( String.format("InstanceId: %s; AdvertisingId: %s", mFirebaseAnalytics.getFirebaseInstanceId(), advertisingId)); | |
} | |
@Override | |
protected void onPostExecute(String advertId) { | |
Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
task.execute(); | |
} | |
} | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment