The following is a guest blog post from Sidney Maestre, a Platform Evangelist for StackMob, a leading provider of backend services for mobile developers. He spent the last few years building mobile apps and sharing his knowledge with others. These efforts included speaking at Adobe MAX, 360iDev, SenchaCon, HTML5DevConf, Silicon Valley Code Camp, creating two courses, jQuery Mobile for Beginners and Learn Backbone.js + StackMob, for the uDemy.com platform and organizing the Bay Area Mobile meetup. Follow him on Twitter and Github at @SidneyAllen.
Building backend services for a mobile app is time consuming and difficult. Adding a datastore API, push notifications and user authentication can eat up 25 to 40 percent of development time. That’s why many mobile developers turn to StackMob to help them build a backend in minutes not months.
Introducing the StackMob Platform
StackMob provides a cloud-based backend for mobile app developers that incorporates BaaS (backend as a service) features to enable rapid creation of CRUD (create, read, update and delete) APIs for mobile apps. StackMob simplifies file upload and storage to Amazon S3, user management and user authentication through Facebook and Twitter. They also support two of the most popular cloud services among mobile developers push notifications and geo location services.
Go beyond the StackMob Platform
StackMob supports custom-code enabling mobile developers to write business logic and go beyond what is possible with a traditional datastore API. With the introduction of HTTPService support in custom code, you can now call third-party APIs over HTTP. This article shows how you can leverage email infrastructure service provider SendGrid. SendGrid is a cloud-based service that replaces your company’s email infrastructure and provides everything you need to operate a reliable and scalable outbound email delivery system. For more information about SendGrid, check out their API documentation.
Objective for this article
We’ll review how to create a StackMob application, modify the user schema to add an email and name field and add a new user via the StackMob dashboard. Next we’ll download the example custom code, add our SendGrid credentials and compile and upload the code to StackMob. Lastly, we’ll download and configure an XCode project and run it in the simulator to send a custom email message for our user.
Create a StackMob application
You’ll need a StackMob account, so if you haven’t already signup for your free StackMob account now.
After you sign up, you’ll be taken to our getting started page. Give your application a name and click the create application button. I’m calling my “sendgrid”, you can call it anything you want. You have the option to select a platform and download the StackMob SDK, but for this article we provide you with an XCode project with the SDK already install, so you can skip this step. At the top of the page click on dashboard.
Modify the User Schema
Every application created on StackMob includes a User schema with a username and password. Let’s add an email address and name field to the User schema. In the dashboard, click on Manage Schemas link on the left.
Click the edit link next to the User schema.
Click the add field button and add the email field with a data type string. Repeat this step to add a field called name. You’ll want to name the field “email” and “name”, since that’s how I refer to them in the SendGrid code for this article.
Remember to click the SAVE SCHEMA button to save your changes.
Create a new user
Click the object browser link on the left side and click add object on the user schema. Enter your username, password, email and name for your user.
Download StackMob Example Code
Download the StackMob custom-code examples from Github. You’ll find the SendGrid sample code in /Java/src/main/java/com/stackmob/examples/SendGrid.java.
Get SendGrid Credentials
You’ll need a SendGrid account to make the API call, so if you haven’t already signup for your SendGrid account now.
Enter your SendGrid username and password into the SendGrid.java file
What are we doing in custom code?
The first part of the ResponseToProcess method we parse the JSON object sent from our mobile app, then query the StackMob datastore with the username that is passed. Once we find a match, we can use the name and email address for our SendGrid email.
Then, we’ll build our SendGrid URL and POST it to SendGrid.
Now, it’s time to build our JAR file.
- In your Terminal app, cd into the stackmob-customcode-example/java/ directory.
- Type mvn clean package and hit enter.
- Your JAR file will be written to stackmob-customcode-example/java/target with the name java-example-0.1.0-SNAPSHOT-jar-with-dependencies.jar
- Upload JAR to StackMob through the StackMob dashboard.
Get your StackMob Public API Key
Go to Manage App Info in the StackMob Dashboard and copy the Development Public Key. You’ll be pasting it into the AppDelegate.m file in the XCode project.
Download the XCode Project
You’ll download a project with the StackMob SDK installed and setup to pass a username, subject and email text to the custom method.
Inside the XCode project, open the AppDelegate.m file. You’ll update the StackMob client init with the Development Public Key.
Try it out
Run the project in the iOS Simulator. Enter the username of the User you created on StackMob and click send email button. You should get your email shortly.
If you have feedback or questions please contact me at email@example.com.