bluemix build a holidays notification app for android on ibm · the jars (ibmbluemix.jar,...

12
© Copyright IBM Corporation 2015 Trademarks Build a holidays notification app for Android on Bluemix Page 1 of 12 Build a holidays notification app for Android on Bluemix Register mobile devices to receive holiday notifications Arun Kumar Rachamalla Software Engineer, IBM IBM Harish Kumar Challa Senior Staff Software Engineer, IBM IBM Naga Praveena Palasamudram Advisory Software Engineer, IBM IBM 10 March 2015 Learn to develop an app for Android mobile devices to get notifications of upcoming holidays. The MyHolidays app lets users register their mobile device with this service. Registered users can receive notifications on their mobile device, at desired intervals (weekly, bimonthly, monthly), about upcoming holidays and plan vacations accordingly. Examples show how to use Push, Mobile Data, Mobile Application Security, and Workload Scheduler IBM Bluemix services to configure and deploy the Android mobile app. Learn how to build an app for Android mobile devices that notifies registered users about upcoming holidays in the next month. The app lets a user register their mobile device with IBM® Bluemix™ services. The app also has a facility to search for holidays during a specific time period, which is helpful when planning vacations. In many companies, the human resources department sends a list of holidays in Excel format at the beginning of the year. It can be very cumbersome to open a spreadsheet like this on an Android mobile device to view all of the holidays for different geographies. Filtering the list on the mobile device is also cumbersome (for example, when you need to see upcoming holidays in December and you aren't carrying your laptop).

Upload: others

Post on 22-Oct-2019

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

© Copyright IBM Corporation 2015 TrademarksBuild a holidays notification app for Android on Bluemix Page 1 of 12

Build a holidays notification app for Android onBluemixRegister mobile devices to receive holiday notifications

Arun Kumar RachamallaSoftware Engineer, IBMIBM

Harish Kumar ChallaSenior Staff Software Engineer, IBMIBM

Naga Praveena PalasamudramAdvisory Software Engineer, IBMIBM

10 March 2015

Learn to develop an app for Android mobile devices to get notifications of upcoming holidays.The MyHolidays app lets users register their mobile device with this service. Registeredusers can receive notifications on their mobile device, at desired intervals (weekly, bimonthly,monthly), about upcoming holidays and plan vacations accordingly. Examples show how to usePush, Mobile Data, Mobile Application Security, and Workload Scheduler IBM Bluemix servicesto configure and deploy the Android mobile app.

Learn how to build an app for Android mobile devices that notifies registered users aboutupcoming holidays in the next month. The app lets a user register their mobile device with IBM®Bluemix™ services. The app also has a facility to search for holidays during a specific time period,which is helpful when planning vacations.

In many companies, the human resources department sends a list of holidays in Excel formatat the beginning of the year. It can be very cumbersome to open a spreadsheet like this on anAndroid mobile device to view all of the holidays for different geographies. Filtering the list on themobile device is also cumbersome (for example, when you need to see upcoming holidays inDecember and you aren't carrying your laptop).

Page 2: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

developerWorks® ibm.com/developerWorks/

Build a holidays notification app for Android on Bluemix Page 2 of 12

We'll show you how to build an app that solves these problems by letting you view the list ofholidays before the start of every month through a notification. You can also search the holidaysscheduled for any month.

The app, which is built using the Mobile Cloud starter on Bluemix, uses the following Bluemixservices:

• Mobile Data• Push• Mobile Application Security• Workload Scheduler

The app has two parts: Client and server. The client contains the user interface (UI) that lets usersregister and specify when they want to receive notifications (weekly or monthly). The UI also letsusers view holidays within a specific time period. The code is under the client folder.

The server has two components:

• The code that exposes, or defines, the REST APIs for sending out notifications weekly andmonthly. Each of these methods gets the list of holidays and feeds them as input to thesendNotificationByTags method.

• Two scheduler processes: One that runs once a week and the other that runs once a month.Each scheduler process calls the required REST API (defined in app.js), weekly or monthly, tosend out notifications to users with the appropriate holiday list.

The holiday dates are stored in a CSV file, which is placed in the public folder under server code(in our case, Holidays.csv). The same is loaded into the mobile data (service) with the class nameHolidays.

What you need to build your application• The Android SDK (configured in Step 1)

Page 3: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

ibm.com/developerWorks/ developerWorks®

Build a holidays notification app for Android on Bluemix Page 3 of 12

• Node.js: Optional, though you need it to run your app locally• Cloud Foundry (CF) command line tools• A Bluemix account• A DevOps Services account• Familiarity with the Android development environment

READ: Getting started with the Android SDK

Run the appGet the code

Step 1. Configure Eclipse with ADTDownload and configure Eclipse with Android Developer Tools (ADT) from Android.

1. Open Eclipse and select Window > SDK Manager. Select the Android SDK platform-toolsand Android platform. Our project is based on Android 4.4.2, API 19.

2. Select everything under Android 4.4.2 (API19).3. Choose the Google Play Services package under Extras.4. In the SDK Manager, under Extras, select Intel x86 emulator accelerator (HAXM installer).5. Click INSTALL.

Note: Though you are installing from sdkmanager, it downloads the HAXM installer but willnot install it. You need to manually run the installer available under your adt-bundle\sdk\extras\intel folder.

6. Configure a mobile emulator. Click Window > Android virtual device manager and add amobile emulator. This step must be performed properly for your emulator to be able to receivethe notifications.

Download the code1. Sign in to DevOps Services with your IBM ID.2. Go to Go to MyHolidays.3. Click FORK PROJECT and give your forked project a name. Before clicking CREATE,

remember to write down or copy the URL of your project.4. Get Git if you do not already have it installed. From Git, open a command prompt, cd to the

directory where you want to download the code, then run the following command:git clone <url to your forked project>

5. Install Node.js if you don't already have it. Node.js allows you to test the app on your localenvironment.

6. Install npm (a package manager).

Step 2. Create the app and configure services1. Log in to Bluemix and create an app:

1. Select Create an App.2. Select MOBILE for the kind of application.3. Select Android as the platform.4. Give a sensible name to your application, then click Finish.

Page 4: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

developerWorks® ibm.com/developerWorks/

Build a holidays notification app for Android on Bluemix Page 4 of 12

5. Select ANDROID and click Continue.

Your app is created with the services shown below bound to it.

2. Select Add Service and add the Workload Scheduler Beta service to your app.

Page 5: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

ibm.com/developerWorks/ developerWorks®

Build a holidays notification app for Android on Bluemix Page 5 of 12

3. You might see a screen similar to the one below. Ignore the error message and click CLOSE.

4. Go back to your app. Open the Mobile Data Service, click the Manage Data tab, and importyour holiday list CSV by providing the class name Holidays. A sample CSV file calledHolidays.csv (with the expected format) is provided in the code under server\public directory.This file creates a class under mobile data that is used by the Get Holidays interface in the UI(in the client code) to fetch the holidays occurring in a specific time period.With this app you can also add data into the mobile data, such as saving the mobile numberand location for which the user wants to be notified. (See RegistrationActivity.java in the clientcode.)The Holidays.csv file used in the app has the following fields:

• Serial• HolidayName• dateString• day• type• location

If you plan to use your own Holidays.csv file, make sure it is in the following format:

Page 6: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

developerWorks® ibm.com/developerWorks/

Build a holidays notification app for Android on Bluemix Page 6 of 12

Configure the Bluemix Push service

Notifications are sent to registered users by the Workload Scheduler using the IBM Push service.To configure your Push service, go to the tutorial Extend an Android app using the IBM Push forBluemix cloud service and follow the steps in these sections:

• Before you start: Get your Google API Project Number and GCM API Key needed for Push• Add Google Play Services to your project (Skip this step if using Android Studio)• Add IBM Push and IBM CloudCode client SDK JARs to your project (skip this step if using

Android Studio)• Add the Google API Project Number and GCM API Key into your application in Bluemix

Note: You can ignore the step to edit the app.js to provide the actual application ID and route. Ourproject has these configured in the appconfigs.js file (under the server folder in the code).

Create the tags weeklybangalore, weeklyhyderabad, monthlybangalore, and monthlyhyderabad.The tags are necessary because the app needs to send weekly notifications for tags starting withweekly and monthly notifications for tags starting with monthly. We are appending the locationnames to confirm the holiday list for that particular location is correct. For example, when a usersubscribes for weekly notifications with location Bangalore, the user or mobile device is subscribedto the weeklybangalore tag. (This piece of code, where the user registers to push notification andsubscribe to these tags, is available in RegistrationActivity.java).

If your Holidays.csv file has a location different from those in the sample Holidays.csv file,create the tags weekly<locationname1>, weekly<locationname2>, monthly<locationname1>,monthly<locationname2>, and so on. When you change these tag names, make sure you makethe necessary changes to:

• Your Android app: Make changes in RegistrationActivity.java by providing the location namesand frequency at which notifications are sent.

• App.js: Change the tag name that is passed in the req.push.sendNotificationByTag method.

You can also extend the app with a few more tags and subscribe for those tags -- for example,bi-monthly<locationname> so that notifications are sent bimonthly, or monthlyall so holidays for

Page 7: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

ibm.com/developerWorks/ developerWorks®

Build a holidays notification app for Android on Bluemix Page 7 of 12

all locations are sent out on a monthly basis. This requires changes to your Android app, app.js(same as above), and also to the Workload Scheduler. You need to have one more schedulerprocess for a new frequency at which notifications are sent out (bimonthly, quarterly, and so forth).

Step 3. Configure and run the app (client)

To get the code working for your sample app, on the client side:

1. Open Eclipse and import the Android project under the client folder of the downloaded code.2. Update the src/client/assests/myHolidays.properties file with the applicationID,

applicationSecret, and applicationRoute values from your Bluemix dashboard.3. Download https://mbaas-catalog.ng.bluemix.net/sdk/ibm-bluemix-sdk-android.zip. Put this

URL in your browser and you will see a pop-up asking you to save the file. Unzip it and copythe JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folderof your Android project.

4. Complete the following steps to enable the Push service:a. Import the Google Play Services library into your workspace from

ADT_installation_dir\sdk\extras\google\google_play_services.Click File > Import > Android > Existing Android Code into the Workspace andbrowse to the location of the library project to import it. Select the option to Copy projectsinto the workspace.

b. Add a reference to the Google Play Services library to your app. Right-click projectand select Properties > Android > Library > Add (to add the Google Play Servicesreference from your project workspace).

5. Build the code and run your app on your Android Virtual Device (AVD). Click Register toregister your device to receive notifications either weekly or monthly.

You can also select Get Holidays to view the list of holidays for the chosen location. Currently, thecode displays all the holidays in the year for a particular location. You can modify the code to listthe holidays only for the next 30 days or so.

After you have registered, go to the Push service on your Bluemix console and check if yourmobile device was registered. Try to send out notifications to the registered mobile devices.

Go to the Mobile Data service on the Bluemix console, go to Manage data, and you can see thatthe new class Registrations was created and contains the information with which you registered.

Step 4. Configure and run the app (server)

On the server side:

1. Modify the appconfig.json file with your app's applicationId and route.2. Optional: If you are not using the sample Holidays.csv file provided with this tutorial, upload or

copy your holidays.csv file into the public folder under the node directory. (This file should bethe same one you uploaded in Step 2 into the Mobile Data service.)

Page 8: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

developerWorks® ibm.com/developerWorks/

Build a holidays notification app for Android on Bluemix Page 8 of 12

3. Modify manifest.yml with your app's name and route.4. Push and deploy your changes to Bluemix.

Deploy your changes to Bluemix

To deploy your Node.js app in the Bluemix environment, use any of these methods:

• The Eclipse plugin (see How to connect to Bluemix using Eclipse Cloud Foundry plugin).• The deployer in DevOps Services (see Developing IBM Bluemix applications in Java with

Eclipse and DevOps Services).• The cf tool command. From your server directory, where the app.js file is located, issue these

commands:cf login –a api.ng.bluemix.net

(provide your bluemix credentials)

cf push <your application name>

Your app should now be running on Bluemix. Test it in your browser by providing the URL.

Step 5. Configure the Workload Scheduler service

1. On your Bluemix console, open the Workload Scheduler service.

Page 9: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

ibm.com/developerWorks/ developerWorks®

Build a holidays notification app for Android on Bluemix Page 9 of 12

2. Ignore the error message and click CLOSE. You should see a screen similar to the one below.

Wait until your subscription is provisioned; it might take a few minutes.3. After your subscription is provisioned, you no longer see the highlighted messages. Select

Manage your processes > Manage simple processes.

Page 10: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

developerWorks® ibm.com/developerWorks/

Build a holidays notification app for Android on Bluemix Page 10 of 12

4. Create two processes: One for monthly schedules and another for weekly schedules.

5. Under the Triggers tab, create a trigger that specifies when this process should run.

Page 11: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

ibm.com/developerWorks/ developerWorks®

Build a holidays notification app for Android on Bluemix Page 11 of 12

6. Go to the Steps tab and create a new step. Double-click it, and select RESTful and youragent.

7. Leave the Authentication field blank unless you specified any authentication mechanisms foryour app in the Mobile Application Security service.

8. In the Action field, provide the serviceuri as https://mobile.mybluemix.net/${appHostName}/v1/apps/${applicationId}/monthlyNotifications/ (depending upon whether you are scheduling it formonthly or weekly).

9. Select POST for the method.10. In HTTP Query Parameters, provide your app's secret:

"applicationSecret=<application secret>

11. Nothing is needed in the Body and Advanced fields. Click OK.12. Enable your process.13. To verify that everything is good to go, click Run Now.

Step 6. See your app running

View your notifications on your AVD (emulator) or on the mobile device where you installed yourapp.

Conclusion

This tutorial showed how to create a simple Android-based mobile app that can register mobiledevices and run a scheduler at the back end to push notifications regularly to registered users.

We showed how to configure and deploy an Android mobile app using four of the Bluemixservices.

Page 12: Bluemix Build a holidays notification app for Android on IBM · the JARs (ibmbluemix.jar, ibmcloudcode.jar, ibmpush.jar, and ibmdata.jar) into the libs folder of your Android project

developerWorks® ibm.com/developerWorks/

Build a holidays notification app for Android on Bluemix Page 12 of 12

The Push servicehttp://www.ibm.com/developerworks/topics/push%20serviceenables youto send relevant content to the right people at the right place and time.The Mobile Dataservicehttp://www.ibm.com/developerworks/topics/mobile%20data%20serviceprovides easy-to-use SDKs that give access to a scalable, fully managed database via familiar object-orientedAPIs.The Mobile Application Security servicehttp://www.ibm.com/developerworks/topics/mobile%20application%20security%20servicehelps you manage application access and implementa basic application security framework for your app.The Workload Scheduler servicehttps://console.ng.bluemix.net/#/store/cloudOEPaneId=store&serviceOfferingGuid=29e39fa8-644f-4d7c-a230-43612f84f84eprovides a flexible job scheduling service for both one-time and recurring tasks.© Copyright IBM Corporation 2015(www.ibm.com/legal/copytrade.shtml)Trademarks(www.ibm.com/developerworks/ibm/trademarks/)