a realtime infrastructure for android apps: firebase may be what you need..and even more!

39
Are you looking for a realtime infrastructure behind your mobile app? Well, Firebase may be what you need..and even more! DroidconIT2016 – Alessandro Martellucci

Upload: alessandro-martellucci

Post on 16-Apr-2017

1.310 views

Category:

Software


0 download

TRANSCRIPT

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

Are you looking for a realtime infrastructure behind your mobile app?

Well, Firebase may be what you need..and even more!

DroidconIT2016 – Alessandro Martellucci

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

I’m Alessandro Martellucci

[email protected]

martellux

+AlessandroMartellucci

@martellux

DroidconIT2016 – Alessandro Martellucci

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

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 [email protected]

DroidconIT2016 – Alessandro Martellucci

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

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

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

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

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

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

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

What is Firebase

•  MBaaS

•  Data synchronization •  Hosting (static assets, Node.js)

•  Authentication (Google, Facebook, Email & Password, Anonymous)

•  Realtime database (JSON)

DroidconIT2016 – Alessandro Martellucci

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

NoSQL Database

•  SQL & RDBMS

•  JSON •  web format •  agile •  structure •  friendliness

•  Drawbacks •  navigational query •  structure

DroidconIT2016 – Alessandro Martellucci

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

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

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

Firebase Web Console

DroidconIT2016 – Alessandro Martellucci

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

Firebase Vulcan Console

DroidconIT2016 – Alessandro Martellucci

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

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/

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

Android integration

•  Account •  Sign with Google

•  Dependecies •  Right click -> Project Structure -> Cloud -> Firebase

•  Reference •  Navigation path

DroidconIT2016 – Alessandro Martellucci

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

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

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

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

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

Reading data

•  Callbacks

•  Read Type •  Value •  Child

•  Firebase Listener •  ValueEventListener •  ChildEventListener (added, removed, changed)

DroidconIT2016 – Alessandro Martellucci

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

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()); } });

ValueEventListener

DroidconIT2016 – Alessandro Martellucci

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

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()); } });

ChildEventListener

DroidconIT2016 – Alessandro Martellucci

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

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()); } });

ChildEventListener

DroidconIT2016 – Alessandro Martellucci

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

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()); } });

ChildEventListener

DroidconIT2016 – Alessandro Martellucci

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

Saving Data

•  Non-concurrent •  Insert •  Update

•  Concurrent •  IDs generation •  Transaction

DroidconIT2016 – Alessandro Martellucci

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

Write or replace data (1/2)

{ …, status: { connectedUsers: 10, ... }, ... }

Onlineusers

DroidconIT2016 – Alessandro Martellucci

Firebase mFirebaseRef = new Firebase(“https://droidconit2016-demo.firebaseio.com”); int connectedUsers = countConnectedUsers(); mFirebaseRef.child(“status/connectedUsers”).setValue(connectedUsers);

1)Naviga.ngbysubnodes

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

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.ngdirecttonode

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)UsingMap

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

Overwrite & delete

JSONnode

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);

JavaCode

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

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.ngnodes

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.ngsubnodes

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

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);

JavaCode

DroidconIT2016 – Alessandro Martellucci

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

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) { … } });

JavaCode

DroidconIT2016 – Alessandro Martellucci

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

Working offline

•  Works offline

•  Seamless experience

•  Auto synchronization

DroidconIT2016 – Alessandro Martellucci

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

User authentication

DroidconIT2016 – Alessandro Martellucci

•  User identification •  Email & password login

•  Easy integration •  Facebook, GitHub, Google and Twitter

•  Session management •  Single session •  Custom token

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

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();

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

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

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

Rules management (1/2)

DroidconIT2016 – Alessandro Martellucci

{ "rules": { ".read": true, ".write": true } }

Basicrules

•  JavaScript-like

•  Node permissions •  Node validations

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

Rules management (2/2)

DroidconIT2016 – Alessandro Martellucci

{ “status”: { “schedule”: { “startTime”: 123456670292, … } } }

startTimenode

{ "rules": { "status": { "schedule": { "startTime": { // readable ".read": true, // data written must be a number ".validate": "newData.isNumber()”, // write authorization ".write": "$user_id === auth.uid” }}}}}

RulesforstartTimenode

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

FirebaseUI

DroidconIT2016 – Alessandro Martellucci

•  Open source library •  iOS support

•  Authentication

•  built-in dialog •  List binding

•  ListView •  RecyclerView

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

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

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

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

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

Pricing

DroidconIT2016 – Alessandro Martellucci

FREE(0$forever)

SPARK(5$month)

… INFERNO(1499$month)

DBconnec.ons 100 100 … UNLIMITED

DBstorage 1GB 1GB … 300GB

DBtransfer 10GB 10GB … 1.5TB

PrivateBackups NO NO … YES

Authen.ca.on UNLIMITEDUSERS

UNLIMITEDUSERS … UNLIMITED

USERS

Hos.ngStorage 1GB 1GB … 10GB

Hos.ngTransfer 100GB 100GB … 1TB

CustomDomain NO YES … YES

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

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

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

THANK YOU ALL

DroidconIT2016 – Alessandro Martellucci