SlideShare a Scribd company logo
Are you looking for a realtime infrastructure
behind your mobile app?
Well, Firebase may be what you need..and even more!
DroidconIT2016 – Alessandro Martellucci
I’m Alessandro Martellucci
alex.martellucci@gmail.com
martellux
+AlessandroMartellucci
@martellux
DroidconIT2016 – Alessandro Martellucci
Open Reply
•  Reply is a leading IT Services Company, with offices in Italy, Germany, UK, Benelux,
USA, Brazil, France and Poland.
•  Open Reply is the company of Reply Group focused on open source software,
multichannel solutions and mobile applications.
•  From Rome to Milan, our team is based on a young team of over 40 engineers 100%
focused on mobile development (iOS, Android & Windows Phone).
•  We are specialised in broadcasting, banking, Android OS Customisation, IoT and
M2M.
We are hiring! Contact us at jobs@reply.eu
DroidconIT2016 – Alessandro Martellucci
Lifecycle 0.2
A binder which let you manage async operations against
Android components lifecycle
•  Seamless execution async-rotation-response
•  Easily integration with third-party library
•  No crashes after Activity/Fragment rotation
•  Fragment transaction management
martellux/Lifecycle com.martellux:lifecycle:0.2.0
DroidconIT2016 – Alessandro Martellucci
Android Programmazione Avanzata
•  Multi-screen and multi device development
•  Functional Programming with RxJava
•  Testing & Code Mantainance
•  Android Wear
•  Bluetooth Classic & Low Energy
•  Google Cast and Chromecast
DroidconIT2016 – Alessandro Martellucci
Agenda
1.  What is Firebase
2.  NoSQL JSON database
3.  Demo
4.  Android integration
5.  Working with data
6.  Working offline
7.  FirebaseUI binding
8.  Rules management
9.  User authentication
10. Pricing
DroidconIT2016 – Alessandro Martellucci
What is Firebase
•  MBaaS
•  Data synchronization
•  Hosting (static assets, Node.js)
•  Authentication (Google, Facebook, Email & Password, Anonymous)
•  Realtime database (JSON)
DroidconIT2016 – Alessandro Martellucci
NoSQL Database
•  SQL & RDBMS
•  JSON
•  web format
•  agile
•  structure
•  friendliness
•  Drawbacks
•  navigational query
•  structure
DroidconIT2016 – Alessandro Martellucci
Firebase JSON
{
“event-name”:”Droidcon Italy”,
“location”: {
“latitude”:22,
“longitude”:22
},
“events”: [
{“name”: “Are you looking for a realtime…”,
“speaker”: “Alessandro Martellucci”,
“room”: “Sala Parigi”,
“time”: 000000
},
…
]
}
JSON	
DroidconIT2016 – Alessandro Martellucci
Firebase Web Console
DroidconIT2016 – Alessandro Martellucci
Firebase Vulcan Console
DroidconIT2016 – Alessandro Martellucci
DroidconIT 2016 FirebaseDemo
•  We’re going live
1.  Download app
2.  Hit your colour
3.  Be patient
DroidconIT2016 – Alessandro Martellucci
Source code at https://github.com/martellux/
Android integration
•  Account
•  Sign with Google
•  Dependecies
•  Right click -> Project Structure -> Cloud -> Firebase
•  Reference
•  Navigation path
DroidconIT2016 – Alessandro Martellucci
Firebase setup (1/2)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.firebase:firebase-client-android:2.5.1+’
}
android {
packagingOptions {
exclude 'META-INF/LICENSE’
exclude 'META-INF/LICENSE-FIREBASE.txt’
exclude 'META-INF/NOTICE’
}
}
Configura.on	
DroidconIT2016 – Alessandro Martellucci
Firebase setup (2/2)
public class ApplicationInstance extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Applica.on	
DroidconIT2016 – Alessandro Martellucci
public class MainPresenter extends Presenter {
private Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”);
}
Presenter
Reading data
•  Callbacks
•  Read Type
•  Value
•  Child
•  Firebase Listener
•  ValueEventListener
•  ChildEventListener (added, removed, changed)
DroidconIT2016 – Alessandro Martellucci
Read value changes
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”);
mFirebaseRef.child(“data”).addValueEventListener(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot snapshot) {
Map<String, Object> value = snapshot.getValue(Map.class);
//or
MyBean myBean = snapshot.getValue(MyBean.class);
}
@Override public void onCancelled(FirebaseError firebaseError) {
Log.e(TAG, "The read failed: " + firebaseError.getMessage());
}
});
Value	Event	Listener	
DroidconIT2016 – Alessandro Martellucci
Read new child
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”);
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
MyBean myBean = snapshot.getValue(MyBean.class);
String foo = myBean.getFoo();
}
@Override public void onCancelled(FirebaseError firebaseError) {
Log.e(TAG, "The read failed: " + firebaseError.getMessage());
}
});
Child	Event	Listener	
DroidconIT2016 – Alessandro Martellucci
Read child updates
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”);
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override public void onChildChanged(DataSnapshot snapshot, String previousChildKey) {
MyBean myBean = snapshot.getValue(MyBean.class);
Log.d(TAG, ”Prop foo has been updated to: " + myBean.getFoo());
}
@Override public void onCancelled(FirebaseError firebaseError) {
Log.e(TAG, "The read failed: " + firebaseError.getMessage());
}
});
Child	Event	Listener	
DroidconIT2016 – Alessandro Martellucci
Read removed child
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”);
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override public void onChildRemoved(DataSnapshot snapshot) {
MyBean myBean = snapshot.getValue(MyBean.class);
System.out.println(”This entry has been removed: " + myBean.toString());
}
@Override public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
Child	Event	Listener	
DroidconIT2016 – Alessandro Martellucci
Saving Data
•  Non-concurrent
•  Insert
•  Update
•  Concurrent
•  IDs generation
•  Transaction
DroidconIT2016 – Alessandro Martellucci
Write or replace data (1/2)
{
…,
status: {
connectedUsers: 10,
...
},
...
}
Online	users	
DroidconIT2016 – Alessandro Martellucci
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”);
int connectedUsers = countConnectedUsers();
mFirebaseRef.child(“status/connectedUsers”).setValue(connectedUsers);
1)	Naviga.ng	by	subnodes
Write or replace data (2/2)
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”);
int connectedUsers = countConnectedUsers();
mFirebaseRef.child(“connectedUsers”).setValue(connectedUsers);
2)	Naviga.ng	direct	to	node	
DroidconIT2016 – Alessandro Martellucci
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”);
Map<String, String> statusMap = new HashMap<String, String>();
statusMap.put(“connectedUsers”, String.valueOf(countConnectedUsers());
mFirebaseRef.setValue(statusMap);
3)	Using	Map
Overwrite & delete
JSON	node	
DroidconIT2016 – Alessandro Martellucci
{ “status”:{
“connectedUsers”:1,
“startTime”:1456426772000,
“endTime”:1456426772000
}
}
{ “status”:{
“connectedUsers”:1
}
}
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”);
Map<String, String> statusMap = new HashMap<String, String>();
statusMap.put(“connectedUsers”, String.valueOf(countConnectedUsers());
mFirebaseRef.setValue(statusMap);
Java	Code
Update children
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”);
Map<String, String> statusMap = new HashMap<String, String>();
statusMap.put(“connectedUsers”, String.valueOf(countConnectedUsers());
mFirebaseRef.updateChildren(statusMap);
1)	Upda.ng	nodes	
DroidconIT2016 – Alessandro Martellucci
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”);
Map<String, String> statusMap = new HashMap<String, String>();
statusMap.put(“status/connectedUsers”, String.valueOf(countConnectedUsers());
mFirebaseRef.updateChildren(statusMap);
2)	Upda.ng	subnodes
Concurrent-indipendent insert
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”);
Firebase dataChildRef = mFirebaseRef.child(“data”);
Firebase newChildRef = dataChildRef.push();
Map<String, Object> colorMap = new HahsMap<String, Object>();
colorMap.put(“red”, “242”);
colorMap.put(“green”, “10”);
colorMap.put(“blue”, “22”);
newChildRef .setValue(colorMap);
Java	Code	
DroidconIT2016 – Alessandro Martellucci
Concurrent-dipendent insert
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”);
newChildRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData currentData) {
if((currentData.getValue() == null) {
currentData.setValue(1);
} else {
currentData.setValue((Long) currentData.getValue() + 1);
}
return Transaction.success(currentData);
}
@Override
public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) {
…
}
});
Java	Code	
DroidconIT2016 – Alessandro Martellucci
Working offline
•  Works offline
•  Seamless experience
•  Auto synchronization
DroidconIT2016 – Alessandro Martellucci
User authentication
DroidconIT2016 – Alessandro Martellucci
•  User identification
•  Email & password login
•  Easy integration
•  Facebook, GitHub, Google and Twitter
•  Session management
•  Single session
•  Custom token
FirebaseUI - Authentication
DroidconIT2016 – Alessandro Martellucci
1.  Add SDK dependencies
2.  Add Facebook/Twitter/Google keys to
strings.xml
3.  Change our AndroidManifest.xml
4.  Inherit from FirebaseLoginBaseActivity
5.  Enable authentication providers
6.  Call showFirebaseLoginDialog();
Facebook authentication
DroidconIT2016 – Alessandro Martellucci
Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”);
mFirebaseRef .addAuthStateListener(new Firebase.AuthStateListener() {
@Override
public void onAuthStateChanged(AuthData authData) {
if (authData != null) {
// user is logged in
Map<String, String> map = new HashMap<String, String>();
map.put("provider", authData.getProvider());
if(authData.getProviderData().containsKey("displayName")) {
map.put("displayName", authData.getProviderData().get("displayName").toString());
}
ref.child("users").child(authData.getUid()).setValue(map);
} else {
// user is not logged in
}
}
});
Authen.ca.on
Rules management (1/2)
DroidconIT2016 – Alessandro Martellucci
{
"rules": {
".read": true,
".write": true
}
}
Basic	rules	
•  JavaScript-like
•  Node permissions
•  Node validations
Rules management (2/2)
DroidconIT2016 – Alessandro Martellucci
{
“status”: {
“schedule”: {
“startTime”: 123456670292,
…
}
}
}
startTime	node	
{
"rules": {
"status": {
"schedule": {
"startTime": {
// readable
".read": true,
// data written must be a number
".validate": "newData.isNumber()”,
// write authorization
".write": "$user_id === auth.uid”
}}}}}
Rules	for	startTime	node
FirebaseUI
DroidconIT2016 – Alessandro Martellucci
•  Open source library
•  iOS support
•  Authentication
•  built-in dialog
•  List binding
•  ListView
•  RecyclerView
FirebaseUI – ListView
DroidconIT2016 – Alessandro Martellucci
@Override
protected void onCreate(Bundle savedInstanceState) {
…
ListView messagesView = (ListView) findViewById(R.id.messages_list);
mAdapter = new FirebaseListAdapter<MyBean>(this, MyBean.class,
R.layout.my_list_item, mFirebaseRef) {
@Override
protected void populateView(View view, MyBean myBean, int position) {
((TextView)view.findViewById(android.R.id.text1)).setText(myBean.getFoo());
}
};
messagesView.setAdapter(mAdapter);
}
Adapter
FirebaseUI – RecyclerView
DroidconIT2016 – Alessandro Martellucci
@Override
protected void onCreate(Bundle savedInstanceState) {
…
RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new FirebaseRecyclerAdapter<MyBean, MyBeanViewHolder>(MyBean.class, R.layout.my_line_list_item,
MyBeanViewHolder.class, mFirebaseRef) {
@Override
public void populateViewHolder(MyBeanViewHolder myBeanViewHolder, MyBean myBean, int position) {
myBeanViewHolder.nameText.setText(myBean.getFoo());
}
};
recycler.setAdapter(mAdapter);
}
Adapter
Pricing
DroidconIT2016 – Alessandro Martellucci
FREE	
(0$	forever)	
SPARK	
(5$	month)	
…	 INFERNO	
(1499$	month)	
DB	connec.ons	 100	 100	 …	 UNLIMITED	
DB	storage	 1	GB	 1	GB	 …	 300	GB	
DB	transfer	 10	GB	 10	GB	 …	 1.5	TB	
Private	Backups	 NO	 NO	 …	 YES	
Authen.ca.on	
UNLIMITED	
USERS	
UNLIMITED	
USERS	
…	
UNLIMITED	
USERS	
Hos.ng	Storage	 1	GB	 1	GB	 …	 10	GB	
Hos.ng	Transfer	 100	GB	 100	GB	 …	 1	TB	
Custom	Domain	 NO	 YES	 …	 YES
Resources
•  BLOG: https://www.firebase.com/blog
•  Twitter: @firebase
•  Facebook: Firebase
•  Google Plus: Firebase
•  Official: http://www.firebase.com
•  Github: https://github.com/firebase
•  YouTube: Firecast: Firebase Screencast
DroidconIT2016 – Alessandro Martellucci
THANK YOU ALL
DroidconIT2016 – Alessandro Martellucci

More Related Content

PDF
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Utah JUG...
PDF
Bootiful Development with Spring Boot and React - UberConf 2018
PDF
Reactive Java Microservices with Spring Boot and JHipster - Denver JUG 2021
PDF
Bootiful Development with Spring Boot and React - SpringOne 2017
PPT
Os Johnson
PDF
Bootiful Development with Spring Boot and React - Richmond JUG 2018
PDF
Hybrid Apps (Native + Web) via QtWebKit
PDF
Spring Boot APIs and Angular Apps: Get Hip with JHipster! KCDC 2019
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Utah JUG...
Bootiful Development with Spring Boot and React - UberConf 2018
Reactive Java Microservices with Spring Boot and JHipster - Denver JUG 2021
Bootiful Development with Spring Boot and React - SpringOne 2017
Os Johnson
Bootiful Development with Spring Boot and React - Richmond JUG 2018
Hybrid Apps (Native + Web) via QtWebKit
Spring Boot APIs and Angular Apps: Get Hip with JHipster! KCDC 2019

What's hot (20)

PDF
A friend in need - A JS indeed
PDF
Front End Development for Backend Developers - GIDS 2019
PDF
Mobile Development with Ionic, React Native, and JHipster - ACGNJ Java Users ...
PDF
Java REST API Framework Comparison - PWX 2021
PPT
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
PDF
Seven Simple Reasons to Use AppFuse
PDF
Bootiful Development with Spring Boot and React - RWX 2017
PDF
Front End Development for Back End Java Developers - Jfokus 2020
PDF
Front End Development for Back End Java Developers - NYJavaSIG 2019
PDF
JAX-RS JavaOne Hyderabad, India 2011
PDF
Spark IT 2011 - Developing RESTful Web services with JAX-RS
PDF
Full Stack Reactive with React and Spring WebFlux - SpringOne 2018
PDF
Grails 4: Upgrade your Game!
PDF
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
PDF
A Gentle Introduction to Angular Schematics - Angular SF 2019
PDF
Spring Up Your Graph
PDF
Front End Development for Back End Developers - vJUG24 2017
PDF
Mobile Development with Ionic, React Native, and JHipster - AllTheTalks 2020
PPTX
Containerizing ContentBox CMS
PDF
How to Win at UI Development in the World of Microservices - THAT Conference ...
A friend in need - A JS indeed
Front End Development for Backend Developers - GIDS 2019
Mobile Development with Ionic, React Native, and JHipster - ACGNJ Java Users ...
Java REST API Framework Comparison - PWX 2021
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
Seven Simple Reasons to Use AppFuse
Bootiful Development with Spring Boot and React - RWX 2017
Front End Development for Back End Java Developers - Jfokus 2020
Front End Development for Back End Java Developers - NYJavaSIG 2019
JAX-RS JavaOne Hyderabad, India 2011
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Full Stack Reactive with React and Spring WebFlux - SpringOne 2018
Grails 4: Upgrade your Game!
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Angular SF 2019
Spring Up Your Graph
Front End Development for Back End Developers - vJUG24 2017
Mobile Development with Ionic, React Native, and JHipster - AllTheTalks 2020
Containerizing ContentBox CMS
How to Win at UI Development in the World of Microservices - THAT Conference ...
Ad

Viewers also liked (16)

PDF
Add ClassyShark to your Android toolbox
PPTX
Evolving the Android Core with Aspects
PDF
Crafting Great Hypotheses - Droidcon 2016
PDF
World-Class Testing Development Pipeline for Android
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
PDF
Firebase Tech Talk By Atlogys
PDF
Contextual communications and why you should care - Droidcon DE
PDF
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
PPTX
Unit testing without Robolectric, Droidcon Berlin 2016
PDF
Little Helpers for Android Development with Kotlin
PPTX
Android Firebase
PDF
Engage and retain users in the android world - Droidcon Italy 2016
PDF
Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)
PPTX
Introduction to Firebase
PDF
Data Binding in Action using MVVM pattern
PDF
Building maintainable app
Add ClassyShark to your Android toolbox
Evolving the Android Core with Aspects
Crafting Great Hypotheses - Droidcon 2016
World-Class Testing Development Pipeline for Android
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Firebase Tech Talk By Atlogys
Contextual communications and why you should care - Droidcon DE
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Unit testing without Robolectric, Droidcon Berlin 2016
Little Helpers for Android Development with Kotlin
Android Firebase
Engage and retain users in the android world - Droidcon Italy 2016
Internal Android Library Management (DroidCon SF 2016, Droidcon Italy 2016)
Introduction to Firebase
Data Binding in Action using MVVM pattern
Building maintainable app
Ad

Similar to A realtime infrastructure for Android apps: Firebase may be what you need..and even more! (20)

PDF
2013 lecture-01-introduction
PPTX
How to Hybrid : Effective Tactics in HTML5-Native App Development
PDF
Enjoying the full stack - Frontend 2010
PPTX
Droidstat-X, Android Applications Security Analyser Xmind Generator
PDF
What's new and next for mobile development with .NET
PDF
Introduction to Xamarin for Visual Studio 2017
PDF
Resume-pierre-stephane-us
PPTX
NET !!! A must have tool under your belt
PDF
web-of-twins-20190604rzr
PPTX
Flutter Beta but Better and Better
PPTX
DevOps and the cloud: all hail the (developer) king - Daniel Bryant, Steve Poole
PDF
PharoDAYS 2015: Consortium Message: Get involved, you can get an impact by St...
PPTX
Webinar - Analyzing Video
PDF
Faites évoluer votre accès aux données avec MongoDB Stitch
PPTX
JAXLondon 2015 "DevOps and the Cloud: All Hail the (Developer) King"
PDF
Scaling frontend applications with micro-frontends Presentation.pdf
PPTX
I/O Extended (GDG Bogor) - Sidiq Permana
PPTX
WinOps meetup April 2016 DevOps lessons from Microsoft \\Build\
DOCX
Krunal_Jani_CV
PPT
Outsmarting smartphones
2013 lecture-01-introduction
How to Hybrid : Effective Tactics in HTML5-Native App Development
Enjoying the full stack - Frontend 2010
Droidstat-X, Android Applications Security Analyser Xmind Generator
What's new and next for mobile development with .NET
Introduction to Xamarin for Visual Studio 2017
Resume-pierre-stephane-us
NET !!! A must have tool under your belt
web-of-twins-20190604rzr
Flutter Beta but Better and Better
DevOps and the cloud: all hail the (developer) king - Daniel Bryant, Steve Poole
PharoDAYS 2015: Consortium Message: Get involved, you can get an impact by St...
Webinar - Analyzing Video
Faites évoluer votre accès aux données avec MongoDB Stitch
JAXLondon 2015 "DevOps and the Cloud: All Hail the (Developer) King"
Scaling frontend applications with micro-frontends Presentation.pdf
I/O Extended (GDG Bogor) - Sidiq Permana
WinOps meetup April 2016 DevOps lessons from Microsoft \\Build\
Krunal_Jani_CV
Outsmarting smartphones

Recently uploaded (20)

PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
top salesforce developer skills in 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Strategies for Manufacturing Companies
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Nekopoi APK 2025 free lastest update
PDF
AI in Product Development-omnex systems
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
System and Network Administraation Chapter 3
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Introduction to Artificial Intelligence
PDF
Odoo Companies in India – Driving Business Transformation.pdf
CHAPTER 2 - PM Management and IT Context
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
top salesforce developer skills in 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Strategies for Manufacturing Companies
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
VVF-Customer-Presentation2025-Ver1.9.pptx
Nekopoi APK 2025 free lastest update
AI in Product Development-omnex systems
How to Migrate SBCGlobal Email to Yahoo Easily
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ISO 45001 Occupational Health and Safety Management System
System and Network Administraation Chapter 3
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction to Artificial Intelligence
Odoo Companies in India – Driving Business Transformation.pdf

A realtime infrastructure for Android apps: Firebase may be what you need..and even more!

  • 1. Are you looking for a realtime infrastructure behind your mobile app? Well, Firebase may be what you need..and even more! DroidconIT2016 – Alessandro Martellucci
  • 3. Open Reply •  Reply is a leading IT Services Company, with offices in Italy, Germany, UK, Benelux, USA, Brazil, France and Poland. •  Open Reply is the company of Reply Group focused on open source software, multichannel solutions and mobile applications. •  From Rome to Milan, our team is based on a young team of over 40 engineers 100% focused on mobile development (iOS, Android & Windows Phone). •  We are specialised in broadcasting, banking, Android OS Customisation, IoT and M2M. We are hiring! Contact us at jobs@reply.eu DroidconIT2016 – Alessandro Martellucci
  • 4. Lifecycle 0.2 A binder which let you manage async operations against Android components lifecycle •  Seamless execution async-rotation-response •  Easily integration with third-party library •  No crashes after Activity/Fragment rotation •  Fragment transaction management martellux/Lifecycle com.martellux:lifecycle:0.2.0 DroidconIT2016 – Alessandro Martellucci
  • 5. Android Programmazione Avanzata •  Multi-screen and multi device development •  Functional Programming with RxJava •  Testing & Code Mantainance •  Android Wear •  Bluetooth Classic & Low Energy •  Google Cast and Chromecast DroidconIT2016 – Alessandro Martellucci
  • 6. Agenda 1.  What is Firebase 2.  NoSQL JSON database 3.  Demo 4.  Android integration 5.  Working with data 6.  Working offline 7.  FirebaseUI binding 8.  Rules management 9.  User authentication 10. Pricing DroidconIT2016 – Alessandro Martellucci
  • 7. What is Firebase •  MBaaS •  Data synchronization •  Hosting (static assets, Node.js) •  Authentication (Google, Facebook, Email & Password, Anonymous) •  Realtime database (JSON) DroidconIT2016 – Alessandro Martellucci
  • 8. NoSQL Database •  SQL & RDBMS •  JSON •  web format •  agile •  structure •  friendliness •  Drawbacks •  navigational query •  structure DroidconIT2016 – Alessandro Martellucci
  • 9. Firebase JSON { “event-name”:”Droidcon Italy”, “location”: { “latitude”:22, “longitude”:22 }, “events”: [ {“name”: “Are you looking for a realtime…”, “speaker”: “Alessandro Martellucci”, “room”: “Sala Parigi”, “time”: 000000 }, … ] } JSON DroidconIT2016 – Alessandro Martellucci
  • 10. Firebase Web Console DroidconIT2016 – Alessandro Martellucci
  • 11. Firebase Vulcan Console DroidconIT2016 – Alessandro Martellucci
  • 12. DroidconIT 2016 FirebaseDemo •  We’re going live 1.  Download app 2.  Hit your colour 3.  Be patient DroidconIT2016 – Alessandro Martellucci Source code at https://github.com/martellux/
  • 13. Android integration •  Account •  Sign with Google •  Dependecies •  Right click -> Project Structure -> Cloud -> Firebase •  Reference •  Navigation path DroidconIT2016 – Alessandro Martellucci
  • 14. Firebase setup (1/2) dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.firebase:firebase-client-android:2.5.1+’ } android { packagingOptions { exclude 'META-INF/LICENSE’ exclude 'META-INF/LICENSE-FIREBASE.txt’ exclude 'META-INF/NOTICE’ } } Configura.on DroidconIT2016 – Alessandro Martellucci
  • 15. Firebase setup (2/2) public class ApplicationInstance extends Application { @Override public void onCreate() { super.onCreate(); Firebase.setAndroidContext(this); } } Applica.on DroidconIT2016 – Alessandro Martellucci public class MainPresenter extends Presenter { private Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”); } Presenter
  • 16. Reading data •  Callbacks •  Read Type •  Value •  Child •  Firebase Listener •  ValueEventListener •  ChildEventListener (added, removed, changed) DroidconIT2016 – Alessandro Martellucci
  • 17. Read value changes Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”); mFirebaseRef.child(“data”).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { Map<String, Object> value = snapshot.getValue(Map.class); //or MyBean myBean = snapshot.getValue(MyBean.class); } @Override public void onCancelled(FirebaseError firebaseError) { Log.e(TAG, "The read failed: " + firebaseError.getMessage()); } }); Value Event Listener DroidconIT2016 – Alessandro Martellucci
  • 18. Read new child Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”); mFirebaseRef.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot snapshot, String previousChildKey) { MyBean myBean = snapshot.getValue(MyBean.class); String foo = myBean.getFoo(); } @Override public void onCancelled(FirebaseError firebaseError) { Log.e(TAG, "The read failed: " + firebaseError.getMessage()); } }); Child Event Listener DroidconIT2016 – Alessandro Martellucci
  • 19. Read child updates Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”); mFirebaseRef.addChildEventListener(new ChildEventListener() { @Override public void onChildChanged(DataSnapshot snapshot, String previousChildKey) { MyBean myBean = snapshot.getValue(MyBean.class); Log.d(TAG, ”Prop foo has been updated to: " + myBean.getFoo()); } @Override public void onCancelled(FirebaseError firebaseError) { Log.e(TAG, "The read failed: " + firebaseError.getMessage()); } }); Child Event Listener DroidconIT2016 – Alessandro Martellucci
  • 20. Read removed child Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”); mFirebaseRef.addChildEventListener(new ChildEventListener() { @Override public void onChildRemoved(DataSnapshot snapshot) { MyBean myBean = snapshot.getValue(MyBean.class); System.out.println(”This entry has been removed: " + myBean.toString()); } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } }); Child Event Listener DroidconIT2016 – Alessandro Martellucci
  • 21. Saving Data •  Non-concurrent •  Insert •  Update •  Concurrent •  IDs generation •  Transaction DroidconIT2016 – Alessandro Martellucci
  • 22. Write or replace data (1/2) { …, status: { connectedUsers: 10, ... }, ... } Online users DroidconIT2016 – Alessandro Martellucci Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”); int connectedUsers = countConnectedUsers(); mFirebaseRef.child(“status/connectedUsers”).setValue(connectedUsers); 1) Naviga.ng by subnodes
  • 23. Write or replace data (2/2) Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”); int connectedUsers = countConnectedUsers(); mFirebaseRef.child(“connectedUsers”).setValue(connectedUsers); 2) Naviga.ng direct to node DroidconIT2016 – Alessandro Martellucci Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”); Map<String, String> statusMap = new HashMap<String, String>(); statusMap.put(“connectedUsers”, String.valueOf(countConnectedUsers()); mFirebaseRef.setValue(statusMap); 3) Using Map
  • 24. Overwrite & delete JSON node DroidconIT2016 – Alessandro Martellucci { “status”:{ “connectedUsers”:1, “startTime”:1456426772000, “endTime”:1456426772000 } } { “status”:{ “connectedUsers”:1 } } Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”); Map<String, String> statusMap = new HashMap<String, String>(); statusMap.put(“connectedUsers”, String.valueOf(countConnectedUsers()); mFirebaseRef.setValue(statusMap); Java Code
  • 25. Update children Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/status”); Map<String, String> statusMap = new HashMap<String, String>(); statusMap.put(“connectedUsers”, String.valueOf(countConnectedUsers()); mFirebaseRef.updateChildren(statusMap); 1) Upda.ng nodes DroidconIT2016 – Alessandro Martellucci Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”); Map<String, String> statusMap = new HashMap<String, String>(); statusMap.put(“status/connectedUsers”, String.valueOf(countConnectedUsers()); mFirebaseRef.updateChildren(statusMap); 2) Upda.ng subnodes
  • 26. Concurrent-indipendent insert Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”); Firebase dataChildRef = mFirebaseRef.child(“data”); Firebase newChildRef = dataChildRef.push(); Map<String, Object> colorMap = new HahsMap<String, Object>(); colorMap.put(“red”, “242”); colorMap.put(“green”, “10”); colorMap.put(“blue”, “22”); newChildRef .setValue(colorMap); Java Code DroidconIT2016 – Alessandro Martellucci
  • 27. Concurrent-dipendent insert Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”); newChildRef.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(MutableData currentData) { if((currentData.getValue() == null) { currentData.setValue(1); } else { currentData.setValue((Long) currentData.getValue() + 1); } return Transaction.success(currentData); } @Override public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) { … } }); Java Code DroidconIT2016 – Alessandro Martellucci
  • 28. Working offline •  Works offline •  Seamless experience •  Auto synchronization DroidconIT2016 – Alessandro Martellucci
  • 29. User authentication DroidconIT2016 – Alessandro Martellucci •  User identification •  Email & password login •  Easy integration •  Facebook, GitHub, Google and Twitter •  Session management •  Single session •  Custom token
  • 30. FirebaseUI - Authentication DroidconIT2016 – Alessandro Martellucci 1.  Add SDK dependencies 2.  Add Facebook/Twitter/Google keys to strings.xml 3.  Change our AndroidManifest.xml 4.  Inherit from FirebaseLoginBaseActivity 5.  Enable authentication providers 6.  Call showFirebaseLoginDialog();
  • 31. Facebook authentication DroidconIT2016 – Alessandro Martellucci Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com/data”); mFirebaseRef .addAuthStateListener(new Firebase.AuthStateListener() { @Override public void onAuthStateChanged(AuthData authData) { if (authData != null) { // user is logged in Map<String, String> map = new HashMap<String, String>(); map.put("provider", authData.getProvider()); if(authData.getProviderData().containsKey("displayName")) { map.put("displayName", authData.getProviderData().get("displayName").toString()); } ref.child("users").child(authData.getUid()).setValue(map); } else { // user is not logged in } } }); Authen.ca.on
  • 32. Rules management (1/2) DroidconIT2016 – Alessandro Martellucci { "rules": { ".read": true, ".write": true } } Basic rules •  JavaScript-like •  Node permissions •  Node validations
  • 33. Rules management (2/2) DroidconIT2016 – Alessandro Martellucci { “status”: { “schedule”: { “startTime”: 123456670292, … } } } startTime node { "rules": { "status": { "schedule": { "startTime": { // readable ".read": true, // data written must be a number ".validate": "newData.isNumber()”, // write authorization ".write": "$user_id === auth.uid” }}}}} Rules for startTime node
  • 34. FirebaseUI DroidconIT2016 – Alessandro Martellucci •  Open source library •  iOS support •  Authentication •  built-in dialog •  List binding •  ListView •  RecyclerView
  • 35. FirebaseUI – ListView DroidconIT2016 – Alessandro Martellucci @Override protected void onCreate(Bundle savedInstanceState) { … ListView messagesView = (ListView) findViewById(R.id.messages_list); mAdapter = new FirebaseListAdapter<MyBean>(this, MyBean.class, R.layout.my_list_item, mFirebaseRef) { @Override protected void populateView(View view, MyBean myBean, int position) { ((TextView)view.findViewById(android.R.id.text1)).setText(myBean.getFoo()); } }; messagesView.setAdapter(mAdapter); } Adapter
  • 36. FirebaseUI – RecyclerView DroidconIT2016 – Alessandro Martellucci @Override protected void onCreate(Bundle savedInstanceState) { … RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler); recycler.setHasFixedSize(true); recycler.setLayoutManager(new LinearLayoutManager(this)); mAdapter = new FirebaseRecyclerAdapter<MyBean, MyBeanViewHolder>(MyBean.class, R.layout.my_line_list_item, MyBeanViewHolder.class, mFirebaseRef) { @Override public void populateViewHolder(MyBeanViewHolder myBeanViewHolder, MyBean myBean, int position) { myBeanViewHolder.nameText.setText(myBean.getFoo()); } }; recycler.setAdapter(mAdapter); } Adapter
  • 37. Pricing DroidconIT2016 – Alessandro Martellucci FREE (0$ forever) SPARK (5$ month) … INFERNO (1499$ month) DB connec.ons 100 100 … UNLIMITED DB storage 1 GB 1 GB … 300 GB DB transfer 10 GB 10 GB … 1.5 TB Private Backups NO NO … YES Authen.ca.on UNLIMITED USERS UNLIMITED USERS … UNLIMITED USERS Hos.ng Storage 1 GB 1 GB … 10 GB Hos.ng Transfer 100 GB 100 GB … 1 TB Custom Domain NO YES … YES
  • 38. Resources •  BLOG: https://www.firebase.com/blog •  Twitter: @firebase •  Facebook: Firebase •  Google Plus: Firebase •  Official: http://www.firebase.com •  Github: https://github.com/firebase •  YouTube: Firecast: Firebase Screencast DroidconIT2016 – Alessandro Martellucci
  • 39. THANK YOU ALL DroidconIT2016 – Alessandro Martellucci