Twitter have some awesome tools for saving developers time and boosting productivity. One such tool is Twitter Fabric. Fabric is a collection of tools like Crashlytics (Crash Reporting), Mobile Analytics, Digits and much more.
Sadly, their official SDK’s are not yet available for Xamarin, still, there is a Xamarin binding which works well. Thanks to Michel Moreira for this binding.
This binding is available as Nuget Package, so getting started with this plugin is so easy.
Steps to integrate Digits to your Xamarin.Android app.
Step 1: Get a Fabric Account (https://fabric.io)
Step 2: Sadly, for getting API Keys, you need Android studio. So make sure you have Android Studio Installed (just for the sake of API keys).
Step 3: In Android Studio, select Plugins-> Browse Repositories -> Search for Fabric Plugin, Install it, Restart the IDE.
Step 4: Start a Blank New Project with the name Intended for your Xamarin.Android project.
Step 5: Click on Fabric Icon in Android Studio, Login to Fabric with your account and select Digits from the kits listed.
Step 6: Create Digits Account, and get the API keys obtained from AndroidManifest.xml file and MainActivity.java.
Step 7: Build the project in Android Studio. Once the app is built from Android Studio, your app gets listed in Fabric Dashboard where you can customize the One Time Password message, and get all they API keys.
Step 8: Enough with Android Studio (make sure you copy the keys from AndroidManifest.xml and MainActivity.java
AndroidManifest.xml
<meta-data
android:name=”io.fabric.ApiKey”
android:value=”your-key-here”
/>
MainActivity.java
1 2 |
private static final String TWITTER_KEY = "your-key-here"; private static final String TWITTER_SECRET = "your-key-here"; |
Step 9: Back to our awesome Xamarin, either in Visual Studio or Xamarin Studio. (I use VS)
Step 10: Open your project, Right Click your project in Solution Explorer and Select Manage Nuget Package.
Step 11: Search for Digits by Michel Moreira, and Install the package.
Step 12: Open your activity, in which you want Digits Authentication (in my case MainActivity.cs)
Step 13: Paste the TWITTER_KEY and TWITTER_SECRET TO your MainActivity.cs
1 2 3 |
// Note: Your consumer key and secret should be obfuscated in your source code before shipping. private static string TWITTER_KEY = "your-key-here"; private static string TWITTER_SECRET = "your-key-here"; |
Also, paste the meta data information to AndroidManifest.xml under application node.
<application android:label=”Fabric_Demo”>
<meta-data
android:name=”io.fabric.ApiKey”
android:value=”your-key-here”
/>
</application>
Step 14: Initialize fabric in your OnCreate method.
1 2 |
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.With(this, new TwitterCore(authConfig), new Digits()); |
Step 15: Inherit your MainActivity from IAuthCallBack and implement the interface. (Which will add two methods namely Success and Failure to your Activity)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MainActivity : Activity, IAuthCallback { public void Failure(DigitsException p0) { throw new NotImplementedException(); } public void Success(DigitsSession p0, string p1) { throw new NotImplementedException(); } } |
Step 16: There are multiple ways to authenticate. You can theme your default button or have custom Authentication action from anywhere in the program.
Step 17: For example, you need to authenticate mobile number on a button click, just have Digits.Authenticate(this); in your button click event.
Step 18: If you want to have digits button, add
<com.digits.sdk.android.DigitsAuthButton
android:id=”@+id/auth_button”
android:layout_width=”match_parent”
android:layout_height=”wrap_content” />
in your layout file.
and in your Main Activity, set the callback of the button to the current class.
1 2 3 |
DigitsAuthButton digitsButton = (DigitsAuthButton)FindViewById(Resource.Id.auth_button); digitsButton.Text = "Hey Authenticate Me"; digitsButton.SetCallback(this); |
Step 19: Handle Success and Failure methods. Success method will be called when your authentication using mobile number if verified and it will have the details in DigitsSession p0 variable or p1 gives the mobile number. Failure method will be called if the number cannot be authenticated or User cancels the process.
1 2 3 4 5 6 7 8 9 |
public void Failure(DigitsException p0) { Toast.MakeText(this, "Unable to verify you!", ToastLength.Long).Show(); } public void Success(DigitsSession p0, string p1) { Toast.MakeText(this, "Number " + p0.PhoneNumber + " verified", ToastLength.Long).Show(); } |
Step 20: Run and see it in action.
Skip Step 1 to 10 by requesting Twitter Fabrics team a Xamairn plugin 🙂 https://twitter.com/fabric
Shout out your suggestions, feedback and doubts in comment section below.
The Fabric Demo Solution can be downloaded from my Github
Credits: Michel Moreira‘s Fabric Binding for Xamarin.
Awesome post. Step-by-step detailing really helps.
Have this issue:
Duplicate Java type found! Mappings between managed types and Java types must be unique. First Type: ‘IO.Fabric.Sdk.Android.Services.Events.IEventsStorageListenerImplementor, Fabric, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’; Second Type: ‘Bindings.FabricSdk.Services.Events.IEventsStorageListenerImplementor, FabricSdk.Platform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ Fabric Demo
I guess you solved the issue 🙂
I’m getting deployment error when I use AppCompatActivity instead Activity. My question is Why I can’t use the AppCompatActivity.?
You must be able to use, Could you provide me the error you are getting
https://github.com/muhaym/Fabric.Sdk.Xamarin/issues
Hello I’m having an issue with Application Theming.
Could you explain more, Thanks
Hello, im using xamarin digits in my projet, one error is appear when invoke new Digits
Fabric.With(this, new TwitterCore(authConfig), new Digits());
/Users/jefferson/Projects/investeapp31/investeapp31/MainActivity.cs(60,60): Error CS1540: Cannot access protected member Bindings.DigitsKit.Digits.Digits(System.IntPtr, Android.Runtime.JniHandleOwnership)’ via a qualifier of typeBindings.DigitsKit.Digits’. The qualifier must be of type `investeapp31.MainActivity’ or derived from it (CS1540) (investeapp31)
Could you please share more details like Code and error through
https://github.com/muhaym/Fabric.Sdk.Xamarin/issues
Also please make sure you update to latest Nuget Package
This is My Code and following will be the Errors:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.With(this, new TwitterCore(authConfig), new Digits());
// Set our view from the “main” layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
DigitsAuthButton digitsButton = (DigitsAuthButton)FindViewById(Resource.Id.auth_button);
digitsButton.Text = “Hey Authenticate Me”;
digitsButton.SetCallback(this);
digitsButton.Click += delegate {
Digits.Authenticate(this);
};
}
The errors I get is.
Error:
1. ‘Digits’ does not contain a constructor that takes 0 arguments
2. Argument 1: cannot convert from ‘BlankApp1.Droid.MainActivity’ to ‘Bindings.DigitsKit.AuthConfig’ BlankApp1.Droid
Can you paste the whole page code in gist?
Or refer the sample code.
Is the nuget properly installed?
hi hello dear im using your way for fabric.Digits (xamarin.android) >
in debug mode enything is ok and i recieved confirm sms messages
but
in relse mode at enter phone number screen after click send code tooltipe that try agine.
i cont find probelm
just in relase mode not work
plase help me and send answer to my email
i.entersoft@gmail.com
mehdi golzari
thanks for time
i cant use following after added Digits
Fabric.With(this, new TwitterCore(authConfig), new DigitsKit.Digits());
And using Bindings.FabricSdk is not avail Digits after install from
ManageNugetPackage
I tried but fabric.sdk could not able to add.
This article is outdated.