Java is generally used for larger, enterprise ready projects.
This example details sending a message in plain old Java with no required libraries.
It includes a messageId which can be used by your code to identify conversational context (threading).
If you do not specify a messageId when using the API, the default will be applied and you will be unable to thread messages.
Plan of attack
This example can serve as library functions for your later project
It is not served up by a web server, it is just a piece of plain old Java that just needs a JDK installed to compile and run.
What we'll do therefore is:
Write the code
Compile the example
Run the example
Have a pat on the back - you've integrated the Messenger API with Java
Write the code
Open up a text editor and paste the code below:
Save the file as SimpleSend.java
The code has 3 methods.
The main method kicks off the sending and checks to see that you have specified the messageId on the command line.
The send method prepares an HTTP post response, connects to the API and transmits the payload
The encodeBasicAuth method Base64Encodes the authentication credentials as per the HTTP standard
Compile the code
Open up a terminal (Unix/Linux) or cmd.exe (Windows)
Run the following:
This should produce SimpleSend.class in the current folder
Send a Message
In the same terminal as above type:
java SimpleSend <messageId>
A message should arrive on your handset.
Your should see a 204 result string the console:
Result code: 204
Hurray! You have successfully sent your first Messenger API Message using some Java.
Now you can lift this code and use it in your software, either standalone or web. See the receiving messages tutorial to pickup the message.