前情提要
Line Notify API 串接实作-前置作业
Line Notify API 串接实作(一)-取得access token
根据我们上篇取得使用者的access token后,我们就可以利用这个token对使用者推播讯息,Line notify除了文字讯息可以推播外,还可以推送贴图、图片。
讯息推播
POST https://notify-api.line.me/api/notify
Content-Type: application/x-www-form-urlencoded OR multipart/form-data
Authorization: Bearer <access_token>
请求参数
回应参数-表头
回应参数-body内容
以下是利用postman测试
程式码
notify 方法
public static void notify(String token,String message) {//设定表头参数HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);headers.setBearerAuth(token);//设定请求body参数MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();params.add("message", message);HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params,headers);//传送给line serverLineNotifyResponse response = restTemplate.postForObject( LineNotifyUrl.NOTIFY_MSG.getUrl(), entity, LineNotifyResponse.class);if(response.getStatus()!=HttpStatus.OK.value()) {throw new LineNotifyUtilException(LineNotifyErrorEnum.MESSAHE_SEND_ERROR.getError(),LineNotifyErrorEnum.MESSAHE_SEND_ERROR.getMessage());}}
LineNotifyResponse物件
@Datapublic class LineNotifyResponse {private Integer status;private String message;}
接下来我会再讲解line notify还有提供API去检查使用者token的授权状况和可以无效使用者token
这也会是line notify API解说的最后一章
参考资料
Line Notify Document