Line Notify API 串接实作(二)-推播讯息

前情提要
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>

请求参数

图一

如果imageThumbnail有放值 则imageFullsize也需要放值,可以放不同照片,但会依据我下方的优先顺序只显示 一张。三者的显示优先顺序是 imageFile > imageThumbnail > imageFullsize。

回应参数-表头
图二

回应参数-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


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章