Unable to get Android Advertising ID - android

I'm experimenting with getting the Android Advertising Id, but I can't find the proper way. In fact, I can't even get the Advertising Id Provider.
isAdvertisingIdProviderAvailable( ) always returns false. I'm using a Samsung with 8.0 + PlayStore and also on the emulator with 8.1 + Google Play, running as a debug build.
I've followed this guide:
https://developer.android.com/training/articles/ad-id
This must be something simple, but I can't see it.
Thanks for any suggestions.
I created a blank project and this is my code:
public class MainActivity extends AppCompatActivity {
public static final String TAG="MYTAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isProvider = AdvertisingIdClient.isAdvertisingIdProviderAvailable(getApplicationContext());
Log.i(TAG, "isProviderAvailable:" + isProvider);
}
}
Gradle:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "company.com.adidviewer"
minSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.gms:play-services-ads:18.3.0'
implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
//implementation 'androidx.ads:ads-identifier-common:1.0.0-alpha04'
//implementation 'androidx.ads:ads-identifier-provider:1.0.0-alpha04'
// Used for the calls to addCallback() in the snippets on this page.
//implementation 'com.google.guava:guava:28.0-android'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
Manifest
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
</application>

If you want to get the advertising id for your published app, use the following approach:
Add this line to your gradle file;
com.google.android.gms:play-services-ads-identifier:16.0.0
And the code like this;
private void determineAdvertisingInfo(Context context) {
AsyncTask.execute(new Runnable() {
#Override
public void run() {
try {
AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
// You should check this in case the user disabled it from settings
if (!advertisingIdInfo.isLimitAdTrackingEnabled()) {
String id = advertisingIdInfo.getId();
// getUserAttributes(id);
} else {
//If you stored the id you should remove it
}
} catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
}
});
}
There is a note specifying this in the document as well:
Note

Related

.addOnCompleteListener not working for FirebaseInstanceId.getInstance().getInstanceId()

I am new to android development. Before posting this question, I have checked if same issue had been posted and had a solution. I couldn't find and so posting here.
I am trying to get FCM token and using the below piece of code.
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(#NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
// Log and toast
Log.d(TAG, token);
}
});
I am not getting any error. However method onComplete is not getting executed.
Could please someone help me on this. I have been trying this for couple of days.
Please note that, as per few suggestions on SOF, I followed .on SuccessfullListener also. But the same issue with that as well.
I am using the below things.
Manifest:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
App level Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.abc.xyz"
minSdkVersion 21
multiDexEnabled true
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.google.firebase:firebase-messaging:17.5.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-auth:16.2.0'
implementation 'com.google.firebase:firebase-firestore:18.2.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.firebase:firebase-database:16.1.0'
}
apply plugin: 'com.google.gms.google-services'
Project Gradle:
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
addOnCompleteListener runs on the background thread so it's not clear when it will be done.
I advice using onNewToken(String token) in your custom FirebaseMessagingService :
public class FireBaseCustomReciver extends FirebaseMessagingService {
private static final String TAG = "aaa";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
}
#Override
public void onNewToken(String token) {
Log.e("aaa", "Refreshed token: " + token);
}
}

missing binaries for apk to install on device

I have looked at this article
[INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113]
But i was unable to get any resolution.
right now this is my code.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.example.testapp.testapp"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
splits {
abi {
enable true
reset()
include 'arm64-v8a'
universalApk true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation('me.dm7.barcodescanner:zxing:1.9') { exclude module: 'support-v4' }
implementation 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
implementation 'org.bitcoinj:bitcoinj-core:0.14.7'
api 'org.slf4j:slf4j-api:1.7.25'
implementation 'org.slf4j:slf4j-simple:1.7.12'
}
And also my mainactivity
public class MainPage extends AppCompatActivity {
public static NetworkParameters BTCparams = TestNet3Params.get();
public static WalletAppKit BTCkit = new WalletAppKit(BTCparams,new File("."),"BTC-Test");
static String[] account;
static String[] server;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tVBTCPer = (TextView)findViewById(R.id.tVBTCPer);
TextView tVNull = (TextView)findViewById(R.id.tVNull);
tVNull.setText(System.getProperty("os.arch"));
DownloadProgressTracker BTCListener = new DownloadProgressTracker() {
#Override
public void progress(double pct, int blocksSoFar, Date date) {
tVBTCPer.setText((int) pct+"%");
}
#Override
public void doneDownload() {
tVBTCPer.setText("100%");
}
};
BTCkit.setDownloadListener(BTCListener).setBlockingStartup(false).startAsync().awaitRunning();
}
I used this library to make java applications for pc/linux and it works fine. But when im trying to compile this for android to be usable it gets hung up trying to install the apk onto my device.
my os.arch is aarch64. I also have a previous post in stackoverflow to help give it some context.
Bitcoinj library on android hung on installing APK
I am not entirely sure what library i would need to help this apk install onto my device.

Unresolved class for firebase ".MyFirebaseInstanceIDService" and ".java.MyFirebaseMessagingService"

I'm trying to import Firebase and FCM into my android app, but I'm getting
Unresolved class for firebase ".MyFirebaseInstanceIDService"
Unresolved package for firebase ".java.MyFirebaseMessagingService"
as an "error." Of course, it isn't stopping my app from running or building, but I cannot call onTokenRefresh in any class at all. So, I believe this problem is preventing me from using Firebase.
here is my app build;
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.northlandcaps.crisis_response"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:mediarouter-v7:28.0.0'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
}
apply plugin: 'com.google.gms.google-services'
Screenshot of problem
This how you two classes should look like:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
#Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
And:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
sendNotification(notification, data);
}
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
//Send notification
}
}

Issue in Integration GTM (Google Tag Manager ) android

i want to implement Google Tag Manager in android app so i set followwing code
public class MainActivity extends Activity {
private static final String CONTAINER_ID = "GTM-YYYY";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TagManager tagManager = TagManager.getInstance(this);
tagManager.setVerboseLoggingEnabled(true);
PendingResult<ContainerHolder> pending =
tagManager.loadContainerPreferNonDefault(CONTAINER_ID,
R.raw.gtm_default_container);
pending.setResultCallback(new ResultCallback<ContainerHolder>() {
#Override
public void onResult(ContainerHolder containerHolder) {
ContainerHolderSingleton.setContainerHolder(containerHolder);
Container container = containerHolder.getContainer();
if (!containerHolder.getStatus().isSuccess()) {
Log.e("CuteAnimals", "failure loading container");
displayErrorToUser(R.string.load_error);
return;
}
ContainerHolderSingleton.setContainerHolder(containerHolder);
ContainerLoadedCallback.registerCallbacksForContainer(container);
containerHolder.setContainerAvailableListener(new ContainerLoadedCallback());
startMainActivity();
}
}, 2, TimeUnit.SECONDS);
}
}
i also set google play library in my gradle file as below
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "pkg.android.demo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services:8.1.0'
}
When i put above code ContainerLoadedCallback clas not found as below image
Any idea how can i solve this?
This methods are from example. You have to implement them by yourself if you want.
You can also comment // them and it will work fine. Remove lines with errors.
This classes are shown here to show what can you do with your container.
You can find these classes in the cuteanimal sample. Go to your directory where sdk is located..... /extras/google/google_play_services/tagmanager/cuteanimals

ChannelAPI can not be resolved

I am trying to exchange files between watch and handheld by using the channel API.
But when I tried to open the channel by calling the following code, the ChannelAPI can not be resolved . Its not part of the "wearable" class I am using.
I guess there probably was caused by the wrong Android version. Does anyone know which version should I specify in my config files or how to fix it?
Thanks a lot.
My wear bundle file looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "mywearapp"
minSdkVersion 20
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize "4096m"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.support:wearable:1.1.0'
compile 'com.google.android.gms:play-services-wearable:7.8.0'
compile project(':shared')
}
Wearable.ChannelApi.openChannel(
mGoogleApiClient, node.getId(), "/mypath").setResultCallback(
new ResultCallback<ChannelApi.OpenChannelResult>() {
#Override
public void onResult(ChannelApi.OpenChannelResult openChannelResult) {
if (openChannelResult.getStatus().isSuccess()) {
mChannel = openChannelResult.getChannel();
mChannel.getOutputStream(mGoogleApiClient).setResultCallback(
new ResultCallback<Channel.GetOutputStreamResult>() {
#Override
public void onResult(Channel.GetOutputStreamResult getOutputStreamResult) {
if (getOutputStreamResult.getStatus().isSuccess()) {
mOutputStream = getOutputStreamResult.getOutputStream();
} else {
// handle failure, and close channel
}
}
});
}
}
});

Categories

Resources