C2DM이란 안드로이드 2.2 프로요 버전 부터 푸시 서버를 제공해 주는 기능이다.
이하 소스는 http://www.androidside.com/bbs/board.php?bo_table=B46&wr_id=14705
에서 소스를 받아 수정 하였다.
푸시서버를 사용하기 위해서는 다음과 같은 절차가 필요하다.
- 구글에 해당 서버 사용 인증 요청
http://code.google.com/intl/ko-KR/android/c2dm/signup.html에서 등록 요청 - 메일로 승인 확인
- 소스 코딩
C2dm_BroadcastReceiver.java
package com.coolsharp.push; import android.content.BroadcastReceiver; public class C2dm_BroadcastReceiver extends BroadcastReceiver { static String registration_id = null; /* 메시지 도착 handleRegistration(context, intent); } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { c2dm_msg = intent.getExtras().getString("msg"); System.out.println("c2dm_msg======>" + c2dm_msg); } /** registration_id = intent.getStringExtra("registration_id"); System.out.println("registration_id====>" + registration_id); if (intent.getStringExtra("error") != null) { Log.v("C2DM_REGISTRATION", ">>>>>" + "Registration failed, should try again later." + "<<<<<"); } else if (intent.getStringExtra("unregistered") != null) { Log.v("C2DM_REGISTRATION", ">>>>>" + "unregistration done, new messages from the authorized sender will be rejected" + "<<<<<"); } else if (registration_id != null) { System.out.println("registration_id complete!!"); } |
Coolsharp_pushActivity.java
package com.coolsharp.push; import java.io.BufferedReader; import android.app.Activity; public class Coolsharp_pushActivity extends Activity { // 아래 문자는 수정하면 안됨. @Override // 마지막 저장된 인증 msg_text = (EditText) findViewById(R.id.msg_text); msg_send.setOnClickListener(new OnClickListener() { /** postDataBuilder.append("registration_id=" + regId); // 등록ID byte[] postData = postDataBuilder.toString().getBytes("UTF8"); URL url = new URL("https://android.apis.google.com/c2dm/send"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); OutputStream out = conn.getOutputStream(); conn.getInputStream(); } /** StringBuffer postDataBuilder = new StringBuffer(); byte[] postData = postDataBuilder.toString().getBytes("UTF8"); URL url = new URL("https://www.google.com/accounts/ClientLogin"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); OutputStream out = conn.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String sidLine = br.readLine(); System.out.println("sidLine----------->>>" + sidLine); authtoken = authLine.substring(5, authLine.length()); return authtoken; } |
Util.java
package com.coolsharp.push; import android.content.Context; public class Util { public static boolean setSharedPreferencesValue(Context context, String name, int mode, String key, String value) { |
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <uses-sdk android:minSdkVersion="8" /> <application <category android:name="android.intent.category.LAUNCHER" /> <receiver <category android:name="com.coolsharp.push" /> <category android:name="com.coolsharp.push" /> <permission <uses-permission android:name="com.coolsharp.push.permission.C2D_MESSAGE" /> </manifest> |