본문 바로가기

개발도구/aOS - 안드로이드 개발

[안드로이드] Push 2 강의 - 2.2 이상 (C2DM 방식)

C2DM  방식은 2.2 - sdk 8 이상만 가능합니다. 
2.1 버전 아래는 제가 글 써논 푸시 1강의를 확인해 보시면 될듯 싶습니다.
2.2 버전 C2DM 방식은 자세히 써 내려 가려 합니다.  물론 소스를 완전 오픈할수는 없지만,
사용하시기에 불편함 없을정도일겁니다.

저도 검색을 통해 여러종류의 소스를 얻어 돌려봤는데 이 방법이 저에겐 맞더라구요..

Introduce
http://code.google.com/intl/ko-KR/android/c2dm/ 

푸시을 위해 아래 주소에 가입을 하고, 해당 주소 메일이 날라옵니다.


Java Intro.class(시작화면)
2.1 이전과 2.2(C2DM)이상의 버전 으로 if 문을 걸어 놧지요...:)

//C2DM 방식 채택
if(android.os.Build.VERSION.SDK_INT >= 8){
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
       registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
       registrationIntent.putExtra("sender", "이메일");
       startService(registrationIntent);
Log.v("sdk version", Integer.toString(android.os.Build.VERSION.SDK_INT));
 
}else{ // 기존방식 2.1 이하만 되도록..
//  int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
PendingIntent mAlarmSender = PendingIntent.getService(this,
            0, new Intent(this, NewsAlarmService.class), 0);
 
AlarmManager am = (AlarmManager)this.getSystemService(android.content.Context.ALARM_SERVICE);
       am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
       firstTime, Integer.parseInt(this.getText(R.string.pushTime).toString()), mAlarmSender);
      
}

Auth_android.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
 
public class Auth_android
{
    private static String HOST = "https://www.google.com/accounts/ClientLogin";
    private static String EMAIL = "메일주소";
    private static String PASS = "메일주소 비밀번호";
    private static String SOURCE = "알아보기 쉽게 적힌 프로그램 정보(예 : company-project-version. apple-iphone-4.2.1)";
     
 
    public  static void main( String[] args ) throws Exception
    {
        try {
            StringBuffer postDataBuilder = new StringBuffer();
            postDataBuilder.append("Email=" + EMAIL);
            postDataBuilder.append("&Passwd=" + PASS);
            postDataBuilder.append("&accountType=GOOGLE");
            postDataBuilder.append("&source=" + SOURCE);
            postDataBuilder.append("&service=ac2dm");
             
            byte[] postData = postDataBuilder.toString().getBytes("UTF8");
             
            URL url = new URL(HOST);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
            OutputStream out = conn.getOutputStream();
            out.write(postData);
            out.close();
            BufferedReader in = new BufferedReader(   newInputStreamReader(conn.getInputStream()));
 
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
             System.out.println(inputLine);
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }   
}
 
 
 

AndroidManifest.xml 에 추가하여 넣으세요

<?xml version="1.0" encoding="utf-8"?>
      package="mint.TestC2dm"
      android:versionCode="1"
      android:versionName="1.0"
      android:minSdkVersion="8">
       
    <permission android:name="mint.TestC2dm.permission.C2D_MESSAGE"android:protectionLevel="signature" />
    <uses-permission android:name="mint.TestC2dm.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     
     
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestC2dm"
                android:theme="@android:style/Theme.Black.NoTitleBar"
                android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <receiver android:name=".C2DMReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="mint.TestC2dm" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="mint.TestC2dm" />
            </intent-filter>
        </receiver>
         
        <activity android:name=".showMsg"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.AlertDialogs" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


TestC2dm.java
package mint.TestC2dm;
 
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
 
public class TestC2dm extends Activity {
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
     
    // 등록
    public void onRegist(View v)
    {
        Intent registrationIntent = newIntent("com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app", PendingIntent.getBroadcast(this0newIntent(), 0));
        registrationIntent.putExtra("sender""메일주소");
        startService(registrationIntent);
    }
  
    // 해지  
    public void onUnregist(View v)
    {
        Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
        unregIntent.putExtra("app", PendingIntent.getBroadcast(this0new Intent(),0));
        startService(unregIntent);
    }
}

 
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="가입"
        android:onClick="onRegist"/>
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="해지"
        android:onClick="onUnregist"/>
</LinearLayout>l
 

C2DMReceiver.java
package mint.TestC2dm;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
 
public class C2DMReceiver extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("###############""onReceive");
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(context, intent);
        else if(intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(context, intent);
         }
     }
 
    private void handleRegistration(Context context, Intent intent) {
        Log.e("###############""handleRegistration");
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
        else if (intent.getStringExtra("unregistered") != null) {
            Log.e("@@@@@@@@unregistered""unregistered");
        else if (registration != null) {
           // Send the registration ID to the 3rd party site that is sending the messages.
           // This should be done in a separate thread.
           // When done, remember that all registration is done.
            Log.e("@@@@@@@@registration_id", registration);
        }
    }
     
    private void handleMessage(Context context, Intent intent) {
        Log.e("###############""handleMessage");
        String title = intent.getStringExtra("title");
        String msg = intent.getStringExtra("msg");
         
    // 화면 깨우기
        PushWakeLock.acquireCpuWakeLock(context);
 
        Intent i = new Intent(context, showMsg.class);
        Bundle b = new Bundle();
        b.putString("title", title);
        b.putString("msg", msg);
        i.putExtras(b);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}
 
 
 

PushWakeLock.java
package mint.TestC2dm;
 
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.util.Log;
 
class PushWakeLock {
 
    private static PowerManager.WakeLock sCpuWakeLock;
    private static KeyguardManager.KeyguardLock mKeyguardLock;
    private static boolean isScreenLock;
 
    static void acquireCpuWakeLock(Context context) {
        Log.e("PushWakeLock""Acquiring cpu wake lock");
        if (sCpuWakeLock != null) {
            return;
        }
 
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
 
        sCpuWakeLock = pm.newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "I'm your father");
        sCpuWakeLock.acquire();
         
//        KeyguardManager km = (KeyguardManager)context.getSystemService(context.KEYGUARD_SERVICE);
//        mKeyguardLock = km.newKeyguardLock("key guard");
//        if (km.inKeyguardRestrictedInputMode()) {
//        mKeyguardLock.disableKeyguard();
//          isScreenLock = true;
//        } else {
//          isScreenLock = false;
//        }
 
    }
 
    static void releaseCpuLock() {
        Log.e("PushWakeLock""Releasing cpu wake lock");
//        if (isScreenLock) {
//          mKeyguardLock.reenableKeyguard();
//          isScreenLock = false;
//      }
 
        if (sCpuWakeLock != null) {
            sCpuWakeLock.release();
            sCpuWakeLock = null;
        }
    }
}
 
 

showMsg.java
package mint.TestC2dm;
 
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
 
public class showMsg extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         
        String title, msg;
        Bundle bun = getIntent().getExtras();
        title = bun.getString("title");
        msg = bun.getString("msg");
         
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(showMsg.this);
         
        alertDialog.setPositiveButton("닫기"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                PushWakeLock.releaseCpuLock();
                showMsg.this.finish();
            }
        });
         
        alertDialog.setNegativeButton("보기"new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent().setClassName(getPackageName(), getPackageName()+".TestC2dm"));
                PushWakeLock.releaseCpuLock();
                showMsg.this.finish();
            }
        });
         
        alertDialog.setTitle(title);
        alertDialog.setMessage(msg);
        alertDialog.show();
 
        // 폰 설정의 조명시간을 가져와서 해당 시간만큼만 화면을 켠다.
        int defTimeOut = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 15000);
        TimerTask task = new TimerTask() {
                 @Override
                public void run() {
                        PushWakeLock.releaseCpuLock();
                }
        };
             
        Timer timer = new Timer();
        timer.schedule(task, defTimeOut);
    }
}


메세지 보내기
 
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
 
 
public class Push
{
    private static String HOST = "http://android.apis.google.com/c2dm/send";
    private static String AUTH = "DQAAAL8AAADrYkFbucd............";
     
    // DB에 저장된 디바이스 토큰 목록
    private static String[] arrId =
    {
        "APA91bGxex5sJi5hbeQkGUaURZo8......."
//      "APA91bHCRg6NhgMYv8Rbb2LVCoj4al......."
    };
     
 
    public  static void main( String[] args ) throws Exception
    {
        for (int i=0; i<arrId.length; i++)
        {
            androidPush(arrId[i], "제목""내용");
        }
    }
     
    public static void androidPush(String  regId, String title, String msg) throwsException {
        try {
            StringBuffer postDataBuilder = new StringBuffer();
            postDataBuilder.append("registration_id=" + regId); // 등록ID
            postDataBuilder.append("&collapse_key=1");
            postDataBuilder.append("&delay_while_idle=1");
            postDataBuilder.append("&data.title=" + URLEncoder.encode(title, "UTF-8")); // 제목
            postDataBuilder.append("&data.msg=" + URLEncoder.encode(msg, "UTF-8")); // 내용
             
            byte[] postData = postDataBuilder.toString().getBytes("UTF8");
             
            URL url = new URL(HOST);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
            conn.setRequestProperty("Authorization""GoogleLogin auth="+AUTH);
            OutputStream out = conn.getOutputStream();
            out.write(postData);
            out.close();
            conn.getInputStream();
        catch (Exception e) {
            e.printStackTrace();
        }
    }   
}