copyright | lastupdated | ||
---|---|---|---|
|
2017-11-27 |
{:java: #java .ph data-hd-programlang='java'} {:swift: #swift .ph data-hd-programlang='swift'} {:ios: data-hd-operatingsystem="ios"} {:android: data-hd-operatingsystem="android"} {:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
In this tutorial, you will learn how to use Cloud Functions along with Cognitive and Data services to build a serverless backend for a mobile application. The application shown in this tutorial is a feedback app that smartly analyses the tone of the feedback text and appropriately acknowledges the customer through a Push Notification. {:shortdesc}
This tutorial is configurable based on your target platform. You are currently viewing the documentation for the iOS / Swift version of this tutorial. Use the drop down switcher at the top of this documentation to select the Android / Java version of this tutorial. {: swift}
This tutorial is configurable based on your target platform. You are currently viewing the documentation for the Android / Java version of this tutorial. Use the drop down switcher at the top of this documentation to select the iOS / Swift version of this tutorial. {: java}
- Provision services to handle user authentication, feedback persistence and analysis.
- Configure push notifications.
- Deploy a serverless backend.
- Configure and run a native mobile application to collect user feedback.
This tutorial uses the following products:
![](images/solution11/Architecture.png)
- The user authenticates against App ID. App ID provides access and identification tokens.
- Further calls to the backend API include the access token.
- The backend is implemented with Cloud Functions. The serverless actions, exposed as Web Actions, expect the token to be sent in the request headers and verify its validity (signature and expiration date) before allowing access to the actual API.
- When the user submits a feedback, the feedback is stored in Cloudant
- The feedback text is processed with Tone Analyzer.
- Based on the analysis result, a notification is sent back to the user with Push Notifications.
- The user receives the notification on the device.
{: #prereqs}
This tutorial uses the IBM Cloud command line tool to provision resources and deploy code. Make sure to install the bx
command line tool.
- IBM Cloud Developer Tools - Script to install bx CLI and required plug-ins (Cloud Foundry and Cloud Functions)
Additionally you will need the following software and accounts:
-
Java 8
-
Android Studio 2.3.3
-
Google Developer account to configure Firebase Cloud Messaging
-
Bash shell, cURL {: java}
-
Xcode
-
Apple Developer account to configure Apple Push Notification Service
-
Bash shell, cURL {: swift}
In this tutorial, you will configure push notifications for the application. The tutorial assumes you have completed the basic Push Notifications tutorial for either Android or iOS and you are familiar with the configuration of Firebase Cloud Messaging or Apple Push Notification Service. {:tip}
For Windows 10 users to work with the command line instructions, we recommend installing the Windows Subsystem for Linux and Ubuntu as described in this article. {: tip} {: java}
The repository contains both the mobile application and the Cloud Functions actions code.
-
Checkout the code from the GitHub repository
git clone https://github.com/IBM-Cloud/serverless-followupapp-android
{: pre} {: java}
git clone https://github.com/IBM-Cloud/serverless-followupapp-ios
{: pre} {: swift}
-
Review the code structure
File | Description |
---|---|
actions | Code for the Cloud Functions actions of the serverless mobile backend |
android | Code for the mobile application |
deploy.sh | Helper script to install, uninstall, update the Cloud Functions trigger, actions, rules |
{: java} |
File | Description |
---|---|
actions | Code for the Cloud Functions actions of the serverless mobile backend |
followupapp | Code for the mobile application |
deploy.sh | Helper script to install, uninstall, update the Cloud Functions trigger, actions, rules |
{: swift} |
{: #provision_services}
In this section, you will provision the services used by the application. You can choose to provision the services from the IBM Cloud catalog or using the bx
command line.
It is recommended that you create a new space to provision the services and deploy the serverless backend. This helps to keep all the resources together.
- Go to the IBM Cloud catalog
- Create a Cloudant NoSQL DB service with the Lite plan. Set the name to serverlessfollowup-db.
- Create a Watson Tone Analyzer service with the Standard plan. Set the name to serverlessfollowup-tone.
- Create an App ID service with the Graduated tier plan. Set the name to serverlessfollowup-appid.
- Create a Push Notifications service with the Lite plan. Set the name to serverlessfollowup-mobilepush.
With the command line, run the following commands to provision the services and retrieve their credentials:
bx cf create-service cloudantNoSQLDB Lite serverlessfollowup-db
{: pre}
bx cf create-service-key serverlessfollowup-db for-cli
{: pre}
bx cf service-key serverlessfollowup-db for-cli
{: pre}
bx cf create-service tone_analyzer standard serverlessfollowup-tone
{: pre}
bx cf create-service-key serverlessfollowup-tone for-cli
{: pre}
bx cf service-key serverlessfollowup-tone for-cli
{: pre}
bx cf create-service AppID "Graduated tier" serverlessfollowup-appid
{: pre}
bx cf create-service-key serverlessfollowup-appid for-cli
{: pre}
bx cf service-key serverlessfollowup-appid for-cli
{: pre}
bx cf create-service imfpush lite serverlessfollowup-mobilepush
{: pre}
bx cf create-service-key serverlessfollowup-mobilepush for-cli
{: pre}
bx cf service-key serverlessfollowup-mobilepush for-cli
{: pre}
{: #push_notifications}
When a user submits a new feedback, the application will analyze this feedback and send back a notification to the user. The user may have moved to another task, or may not have the mobile app started so using push notifications is a good way to communicate with the user. The Push Notifications service makes it possible to send notifications to iOS or Android users via one unified API. In this section, you will configure the Push Notifications service for your target platform.
{: java}
- In the Firebase console, create a new project. Set the name to serverlessfollowup
- Navigate to the Project Settings
- Under the General tab, add two applications:
- one with the package name set to: com.ibm.mobilefirstplatform.clientsdk.android.push
- and one with the package name set to: serverlessfollowup.app
- Download the
google-services.json
containing the two defined applications from Firebase console and place this file in theandroid/app
folder of the checkout directory. - Find the Sender ID and Server Key (also called API Key later on) under the Cloud Messaging tab.
- In the Push Notifications service dashboard, set the value of the Sender ID and API Key. {: java}
{: swift}
- Go to the Apple Developer portal and Register an App ID.
- Create a development and distribution APNs SSL certificate.
- Create a development provisioning profile.
- Configure the Push Notifications service instance on IBM Cloud. Refer to Obtain APNs credentials and configure Push Notifications service for detailed steps. {: swift}
{: #serverless_backend}
With all the services configured, you can now deploy the serverless backend. The following Cloud Functions artifacts will be created in this section:
Artifact | Type | Description |
---|---|---|
serverlessfollowup |
Package | A package to group the actions and to keep all service credentials |
serverlessfollowup-cloudant |
Package Binding | Bound to the built-in Cloudant package |
serverlessfollowup-push |
Package Binding | Bound to the Push Notifications package |
auth-validate |
Action | Validates the access and identification tokens |
users-add |
Action | Persists the user information (id, name, email, picture, device id) |
users-prepare-notify |
Action | Formats a message to use with Push Notifications |
feedback-put |
Action | Stores a user feedback in the database |
feedback-analyze |
Action | Analyzes a feedback with Tone Analyzer |
users-add-sequence |
Sequence exposed as Web Action | auth-validate and users-add |
feedback-put-sequence |
Sequence exposed as Web Action | auth-validate and feedback-put |
feedback-analyze-sequence |
Sequence | read-document from Cloudant, feedback-analyze , users-prepare-notify and sendMessage with Push Notifications |
feedback-analyze-trigger |
Trigger | Called by Cloud Functions when a feedback is stored in the database |
feedback-analyze-rule |
Rule | Links the trigger feedback-analyze-trigger with the sequence feedback-analyze-sequence |
{: java}
-
From the root of the checkout directory, compile the actions code {: java}
./android/gradlew -p actions clean jar
{: pre} {: java}
{: java}
-
Copy template.local.env to local.env
cp template.local.env local.env
-
Get the credentials for Cloudant, Tone Analyzer, Push Notifications and App ID services from the IBM Cloud dashboard (or the output of the bx commands we ran before) and replace placeholders in
local.env
with corresponding values. These properties will be injected into a package so that all actions can get access to the database. -
Deploy the actions to Cloud Functions.
deploy.sh
loads the credentials fromlocal.env
to create the Cloudant databases (users, feedback and moods) and deploy the Cloud Functions artifacts for the application../deploy.sh --install
{: pre}
You can use
./deploy.sh --uninstall
to remove the Cloud Functions artifacts once you have completed the tutorial. {: tip}
{: #mobile_app}
Our Cloud Functions actions are ready for our mobile app. Before running the mobile app, you need to configure its settings to target the services you created.
-
With Android Studio, open the project located in the
android
folder of your checkout directory -
Edit
android/app/src/main/res/values/credentials.xml
and fill in the blanks with values from credentials. You will need the App IDtenantId
, the Push NotificationappGuid
andclientSecret
and the organization and space names where the Cloud Functions have been deployed. -
Build the project
-
Start the application on a real device or with an emulator. For the emulator to receive push notifications, make sure to pick an image with the Google APIs and to log in with a Google account within the emulator. {: tip}
-
Watch the Cloud Functions in the background
bx wsk activation poll
{: pre}
-
In the application, select Log in to authenticate with a Facebook or Google account. Once logged in, type a feedback message and press the Send Feedback button. Few seconds after the feedback has been sent, you should receive a push notifications on the device. The notification text can be customized by modifying the template documents in the
moods
database in the Cloudant service instance. Use the View token button to inspect the access and identification tokens generated by App ID upon login. {: java} -
Push client SDK and other SDKs are available on CocoaPods and Carthage. For this solution, let's use CocoaPods
-
Open Terminal and
cd
intofollowupapp
folder. Run the below command to install the required dependencies.pod install
{: pre}
-
Open the file with
.xcworkspace
extension located under thefollowupapp
folder of your checkout directory to launch your code in Xcode. -
Edit
BMSCredentials.plist file
and fill in the blanks with values from credentials. You will need the App IDtenantId
, the Push NotificationappGuid
andclientSecret
and the organization and space names where the Cloud Functions have been deployed. -
Build the project.
-
Start the application on a real device or with a simulator.
-
Watch the Cloud Functions in the background by running the below command on a Terminal.
bx wsk activation poll
{: pre}
-
In the application, select Log in to authenticate with a Facebook or Google account. Once logged in, type a feedback message and press the Send Feedback button. Few seconds after the feedback has been sent, you should receive a push notifications on the device. The notification text can be customized by modifying the template documents in the
moods
database in the Cloudant service instance. Use the View token button to inspect the access and identification tokens generated by App ID upon login. {: swift}
-
Use
deploy.sh
to remove the Cloud Functions artifacts:./deploy.sh --uninstall
{: pre}
-
Delete the Cloudant, App ID, Push Notifications and Tone Analyzer services from the IBM Cloud console.
-
App ID provides a default configuration to help with the initial set up of your identity providers. Prior to publishing your app, update the configuration to your own credentials. You will also be able to customize the login widget.
-
When you create an OpenWhisk Swift action with a Swift source file(.swift files under
actions
folder), it has to be compiled into a binary before the action is run. Once done, subsequent calls to the action are much faster until the container that holds your action is purged. This delay is known as the cold-start delay. To avoid the cold-start delay, you can compile your Swift file into a binary and then upload to OpenWhisk in a zip file. As you need the OpenWhisk scaffolding, the easiest way to create the binary is to build it within the same environment it runs in. refer Package an Action as a Swift executable for further steps. {: swift}