In this course, we will develop an Android app using Java and implement the functionality to send notifications via KakaoTalk.
We plan to complete a feature that sends notifications to users when specific events occur using the KakaoTalk API.
This process will be explained based on Android Studio, and basic Android development knowledge is required.
Prerequisites
- Installation and setup of Java and Android SDK
- Installation of Android Studio
- Creation of a KakaoTalk developer account and app
- Verification of API Key and Secret Key
1. Android Studio Project Setup
Open Android Studio and create a new project. Select “Empty Activity” and complete the basic configuration before creating the project.
1.1 Gradle Setup
Next, open the build.gradle file of the app module and add the necessary libraries. We will be using Retrofit2 and Gson to utilize the KakaoTalk API.
dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
2. KakaoTalk API Integration
Create an application in the KakaoTalk Developer Center and verify the API Key. This key will be needed for future API calls.
2.1 Retrofit Setup
Add configuration to communicate with the API using Retrofit. Create a Retrofit instance and set up the necessary API interface.
public interface KakaoApi {
@FormUrlEncoded
@POST("/v2/api/talk/memo/default/send")
Call sendMessage(@Header("Authorization") String token,
@Field("template_object") String templateObject);
}
2.2 Implement Method for API Call
Implement a method to invoke the KakaoTalk API to send messages. OAuth authentication is required to authenticate the sender.
public class KakaoService {
private static final String BASE_URL = "https://kapi.kakao.com";
private KakaoApi kakaoApi;
public KakaoService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
kakaoApi = retrofit.create(KakaoApi.class);
}
public void sendKakaoTalk(String accessToken, String message) {
String jsonTemplate = "{\"object_type\":\"text\",\"text\":\"" + message + "\"}";
Call call = kakaoApi.sendMessage("Bearer " + accessToken, jsonTemplate);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
Log.d("KakaoTalk", "Message sent successfully");
} else {
Log.d("KakaoTalk", "Message sending failed: " + response.message());
}
}
@Override
public void onFailure(Call call, Throwable t) {
Log.d("KakaoTalk", "Error occurred: " + t.getMessage());
}
});
}
}
3. User Interface Implementation
Create a simple UI to receive input from the user. Use EditText and Button to implement the functionality to input and send messages.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/messageInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter message"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
3.1 Connect UI to Activity
Connect UI elements to the Activity to send a KakaoTalk message when the button is clicked.
public class MainActivity extends AppCompatActivity {
private KakaoService kakaoService;
private EditText messageInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageInput = findViewById(R.id.messageInput);
Button sendButton = findViewById(R.id.sendButton);
kakaoService = new KakaoService();
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = messageInput.getText().toString();
String accessToken = "YOUR_ACCESS_TOKEN"; // Enter the access token here
kakaoService.sendKakaoTalk(accessToken, message);
}
});
}
}
4. Testing and Deployment
Run the app, enter a message, and click the ‘Send’ button to check if the message is sent via KakaoTalk.
If there are no issues, you can distribute it as an APK file for use on other devices.
Through this course, you have been able to implement a feature that directly sends notifications from an Android app using the KakaoTalk API.
Future additional features may include considering automating the sending of KakaoTalk messages by catching user phone notifications.
Create an app that communicates with more users through these features!