Prevent network requests from Facebook Android SDK - android

The Android Facebook SDK is always making a network request to graph.facebook.com when calling FacebookSdk.sdkInitialize(context) even if nothing of the SDK has been used yet.
So if we're initializing it in the Application.onCreate() there will always be at least one network request. Even if the setting is as follows:
<meta-data
android:name="com.facebook.sdk.AutoLogAppEventsEnabled"
android:value="false" />
We've took a closer look at this because a user complained that he is not using the Facebook Login (which is why we have the Facebook SDK in the first place) and still there is "user data" transferred to Facebook. In times of GDPR and suspicious users, this is a very unfavourable behaviour!
What we're doing now is calling FacebookSdk.sdkInitialize(context) only when the user wants to use the Facebook Login (at the time when the user clicks on the button). Additionally, we removed the meta-data for android:name="com.facebook.sdk.ApplicationId" from the AndroidManifest. This prevents the initial network request but then there is the following crash appearing in the CurrentAccessTokenExpirationBroadcastReceiver:
java.lang.RuntimeException: Unable to start receiver com.facebook.CurrentAccessTokenExpirationBroadcastReceiver: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
at com.facebook.internal.Validate.sdkInitialized(Validate.java:143)
at com.facebook.FacebookSdk.getApplicationContext(FacebookSdk.java:518)
at com.facebook.AccessTokenManager.getInstance(AccessTokenManager.java:86)
at com.facebook.CurrentAccessTokenExpirationBroadcastReceiver.onReceive(CurrentAccessTokenExpirationBroadcastReceiver.java:34)
Now there are several questions:
Why is Facebook still making a request at start? If they want to validate the auth token, they can do it as soon as the SDK is really used...
Does Facebook know and tolerate crashes when sdkInitialize() is not called? Because I fear when this NullPointerException is removed there will be other crashes...
Most important: Are there any other ways to prevent network requests from the Facebook SDK when its features are not used?

I have noticed that the Facebook SDK (at least in version 4.33) adds a provider (com.facebook.internal.FacebookInitProvider) in the manifest of your app, which automatically calls FacebookSdk.sdkInitialize with the application context.
Even if you have added:
<meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="false" />
in your manifest, at least 2 requests will be made to Facebook:
a graph request to get app settings
an Events request "fb_sdk_initialize" which logs all the Facebook frameworks that are included into your app
So to prevent these requests (that we don't want as long as the user didn't allow them (GDPR)), I think you did everything we need to do:
Do not add <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/> in the manifest
Add <meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="false"/> in the manifest
Only initialize Facebook SDK when you need it.
Like this for example:
FacebookSdk.setApplicationId(<context>.getString(R.string.facebook_app_id));
FacebookSdk.sdkInitialize(<context>);
AppEventsLogger logger = AppEventsLogger.newLogger(<context>); // I don't use FB Login for my project but app events.
But regarding your crash, I don't know why it happens since it seems that the broadcast "com.facebook.sdk.ACTION_CURRENT_ACCESS_TOKEN_CHANGED" is sent locally.
Nevertheless, I think you can prevent it by adding this to your manifest:
<provider
android:name="com.facebook.internal.FacebookInitProvider"
tools:node="remove" />
<receiver
android:name="com.facebook.CurrentAccessTokenExpirationBroadcastReceiver"
tools:node="remove" />
However, doing this may have bad consequences with the use of Facebook SDK and I think you will have to provide (and register in your manifest) your own BroadcastReceiver:
public class CustomCurrentAccessTokenExpirationBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (AccessTokenManager.ACTION_CURRENT_ACCESS_TOKEN_CHANGED.equals(intent.getAction())) {
new CurrentAccessTokenExpirationBroadcastReceiver().onReceive(context, intent); // Call it only if you have initialized the Facebook SDK!
}
}
}

According to Facebook Support,
If you would like to temporarily disable automatic events collection, such as to get end-user consent before collecting data, you can set the value of AutoLogAppEventsEnabled to false in the app's AndroidManifest.xml in the application tag.
For example:
<meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="false"/>
To re-enable collection, such as after an end-user provides consent, call the setAutoLogAppEventsEnabled() method of the FacebookSDK class.
For example: setAutoLogAppEventsEnabled(true);
If you need to suspend collection again for any reason, you can call setAutoLogAppEventsEnabled(false); and collection will be suspended until re-enabling it.

Related

How can i call a content provider myself in the code?

I want to remove this from my androidmanifest file :
<provider android:name="com.facebook.internal.FacebookInitProvider"
android:authorities="%package%.FacebookInitProvider"
android:exported="false" />
because it's make Facebook network request every time my app is launch, even if the user will never use any Facebook api. Instead, I want to call myself com.facebook.internal.FacebookInitProvider when I will need to use a Facebook API. How can I do it?

How to opt out from Facebook SDK event logging?

I'm using Facebook App Events tracking for Android, but I have no idea how to disable tracking when a user wants to opt out! Does anyone know how to do that?
The documentation says:
We recommend that you use our SDK tools to offer the opt-out.
But they don't actually describe it anywhere. Classic Facebook move.
EDIT:
Now with the GDPR being in effect for so long, they still don't have any way to disable all tracking until the user consents, do they?
After the documentation was updated it seems the new way to go is a combination of disabling
<meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled"
android:value="false"/>
<meta-data android:name="com.facebook.sdk.AdvertiserIDCollectionEnabled"
android:value="false"/>
<meta-data android:name="com.facebook.sdk.AutoInitEnabled"
android:value="false"/> <!--OPTIONAL-->
And at runtime to opt in call:
FacebookSdk.setAutoLogAppEventsEnabled(true)
FacebookSdk.setAdvertiserIDCollectionEnabled(true)
// OPTIONALLY the following line if AutoInitEnabled is set to false in manifest:
FacebookSdk.setAutoInitEnabled(true) // automatically calls FacebookSdk.fullyInitialize() under the hood
Right now I don't see when it would be good to disable (and later enable) auto init, maybe someone else might have an idea here.
Don't know about per User, but you can opt put from Facebook SDK event logging:
Add this int your Android.Manifest.xml:
<meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled"
android:value="false"/>
The opt-out they are talking about is described in the heading below: https://developers.facebook.com/docs/app-events/android#setlimiteventusage
You can call AppEventsLogger.setLimitEventUsage(this, true); to "opt-out" (at least limit) how the events logged are used.

Disable Firebase crash reporting

I have decided to include the Firebase Crash API 9.0.1 into my Android app.
At the moment everything is working fine. Now I want to give my users the opportunity to disable, that Firebase is sending the Crash reports automatically.
Firebase Analytics can be disabled with this code snippet
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false);
Does anyone of you know a similar way to disable crash reporting?
Many thanks
Sorry for short answer but currently there is no official support for this.
EDIT: 30/10/2017
Now it is possible to enable/disable at build time adding in the AndroidManifest.xml:
<meta-data android:name="firebase_crash_collection_enabled" android:value="false" />
or runtime using:
FirebaseCrash.enableCrash(true|false);
More info here.
Yes it is possible now. Check this,
Disable Crash Reporting
Simply add the below line to your code in the first activity or even better in the Application class.
FirebaseCrash.setCrashCollectionEnabled(false);
To enable,
FirebaseCrash.setCrashCollectionEnabled(true);
This is really helpful when we have multiple build types such as Debug, Release etc.
You can find the instructions on the page:
https://firebase.google.com/support/guides/disable-analytics
Disable Analytics collection on Android
Temporarily disable collection
If you wish to temporarily disable Analytics collection, such as to get end-user consent before collecting data, you can set the value of firebase_analytics_collection_enabled to false in your app's AndroidManifest.xml in the application tag. For example:
<meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
To re-enable collection, such as after an end-user provides consent, call the setAnalyticsCollectionEnabled() method of the FirebaseAnalytics class. For example:
setAnalyticsCollectionEnabled(true);
If you need to suspend collection again for any reason, you can call
setAnalyticsCollectionEnabled(false);
and collection is suspended until you re-enable it.
Permanently deactivate collection
If you need to deactivate Analytics collection permanently in a version of your app, set firebase_analytics_collection_deactivated to true in your app's AndroidManifest.xml in the application tag. For example:
<meta-data android:name="firebase_analytics_collection_deactivated" android:value="true" />

Firebase Cloud Messaging not calling FirebaseInstanceId

I just updated GCM to FCM according to this guide here. The site says my service which extends FirebaseInstanceIdService will be called as soon as the token is generated. For some reason, my service is never called and onTokenRefresh is never executed.
My services are registered like this, according to the guide:
<service android:name=".fcm.FcmService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".fcm.InstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCEID_EVENT"/>
</intent-filter>
</service>
My service:
public class InstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "InstanceIdService";
#Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Got token: " + token);
}
}
The LogCat doesn't say anything about missing configuration files or else, and I can see logs about FirebaseApp and a Log entry mentioning the token, but still my service is never called.
Do I need to start this service somewhere in my activity?
Am I doing something wrong? Can someone guide me in the correct direction?
EDIT: using Log.e("TOKEN", "Token: " + FirebaseInstanceId.getInstance().getToken()); correctly prints my token, so that means it was generated successfully. The service still doesn't get called...
EDIT 2: Changing INSTANCEID_EVENT to INSTANCE_ID_EVENT fixed the issue, although I had to reinstall the app. As I already released a beta version containing the new FCM, my beta testers won't be able to subscribe to the topics to reveice incoming messages. So how should I solve that?
Thanks Endanke for figuring this out with me. It seems that the Firebase documentation is inconsistent. One page says your intent filter in your manifest file should be INSTANCE_ID_EVENT, and another says INSTANCEID_EVENT.
You should use INSTANCE_ID_EVENT, and once you make the change, you'll need to completely uninstall and reinstall your app for the changes to kick in.
FCM should be used as a replacement to GCM. In your case it looks like you have services for both FCM and GCM in your manifest. Try remove the gcm.GcmService, and only use the one that extends FirebaseInstanceId.
Make Sure that your Firebase "MyFirebaseInstanceIDService.java" file is directly under the package name tree "com.company"
Don't put them in the separate subpackage like "com.company.services". This is because your google-services.json will look for the files directly under "com.company" directory.
Add the following in your manifest application tag below meta tag (if any)
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
You will get your TOKEN on the public void onTokenRefresh() method
PS: Download and replace the latest google-services.json from your project console.
After a ton of trial and error my issue was having tools:node="replace" in my application tag...removing it solved the issue for me
I had the same problem and by uninstalling and reinstalling the app on the device onTokenRefresh() got called once. So I guess it changed from GCM where it got called often to just getting called once when there actually is no token.
I've different build variants in my project and I've the following problem:
From: https://firebase.google.com/docs/android/setup#prerequisites
Note: If you have multiple build variants with different package names defined, each app must be added to your project in Firebase console.
Also, check the date of your phone! I once had a difference of 5-7 days! Fixing the day and reinstalling the app fixed this!
What 'Firebase Cloud Messaging' wants is:
Implement a derived class (MyFirebaseInstanceIdService) from
FirebaseInstanceIdService.
Implement the override onTokenRefresh to handle an update on the
token for the device.
In the AndroidManifest.xml reference the 'derived' class prefixed
with a ".".
MyFirebaseInstanceIdService is a 'suggested' name. I personally prefer to prefix such derived classes with an acronym or name that I use throughout the App.
onTokenRefresh() will call only one time.when your app is install first time, if you want to access token from other activity you have to call this method:
FirebaseInstanceId.getInstanceId().getToken();
just change the INSTANCE_ID_EVENT to INSTANCEID_EVENT .it worked for me

Fiksu SDK Integration

I am trying to implement Fiksu SDK for Android and iOS.
I am able to add code to track registration and purchase :
FiksuTrackingManager.uploadRegistration(Context, FiksuTrackingManager.RegistrationEvent.EVENT1);
FiksuTrackingManager.uploadPurchase(Context, FiksuTrackingManager.PurchaseEvent.EVENT1, 0, "USD");
In iOS i am able to get the logs in the console, but in android i am only getting logs which event is called.How can i check the logs in Android ?
Also how to track the app events in Fiksu dashboard ?
They have also mentioned to add client id , from where will i get the client id ?
if any one has implemented it please guide me how to proceed ?
this might be too late but it may help someone that comes across this post.
I just had to integrate the Fiksu SDK for android for a client and face similar issues when testing.
How can i check the logs in Android ?
If the SDK has been properly installed you should be able to see some logs like below which means the events have been succesfully sent to their server.
11-25 10:04:31.010 21588-23132/ D/FiksuTracking? Event: Resume
11-25 10:04:32.320 21588-23132/ D/FiksuTracking? Successfully uploaded tracking information.
11-25 10:05:40.500 21588-23383/ D/FiksuTracking? Event: Purchase
11-25 10:05:42.180 21588-23383/ D/FiksuTracking? Successfully uploaded tracking information.
how to track the app events in Fiksu dashboard ?
This part is quite annoying as they don't have a way to create Apps in the dashboard and link them to your android app so you can see your events coming through. Instead you have to actually perform the test and send them an email for someone from Fiksu to check their servers and track your events.
These are the test steps as per their documentation:
Download and install your application on an Android device and perform your tests.
Fill a form they have and send it via email to their support team.
Someone will get back to you confirming if they have received your events.
They have also mentioned to add client id , from where will i get the client id ?
The clientId is just an identifier for your users within your app. In other words you can set that to whatever you want (e.i. email address). The suggestion is to set this after a user has registered or logged in in your app.
FiksuTrackingManager.setClientId(Context, "XXX");
Based on their documentation: "Where XXX would be the ID you want to send and can be any alphanumeric string up to 64 characters long. All events sent after this call is made would send the Client ID that has been set. If you call setClientID again it will overwrite whatever was there before. However the first Client ID sent with an event will be the Client ID that is in the export on the Fiksu Client Dashboard and it will not get updated."
if any one has implemented it please guide me how to proceed ?
So to sum up below are the steps to integrate the Fiksu SDK for android.
Downlaod the SDK.
Add the SDK jar to your project.
Set the isntall receiver (Only if you want to track app installs). NOTE that they expect to be the only receiver in the AndroidManifest.xml file. If you are using other third party SDKs to track the same you either:
Use fiksu to forward the intent:
<receiver android:name="com.fiksu.asotracking.InstallTracking"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
<!-- Forward to other receivers -->
<meta-data android:name="forward.1"
android:value="com.google.android.apps.analytics.AnalyticsReceiver" />
<meta-data android:name="forward.2" android:value="com.mypackage.thirdparty.MdotmReceiver" />
</receiver>
Or set the below flag in your AndroidManifest and have another install receiver forward it to the fiksu SDK.
<meta-data android:name="FiksuDisableReceiverCheck" android:value="true" />
Set the permissions in your AndroidManifest.
android.permission.INTERNET
android.permission.READ_PHONE_STATE
Initialize the SDK. Normally on your Application onCreate() method.
FiksuTrackingManager.initialize(this);
Setting up Google Play Services on your project.

Categories

Resources