Java
This is walkthrough showing you how to connect Live Link 365's SMS sending API to a flight booking app. This sample creates the model of a flight's booking information and sends an SMS notification to a phone number.
This is not a production-ready application. Please take your time to enhance it for production so that it meets your specific business requirements.
Steps
Create the Java class for CustomerDetails
using the customerFirstName, customerLastName , customerMobNo and cUserName:
- customerFirstName: String
- customerLastName: String
- customerMobNo: String
- cUserName: String
CustomerDetails
class
-
Create the Java class for FlightInformation using fromCity, toCity, departureTime, arrivalTime, noOfSeatsBooked, totalNoOfSeats, availableSeats, newDeptTime.
- fromCity: String
- toCity: String
- departureTime: String
- arrivalTime: String
- noOfSeatsBooked: String
- totalNoOfSeats: String
- availableSeats: String
- newDeptTime: String
-
Create the getter/setter methods to access the attributes of the
FlightInformation
class - Add searchFlight function to search available
-
Add
getAvailableSeat()
function returns available seats with FlightID using noOfSeatsbooked and availableSeats variables and books the flight usingbookFlight()
function -
Add
bookFlight(FlightInformation finalFlight)
function with final selected flight finalFlight object -
Call
AddCustomer()
function within bookFlight() function to assign flight to that customer and send SMS -
Add
modifyDepartTime()
function to add delayed time of flights -
Add
checkIfFlightIsDelayed()
function to check if flight is delayed and send SMS
The config.properties
file acts as a temporary storage for records and holds key information such as: customer information (Name, Contact No, etc.), flight information (FlightID, fromCity, toCity, ArrivalTime, DepartureTime) and so on.
The properties file can be replaced by a database which holds the Flight
and Customer
information
Before you can use the API for sending SMS, you have to fetch an authentication token, by providing:
- App key
- App secret
getAccessToken()
method will take the app key and secret and combine them into a Base64 string, which will then be sent to the /api/oauth/token
endpoint to generate an authentication token For this step we have created two methods:
getAccessToken()
: This is the method that takes your app key and secretpostForAccessToken()
: A method for making the POST request. Please notice that you will need to parse the JSON string that the method returns before using it. After parsing the JSON string, you should return theaccess_token
field from the JSON response
On this step we create the methods for sending an SMS, there are two parts to it:
- The
post()
method: makes an HTTP post request, given the url and a string with the data in a JSON format - The
sendSMS()
takes your message string, your origin phone number, the destination phone number and the authentication token generated by the method from a previous step
Now we finally call the main function, connecting the booking classes to the sending API. Replace in the code:
- appKey: An app key
- appSecret: An app secret
- originNumber: Your origin phone number
- destinationNumber: The phone number you want to send the message to
MessageHandler.sendSMS()
method. See this site's Authentication section for details on how to get an app key and a secret
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|