[Java] Send regular notifications with LineNotify+SpringBoot

2 minute read

at first

When I was creating a web application with SpringBoot, I would like to periodically notify you of specific information! !! !! As a result of examining the thinking method, I was able to implement it unexpectedly easily, so I summarized it.

Execution environment

  • java: 11
  • Spring Boot: 2.4.0
  • Maven: 3.6.3

① Get access token on LINE Notify official page

Access the following URL, scan the displayed QR code, and add LINE Notify as a friend.

https://notify-bot.line.me/ja/

After that, after logging in with your own LINE account on the same page, proceed 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 is displayed * only once*. However, please be assured that it can be issued as many times as you want.

② Create a class to send notifications

Create a Bean that is registered in the DI container to skip the notification 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 = "The 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 a notification with LINE Notify, but authentication is executed by adding Authorization: Bearer <access_token> to the request header in the method.

③ Settings for sending notifications regularly

To enable the scheduled task execution function Use the @EnableScheduling annotation for your main class.

Application.java



@SpringBootApplication
@EnableScheduling //... added
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

This allows you to find any bean in the DI container that uses the @Scheduled annotation. So, go back to the class that implemented the notification function and add annotations to the method you want to periodically execute.

LineNotify.java



@Component
public class LineNotify {
    @Scheduled(cron = "0 * * * * *", zone = "Asia/Tokyo") // Add
    public void executeNotification() {
         String message = "message you want to send";
         String token = "The obtained token";
        
sendNotification(message,token);
}

     ...

Once you have done this, all you have to do is start the server and the task will be executed and you will receive the notification.

cron specifies the cycle to execute the task, and zone specifies the time zone. cron can set the trigger of “second, minute, hour, month, day, day of the week” from the left, and the sample code above is set to execute the task at 0 seconds per minute. For example, if you want to execute a task at 9:00:00 every day, Set it like “cron = "0 0 9 * * *"”.

There are other options such as initialDelay that specifies execution after n seconds from the start of the application, and fixedDelay that specifies execution after n seconds from completion of method processing, so you can customize it relatively freely. can do.

reference

・Notification by LINE Notify from Java implemented in 5 minutes ・How to execute task periodically in Spring Boot