티스토리 뷰
[개발/Android] - 카카오 sdk v2 굳이 자바로 사용하기 -1 (카카오 로그인)
전 포스팅 로그인에 이어서 카카오 메시지 보내는 방법을 자바 코드로 작성하였습니다.
1. kotlin으로 카카오 링크 메시지 보내기
class KakaoLink {
companion object {
@JvmStatic
val instance by lazy { KakaoUtil() }
}
fun kakaoLink(context : Context) {
val defaultFeed = FeedTemplate(
content = Content(
title = "타이틀",
description = "메시지 내용",
imageUrl = "이미지 경로",
link = Link(
webUrl = "웹 링크",
mobileWebUrl = "모바일 링크"
androidExecParams = mapOf("1" to "안드로이드 폰 링크"),
iosExecParams = mapOf("1" to "ios 폰 링크")
)
)
)
LinkClient.instance.defaultTemplate(context, defaultFeed) { linkResult, error ->
if (error != null) {
Log.e("TAG", "카카오링크 보내기 실패", error)
}
else if (linkResult != null) {
context.startActivity(linkResult.intent)
// 카카오링크 보내기에 성공했지만 아래 경고 메시지가 존재할 경우 일부 컨텐츠가 정상 동작하지 않을 수 있습니다.
Log.w("TAG", "Warning Msg: ${linkResult.warningMsg}")
Log.w("TAG", "Argument Msg: ${linkResult.argumentMsg}")
}
}
}
}
2. Java로 카카오 링크 메시지 보내기
public class TestKakaoMsg {
public void kakaoLink(Context context) {
/**
* new Link() : 파라미터 순서대로 webLink, mobileLink, androidLink, iosLink
*/
FeedTemplate feedTemplate = new FeedTemplate(new Content("title","imageUrl", //메시지 제목, 이미지 url
new Link("https://www.naver.com"),"description", //메시지 링크, 메시지 설명
300,300)); //이미지 사이즈
LinkClient.getInstance().defaultTemplate(context, feedTemplate,null,new Function2<LinkResult, Throwable,Unit>() {
@Override
public Unit invoke(LinkResult linkResult, Throwable throwable) {
if (throwable != null) {
Log.e("TAG", "카카오링크 보내기 실패", throwable);
}
else if (linkResult != null) {
context.startActivity(linkResult.getIntent());
// 카카오링크 보내기에 성공했지만 아래 경고 메시지가 존재할 경우 일부 컨텐츠가 정상 동작하지 않을 수 있습니다.
Log.w("TAG", "Warning Msg: "+ linkResult.getWarningMsg());
Log.w("TAG", "Argument Msg: "+ linkResult.getArgumentMsg());
}
return null;
}
});
}
}
반응형
'개발 > Android' 카테고리의 다른 글
카카오 sdk v2 굳이 자바로 사용하기 -1 (카카오 로그인) (0) | 2021.02.20 |
---|
댓글