I want to be notified of specific information regularly when creating a web application with SpringBoot! !! !! As a result of investigating the method of thinking, I was able to implement it unexpectedly easily, so I summarized it.
Access the URL below, read the displayed QR code, and add LINE Notify as a friend.
https://notify-bot.line.me/ja/
After that, after logging in with your LINE account on the same page, go to "My Page" → "Issue Access Token". Enter the token name (displayed at the top of the message when the message is actually notified on LINE) and select any talk room to display the token.
Please note that the token will be displayed *** once ***. However, you can issue as many as you like, so don't worry about that.
Create a bean to register in the DI container to send notifications to LINE. There are no special dependencies required.
LineNotify.java
@Component
public class LineNotify {
public void executeNotification() {
String message = "Message you want to send";
String token = "Obtained token";
sendNotification(message,token);
}
public void sendNotification(String message, String token) {
HttpURLConnection connection = null;
try {
URL url = new URL("https://notify-api.line.me/api/notify");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Authorization", "Bearer " + token);
try (OutputStream outputStream = connection.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream)) {
writer.append("message=").append(URLEncoder.encode(message, "UTF-8")).flush();
}
} catch (Exception e) {
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
Pass the message and token you want to send to the argument of the sendNotification method, and run POST communication to the specified URL. OAuth authentication is required to send notifications with LINE Notify, but authentication is executed by adding ʻAuthorization: Bearer <access_token>` to the request header in the method.
To enable the scheduled task execution function
Use the @EnableScheduling
annotation on the main class.
Application.java
@SpringBootApplication
@EnableScheduling //···add to
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
This allows you to discover any bean in a DI container that uses the @Scheduled
annotation.
So, go back to the class that implemented the notification function earlier, and add annotations to the methods that you actually want to execute periodically.
LineNotify.java
@Component
public class LineNotify {
@Scheduled(cron = "0 * * * * *", zone = "Asia/Tokyo") //···add to
public void executeNotification() {
String message = "Message you want to send";
String token = "Obtained token";
sendNotification(message,token);
}
...
Once this is done, all you have to do is start the server and the task will be executed and you will be notified.
cron
specifies the cycle for executing tasks, and zone
specifies the time zone.
From the left, cron
can set triggers for" seconds, minutes, hours, months, days, days of the week ", and the sample code above is set to execute tasks at 0 seconds per minute. For example, if you want to execute a task every day at 9:00:00
Set something like " cron =" 0 0 9 * * * "
".
There are other options such as ʻinitialDelay that specifies the execution n seconds after the application starts, and
fixedDelay` that specifies the execution n seconds after the execution of the method process is completed, so you can customize it relatively freely. can do.
Notification by LINE Notify from Java implemented in 5 minutes ・ How to execute tasks regularly with Spring Boot
Recommended Posts