안녕하세요.

 

알림기능 개발중입니다.

 

http://blog.naver.com/0677haha?Redirect=Log&logNo=60165740039 블로그를 참조하였습니다.

 

블로그나 안드로이드 사이트 레퍼런스를 찾아보니 아래처럼 구현하는 것 까지는 알겠는데요..

 

단말기 고유번호를 서버단에 저장을 해서 알림을 뿌려야 하는데, 어떤시점에서 어떻게 호출을 해야하는지 모르겠습니다.

 

아래의 소스중 빨간부분에서 registration_id 를 받아 오는걸로 알고 있는데요.....

 

GCMBroadcastReceiver.onReceive 가 어느 시점에서 어떻게 호출이 되는건지 개념이 서지 않습니다. ㅜㅜ

 

좋은 하루 되시구요... 아래의 소스를 어떻게 해야하는지 조언좀 부탁 드리겠습니다.

 

감사합니다.

 

 

 

MainActivity *********************************************

.................

      Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
      registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // 어플리케이션ID
      registrationIntent.putExtra("sender", "0000000000"); // 개발자ID
      startService(registrationIntent); // 서비스 시작(등록ID발급받기)    

................

 

 

 

GCMBroadcastReceiver  **************************************

public class GCMBroadcastReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent intent) {
  Toast.makeText(context, "GCMBroadcastReceiver", Toast.LENGTH_SHORT).show();

 

  if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
String  registration_id = intent.getStringExtra("registration_id");

아이디 생성

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

알림받음

}

 

  GCMIntentService.runIntentInService(context, intent);
 }
}

 

 

 

GCMIntentService ************************************


public class GCMIntentService extends IntentService{
 
 public GCMIntentService(String name) {
  super(name);
 }

 private static PowerManager.WakeLock sWakeLock;
    private static final Object LOCK = GCMIntentService.class;
   
    static void runIntentInService(Context context, Intent intent) {
        synchronized(LOCK) {
            if (sWakeLock == null) {
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock");
            }
        }
        sWakeLock.acquire();
        intent.setClassName(context, GCMIntentService.class.getName());
        context.startService(intent);
       
  Toast.makeText(context, "GCMIntentService 1111111", Toast.LENGTH_SHORT).show();

    }
   
    @Override
    public final void onHandleIntent(Intent intent) {
        try {
            String action = intent.getAction();
            if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
             
            } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {

            }
        } finally {
            synchronized(LOCK) {
                sWakeLock.release();
            }
        }
    }

}