2015-04-14

Mobile back end as a service (MBaaS) aims at giving app developers the ability to create seamlessly new feature-complete cross-platform native and web applications. In the first part1 of this series, I walked through a messaging application demo powered by the Kinvey application. We explored how to leverage user management, file storage and the data store.

To complete the demo, we need to leverage two key pieces of Kinvey functionality: the permissions provided by the data store, and push notifications, which are enabled through the business logic functionality.


2
WaterCooler messaging app demo. (View large version3)
Setting Up

Both the first article4 in this series and my initial article explaining the concept of MBaaS5 are prerequisites for this article. Also, this article does take a “real app” approach to understanding MBaaS. That being said, some knowledge of iOS and Swift would be helpful. However, I have provided the application’s source code6, so you can learn and analyze at your own pace.

Permissions

In addition to the data store and file storage support provided by Kinvey, there is also a robust security model that can be applied to all of this data. All of the configuration for permissions on either files or data store objects happen with the KCSMetadata class. This instance can be configured with custom data model classes or attached as parameters to a file when uploading it to the file store.

File Permissions

As we discussed earlier, the file management functionality is a powerful piece of any MBaaS tool. However, clarifying who can access this content is equally important. In our example, profile pictures that are uploaded should be viewable by all users. If we wanted to add image sharing to the one-to-one messaging portion of the application, then we would want to limit who can access the image. Because we want to allow anyone to see the profile picture, we can leverage the setGloballyReadable method of KCSMetadata to ensure that while only the creator can edit this file, any user may read it.

Data Store Permissions

To make this as simple as possible to integrate in the application, I’ve defined a few Swift extensions7 to the classes provided in Kinvey’s SDK for iOS. One such extension is a convenience initializer for the KCSMetadata class to configure permissions for the Message and MessageThread classes:

With this in place, we can now initialize the meta-data instance with the correct security settings when we create an instance of the Message class. Now, when it is saved, it will be accessible only to the sender and recipient.

Business Logic And Push Notifications

An MBaaS solutions that provides all of the functionality detailed so far would be adequate in a limited set of use cases. However, the ability to centralize business logic when tied to the lifecycle of a data object opens up many doors to creating effective cross-platform applications. In the WaterCooler demo application, this is relevant in two distinct ways:

When a Message instance is saved to the data store, we need to determine who it is being sent to. After we have ascertained the recipient of the message, we need to send them a push notification.

When a Message instance is saved to the data store, we need to also fetch the MessageThread instance and save the latestMessage property. This frees us from having to make an additional fetch call when populating the message thread’s view.

While both of these could be accomplished through the iOS application, that would be putting the logic in the incorrect place. Consider if we also had a web and Android application for the WaterCooler application. We would want the same functionality to occur upon a message being saved on both of those platforms as well. In this case, any business logic that spans multiple platforms should reside on the server using Kinvey’s business logic capability.

Configuring Push Notifications

For push notifications to work properly on iOS, we need to configure Kinvey to work with our push notification certificate(s) for the application. This step involves two distinct steps:

Configure the application with push notification certificates for development and production in the iOS Developer Center.

Export and uploaded the required certificate to Kinvey.

The full process of completing these steps is beyond the scope of this article, but Kinvey has a “Push8” guide in the documentation to assist you in the overall integration process.

Kinvey Collection Hooks

To accomplish the two goals listed above, we leverage one particular piece of functionality in Kinvey: collection hooks. With collection hooks, we can define server-side business logic in JavaScript that is executed at particular points. The predefined points for a collection are listed below:

onPreSave

This logic is executed prior to an instance being saved in the data store. Any errors that occur in this logic would prevent the save action from being executed.

onPreFetch

This logic is executed prior to any fetch call on the entity type. Any errors would prevent the fetch action from being executed.

onPreDelete

This logic is executed prior to any entity instance being deleted from the data store. Any errors would prevent the instance from being deleted from the data store.

onPostSave

This logic would be executed after an instance is saved to the data store. Any errors here would not prevent the object from being stored in the data store, but they would cause an error to return on the request.

onPostFetch

This logic would be executed after a fetch command has been executed by the data store. Any errors here would not prevent the object from being fetched from the data store, but they would cause an error to return on the request.

onPostDelete

The logic would be executed after an instance is deleted from the data store. Any errors here would not prevent the object from being deleted, but they would cause an error to return for the request.

Push notifications are an important part of any messaging application. Ideally, we would want one to be sent each time a sender saves a Message object to the data store. Kinvey provides this capability through its business logic’s hooks functionality. In short, you can add custom JavaScript logic that executes at certain points in the lifecycle of a data object.

Business Logic Modules

Kinvey provides a set of modules that enable you to access core Kinvey functionality within your business logic’s code. Several modules are available, but I’ve leveraged the following modules with the business logic hook:

logger

Provides a seamless way to log information from your business logic to the Kinvey console

collectionAccess

Provides a way to query the Kinvey data store from within your business logic

push

Provides access to the push notification functionality for Kinvey with your business logic

async

Provides an approach for dealing with asynchronous JavaScript. This is the async9 module you may have used in Node.js applications.

For a complete reference on business logic modules, see the “Modules10” guide in the “Business Logic” section of Kinvey’s documentation.

These hooks are configured via the Kinvey Console11. To add an onPostSave hook for the Messages collection, follow these steps:

Select the “Business Logic” option in the navigation on the left side of the Kinvey console.

Select the “Create Your First Script” option.

In the popup menu, select the “Add Hook” option for the Messages collection.

Select the onPostSave option.

You will see the new onPostSave JavaScript hook within the browser, which you can edit.


12
The business logic section of Kinvey’s console. (View large version13)
Complete Code for Business Logic

Putting these pieces together yields a single JavaScript method that is only 115 lines (with comments). This code gets the recipient, updates the thread with the latest message and then sends the push notification to the recipient.

To enable this hook, copy this JavaScript into the hook you’ve just created:

With this code in place (and the push notifications properly configured), the application will now send a push notification to the recipient and also update the lastMessage property in the relevant message thread. In the image below, you can see a sample push notification that has been sent to the recipient of a message:


14
A push notification in the WaterCooler app. (View large version15)
Putting the Pieces Together

While we didn’t walk through every step of how the application was created, you now have a complete example of a Kinvey application that you can use as a starting point for your own application. Hopefully, this article has given you a good working understanding of the code required, but also helped you think about how to solve problems using an MBaaS solution.

16
WaterCooler messaging app demo. (View large version17)

If you want to continue learning, you could add a lot of features to this application:

support for images in messages,

notifications for when a user is typing,

group messaging.

All of these features would be possible with Kinvey’s back end.

Further Resources

Here are a few resources that were used in the creation of our app:

SlackTextViewController18, Slack, GitHub

The team at Slack has released an amazing control for inputting text in a messaging app.

MBProgressHUD19, Jonathan George, GitHub

This is a common control for showing overlays when the application is waiting on a task to finish. It is very customizable and configurable.

Search for Creative Commons-licensed icons on Freepik20, SimpleIcon21, Google22 and Flaticon23.

Brandon McQuilkin24, GitHub

McQuilkin used to provide some great Swift image extensions for masking images. Since my initial work on the app, he has removed these from GitHub.

Conclusion

I hope that through the last few articles, you have seen ways that an MBaaS solution can help you to create compelling business- and consumer-focused mobile experiences. This certainly is a space with a lot of focus at the moment, and I expect rapid maturity and evolution of these services over the next year. Most any mobile developer would agree that this a valuable tool to have in your arsenal.

The following additional resources from Kinvey’s documentation will assist you to create iOS applications:

“Getting Started25,”

“Kinvey Sample Apps26”

“Timeline: See How Our iOS Platform Is Evolving27”

(da, al, ml)

Footnotes

1 http://www.smashingmagazine.com/2014/12/15/understanding-mobile-back-end-as-a-service/

2 http://www.smashingmagazine.com/wp-content/uploads/2015/03/01-watercooler-demo-app-opt.jpg

3 http://www.smashingmagazine.com/wp-content/uploads/2015/03/01-watercooler-demo-app-opt.jpg

4 http://www.smashingmagazine.com/2014/12/15/understanding-mobile-back-end-as-a-service/

5 http://www.smashingmagazine.com/2014/12/15/understanding-mobile-back-end-as-a-service/

6 https://github.com/davidtucker/WaterCooler-Demo

7 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

8 http://devcenter.kinvey.com/ios/guides/push

9 https://github.com/caolan/async

10 http://devcenter.kinvey.com/ios/reference/business-logic/reference.html#utils-module

11 https://console.kinvey.com/

12 http://www.smashingmagazine.com/wp-content/uploads/2015/03/02-kinvey-console-opt.png

13 http://www.smashingmagazine.com/wp-content/uploads/2015/03/02-kinvey-console-opt.png

14 http://www.smashingmagazine.com/wp-content/uploads/2015/03/03-push-notifications-opt.jpg

15 http://www.smashingmagazine.com/wp-content/uploads/2015/03/03-push-notifications-opt.jpg

16 http://www.smashingmagazine.com/wp-content/uploads/2015/03/04-in-se-on-devices-opt.jpg

17 http://www.smashingmagazine.com/wp-content/uploads/2015/03/04-in-se-on-devices-opt.jpg

18 https://github.com/slackhq/SlackTextViewController

19 https://github.com/jdg/MBProgressHUD

20 http://www.freepik.com/

21 http://www.simpleicon.com/

22 http://www.google.com/

23 http://www.flaticon.com/

24 https://github.com/Marxon13

25 http://devcenter.kinvey.com/ios/guides/getting-started

26 http://devcenter.kinvey.com/ios/samples

27 http://devcenter.kinvey.com/ios/timeline

The post Putting Mobile Back End As A Service Into Practice (Part 2) appeared first on Smashing Magazine.

Show more