José Manuel Ortega Candel Mobile Backend as a Service(MBaaS) Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/ Leganés 12-13 Febrero 2015
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 3 Persistence in Mobile iNDEX Cloud Computing / BaaS MBaaS features /architecture/ Startups Push Notifications / GCM / API REST / Storage Kinvey / Backendless / BackBeam / Parse Demos on Android
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 5 Persistence in Mobile Android iOS SQLite android.database.sqlite Tables and relations Core Data Objects DataModel Content Providers /data/data/ /databases/ Only acces with root DataModel editor in Xcode for register objects and their relationships
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 7 Persistence in Mobile public class Event extends Entity { public int id; public String name; } Event e = Entity.query(Event.class).where("id=1") .execute(); p.name = “Techfest"; p.save();
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 15 MBaaS features API REST for CRUD operations(GET,POST,UPDATE,DELETE,PATCH) Multi platform SDK Cloud Storage Push notifications User management Data Browser Query language Import/Export data Analytics / Monetization
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 16 MBaaS features Reduce server side coding developers can focus on front-end development often providers offer SDKs that wrap REST API calls and handle in/out parameters: both HTML/JS and native (iOS, Android,Windows Phone) No server setup, ready to use (HOSTED SERVICE) Deployed on the cloud, with built-in scalability
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 20 MBaaS OBJECT-BASED DATA STORAGE DATA SYNCHRONIZED USER ACCOUNTS & AUTHENTICATION REAL-TIME ANALYTICS PUSH NOTIFICATIONS API REST
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 23 Push Notifications Var channel= _application.pubSub.Subscribe(“channel name”); channel.Send({message}); Push Notification Service MBaaS
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 24 Push Notifications Multiplatforms messages Enable server applications to send information to mobile apps even when the app isn’t in use The device displays the information using a “badge,” alert, or pop up message. A push notification uses the service provided by the device’s operating system: iOS - Apple Push Notification service (APNS) Android - Google Cloud Messaging (GCM)
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 27 Push Notifications iOS Apple Push Notification service (APNs) Certificate Private Key Android Google Cloud Messaging API KEY Sender ID Google API Console > Authentication && Project Number
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 29 Kinvey Data collection Kinvey stores data as collections and entities. Entities are JSON documents. Collections belong to applications. Internally, data is stored in a MongoDB cluster.
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 55 Security Authentication && Authorization Basic Authentication userName and password encoded in Base 64 authorization header: Authorization: Basic bXlVc2VybmFtZTpteVBhc3N3b3Jk Session Authentication Auth token / Social Networks Permissions Shared / Private / Read Only / Full
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 58 Parse SERVICES User Authentication Push Notification Data Storage Rest API JavaScript SDK iOS SDK Android SDK JavaScript SDK
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 60 Connect your app with services APPLICATION ID CLIENT KEY + Parse.initialize(“APP_ID”,”C_KEY”);
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 62 Parse Objects Parse stores data internally as flat JSON Documents, called ParseObject, with have the restriction that keys must be alphanumeric strings. Parse automatically creates ‘classes’ for ParseObjects, grouping objects with similar properties. Classes and all objects associated to them belong to applications, which can be defined on the Parse web interface. ParseObject event = new ParseObject(“Event"); event.put(“eventName", “techfest”); event.put(“eventURL", “http://techfest.uc3m.es"); event.put(“eventDate", new Date());
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 66 Parse with ANDROID
// Enable Local Datastore. Parse.enableLocalDatastore(this); Parse.initialize(this, “APPLICATION_ID", “CLIENT_KEY"); ParseObject event = new ParseObject(“Event"); event.put(“eventName", “techfest”); event.put(“eventURL", “http://techfest.uc3m.es"); event.put(“eventDate", new Date()); event.saveEventually(); //Object saved when user has network connection event.saveInBackground();
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 67 Parse Object import com.parse.ParseClassName; import com.parse.ParseObject; @ParseClassName(“Event") public class Event extends ParseObject{ public Event(){} public String getName(){ return getString(“eventName"); } public void setName(String name){ put(“eventName", name); } }} ParseObject.registerSubclass(Event.class);
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 68 Parse Object with files byte[] data = myPhotoObject.toByteArray(); ParseFile eventPhoto = new ParseFile("t3chfest.jpg", data); eventPhoto.saveInBackground(); ParseObject event = new ParseObject("Event"); event.put("name", "t3chFest"); event.put(”photo”, eventPhoto); event.saveInBackground();
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 72 CACHE query.cachePolicy property CachePolicy.CACHE_ELSE_NETWORK The query first tries to load from the cache, but if that fails, it loads results from the network.If neither cache nor network succeed, there is a PFError. CachePolicy.CACHE_THEN_NETWORK The query first loads from the cache, then loads from the network. In this case, the callback will actually be called twice - first with the cached results, then with the network results. Since it returns two results at different times, this cache policy cannot be used synchronously with findObjects.
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 73 JavaScript var event= Parse.Object.extend ("Event"); var query = new Parse.Query (event); query.find ({ success: function (results) { $scope.data.events = results; }, error: function (error) { alert ("Error:" + error.code + "" + error.message); } });
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 77 Parse Push Notifications //Enable to receive push PushService.setDefaultPushCallback(this, RespondToPushActivity.class); ParseInstallation pi = ParseInstallation.getCurrentInstallation(); //Register a channel to test push channels Context ctx = this.getApplicationContext(); PushService.subscribe(ctx, "ch1", RespondToPushActivity.class); pi.saveEventually();
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 79 API REST AJAX CALL var headers = {"X-Parse-Application-Id":"YOUR-APP-ID-HERE", "X-Parse-REST-API-Key":"YOUR- REST-API-KEY-HERE"}; function getData() { $.ajax({ "type":"GET", "url":"https://api.parse.com/1/classes/Event", "dataType":"json", "contentType":"application/json", "headers":headers, success:function(data, status, xhr) { var result = ""; for(var i = 0; i < data.results.length; i++) { result = result +""+ data.results[i].eventName+" / "+ data.results[i].eventURL+""; }} }); }
Mobile Backend as a Service(MBaaS) Leganés 12-13 Febrero 2015 83 PARSE USER MANAGMENT //login ParseUser.logInInBackground(username, password, new LogInCallback() { public void done(ParseUser user, com.parse.ParseException e) { }}); //signUp ParseUser user = new ParseUser(); user.setUsername(username); user.setPassword(password); user.setEmail(email); //optional user.signUpInBackground(new SignUpCallback() { public void done(com.parse.ParseException e) { }}); //Check if user is logged ParseUser currentUser = ParseUser.getCurrentUser();