프로그래밍

applink-export-to-kakaotalk-카카오톡으로-앱링크-내보내기

tedhong 2023. 2. 9. 13:41
2012-12-19 글쓴이 TED HONG

applink-export-to-kakaotalk-카카오톡으로-앱링크-내보내기

AppLink Export to KakaoTalk 카카오톡으로 앱링크 내보내기

 

보내는 메소드 :

/**
* Send App data
*/
public static void sendAppDataToKakaoTalk(Context context, Activity act, String msg) throws NameNotFoundException {

String str = "testString";
ArrayList metaInfoArray = new ArrayList();

// If application is support Android platform.
Map metaInfoAndroid = new Hashtable(1);
metaInfoAndroid.put("os", "android");
metaInfoAndroid.put("devicetype", "phone");
metaInfoAndroid.put("installurl", "market://details?id=com.yourapp");
metaInfoAndroid.put("executeurl", "tedhome://"+str);

// If application is support ios platform.
Map metaInfoIOS = new Hashtable(1);
metaInfoIOS.put("os", "ios");
metaInfoIOS.put("devicetype", "phone");
metaInfoIOS.put("installurl", "itms-apps://itunes.com/apps/yourapp");
metaInfoIOS.put("executeurl", "tedhome://"+str);

// add to array
metaInfoArray.add(metaInfoAndroid);
metaInfoArray.add(metaInfoIOS);

// Recommended: Use application context for parameter.
KakaoLink kakaoLink = KakaoLink.getLink(context);

// check, intent is available.
if(!kakaoLink.isAvailableIntent()) {
// alert("Not installed KakaoTalk.");
errorDialog("카카오톡이 설치되지 않았습니다.", context);
return;
}

/**
* @param activity
* @param url
* @param message
* @param appId
* @param appVer
* @param appName
* @param encoding
* @param metaInfoArray
*/
kakaoLink.openKakaoAppLink(
act,
"http://tedhome.net",
"Ted's Story",
context.getPackageName(),
context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName,
context.getResources().getString(R.string.app_name),
"UTF-8",
metaInfoArray);

}

Manifest.xml

<application

android:icon=<em>"@drawable/ic_launcher"</em>

android:label=<em>"@string/app_name"</em>

android:theme=<em>"@android:style/Theme.Light.NoTitleBar.Fullscreen"</em> &gt;

&lt;activity android:name=<em>".TestActivity"</em> &gt;

&lt;intent-filter&gt;

&lt;action android:name=<em>"android.intent.action.MAIN"</em> /&gt;

&lt;category android:name=<em>"android.intent.category.LAUNCHER"</em> /&gt;

&lt;/intent-filter&gt;

&lt;!-- custom scheme(execute url) --&gt;

&lt;intent-filter&gt;

&lt;data android:scheme=<em>"tedhome" </em>android:host=<em>"testString"</em>/&gt;

&lt;action android:name=<em>"android.intent.action.VIEW"</em> /&gt;

&lt;category android:name=<em>"android.intent.category.BROWSABLE"</em> /&gt;

&lt;category android:name=<em>"android.intent.category.DEFAULT"</em> /&gt;

&lt;/intent-filter&gt;

&lt;/activity&gt;

&lt;/application&gt;

 

 

다른 것은 그리 어렵지 않다.
다만 좀 헷갈리는 부분은

“executeurl”, “tedhome://”+str

여기다.

excuteurl 은 manifest에 등록되는 Intent Filter의
android:scheme=“tedhome” android:host=“testString”
와 대응하는 값이며

scheme://host 의 형식이다.

scheme 는 일종의 프로토콜이어서 보내는 쪽의 것과
Manifest 쪽의 내용이 동일해야 한다.

그러나 확인 해본 결과  host 부분은 없어도 앱실행엔 이상이 없었다.
(쉽게 말해 android:host=“testString” 부분은 지워도 된다는 얘기)

나는 host 부분에 간단한 String 값을 넣어 보낸 뒤
수신측에서 해당 값을 파싱해 사용했다.
(쇼핑앱에서 사용하기 때문에 딜id 값을 보내서 해당 딜로 이동 할 수 있게 했다.)

수신측의 소스는 다음과 같다.

onCreat 부분에

Intent intent = getIntent();

<strong>if</strong>(Intent.<em>ACTION_VIEW</em>.equals(intent.getAction())) {

Uri uri = intent.getData();

String uriStr = uri.toString();

uriStr =  uriStr.replace("tedhome://", "");

String fromKakaoStr = uriStr.substring(0,uriStr.indexOf("?"));

<em>Log.i</em>("test","fromKakaoStr ="+fromKakaoStr);

}

LOG  : fromKakaoStr= testString

 

intent 에서 추출해낸 uri 를 문자열로 변경하면

“tedhome://testString?….. 어쩌구 저쩌구…. ”

의 형식이라 replace 와 substring 으로 testString 값만 추출할 수 있다.

 

이 원리만 알면 카톡에 웹링크 내보내기나 카카오스토리로 내보내기는
쉽게 이해 할 수 있다.

 

끝~