좋은 샘플들이 많아서 ...참고하여 개발을 하고있는데...


Auth Key와 registration_id 는 넘어오고 있습니다.....


근데 메세지를 보내면.....아무 반응이 없습니다...onReceive 로 넘어오고 있지 않고 있어요...


모가 문제일까요 답답하내요 ㅠㅠ 아래는 소스 입니다.


public class C2DMReceiver extends BroadcastReceiver{


private PushWakeLock pushWakeLock = null;

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

Log.e("ssun","onReceive");

if(intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")){

//C2DM 등록 ID 를 받은 경우

handleRegistration(context,intent);

}else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")){

//C2DM Message 를 받은경우

handleMessage(context,intent);

}

}

private void handleRegistration(Context context, Intent intent){

Log.e("ssun","handleRegistration");

String registration = intent.getStringExtra("registration_id");

if(intent.getStringExtra("error")!= null){

//Registration failed, should tyy again later

}else if (intent.getStringExtra("unregistered") != null){

Log.e("ssun","unregistrered");

}else if(registration != null){

Log.e("ssun","registration===>"+registration);

PreferenceSetting prf = new PreferenceSetting(context);

prf.setPrefs("c2dm", "registrationID", registration);

}

}

private void handleMessage(Context context, Intent intent){

Log.e("ssun","handleMessage");

String title = intent.getStringExtra("title");

String msg = intent.getStringExtra("msg");

//화면깨우기

pushWakeLock.acquireCpuWakeLock(context);

Intent mIntent = new Intent(context,ShowMsg.class);

Bundle bundle = new Bundle();

bundle.putString("title", title);

bundle.putString("msg", msg);

mIntent.putExtras(bundle);

mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(mIntent);

}

}



public class C2dm extends Activity {

private String HOST = "https://android.apis.google.com/c2dm/send";

private EditText edittxt_title = null;

private EditText edittxt_body = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.c2dm);

edittxt_title = (EditText)findViewById(R.id.edittxt_title);

edittxt_body = (EditText)findViewById(R.id.edittxt_body);

}

// 등록

    public void onRegist(View v)

    {

    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");

    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate

    registrationIntent.putExtra("sender", "ssmkalma7@gmail.com");

    startService(registrationIntent);

    }

  

    // 해지   

    public void onUnregist(View v)

    {

        Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");

        unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));

        startService(unregIntent);

    }

public void sendC2dm(View v){

try{

PreferenceSetting prf = new PreferenceSetting(C2dm.this);

String registration_id = prf.getPrefsString("c2dm", "registrationID");

String auth = prf.getPrefsString("c2dm", "Auth");

Log.i("ssun","registration_id===>"+registration_id);

Log.i("ssun","auth===>"+auth);

StringBuffer postDataBuilder = new StringBuffer();

postDataBuilder.append("registration_id="+registration_id);

postDataBuilder.append("&collapse_key=1");

postDataBuilder.append("&delay_while_idle=1");

postDataBuilder.append("&data.title="+URLEncoder.encode(edittxt_title.getText().toString(),"UTF-8"));

postDataBuilder.append("&data.msg="+URLEncoder.encode(edittxt_body.getText().toString(),"UTF-8"));

Log.i("ssun","title==>"+edittxt_title.getText().toString());

Log.i("ssun","body===>"+edittxt_body.getText().toString());

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.getOutputStream();

Log.i("ssun","UrlConnection====>"+HttpURLConnection.HTTP_OK);

}catch(Exception e){

Log.i("ssun","Error====>"+e.getMessage());

}

}

}


Auth Key 받기


private void setC2dmAuth() {

// TODO Auto-generated method stub

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);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

while((inputLine = in.readLine()) != null){

inputAuth = inputLine;

Log.i("ssun","inputLine===>"+inputLine);

Log.i("ssun","inputLine===>"+inputAuth);

}

int start = inputAuth.indexOf("Auth=");

String a = inputAuth.substring(start+5, inputAuth.length());

Log.i("ssun","a====>"+a);

PreferenceSetting prf = new PreferenceSetting(test.this);

prf.setPrefs("c2dm", "Auth", a);

}catch(Exception e){

Log.i("ssun","Error===>"+e.getMessage());

}

}




Manifeast.xml


 <receiver

            android:name="com.bsi.hellosmartie.service.C2DMReceiver"

            android:permission="com.google.android.c2dm.permission.SEND" >

            <!-- Receive the actual message -->

            <intent-filter>

                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.bsi.hellosmartie" />

            </intent-filter>

            <!-- Receive the registration id -->

            <intent-filter>

                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.bsi.hellosmartie" />

            </intent-filter>

        </receiver>


<permission

        android:name="com.bsi.hellosmartie.permission.C2D_MESSAGE"

        android:protectionLevel="signature" />


    <uses-permission android:name="com.bsi.hellosmartie.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive message -->

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />