public class GCMIntentService extends GCMBaseIntentService
{

  public static final String GCM_PROJECT_ID = "=sender id.숫자로된거";

  //----------------------------------------------------------------------------
  // 생성자. 이거 꼭 만들어야 한다.
  //----------------------------------------------------------------------------
  public GCMIntentService()
  {
    this(GCM_PROJECT_ID);
    LibCommon.Log("*** GCMIntentService GCMIntentService()");
  }

  //----------------------------------------------------------------------------
  // 생성자
  //----------------------------------------------------------------------------
  public GCMIntentService(String sProjectID)
  {
    super(sProjectID);
    LibCommon.Log("*** GCMIntentService GCMIntentService(String)");
    LibCommon.Log("project id = [" + sProjectID + "]");
  }

  //----------------------------------------------------------------------------
  // GCM 등록 완료시
  //----------------------------------------------------------------------------
  @Override protected void onRegistered(Context context, String regId)
  {
    LibCommon.Log("GCM. GCMIntentService.onRegistered : ["+regId+"]");
    LibAuth.authSetGCMRegistrationID(context, regId);
  }


  //----------------------------------------------------------------------------
  // GCM 에러 발생시
  //----------------------------------------------------------------------------
  @Override protected void onError(Context context, String errorId)
  {
    LibCommon.Log("GCM. GCMIntentService.onError : ["+errorId+"]");
    LibAuth.authSetGCMRegistrationID(context, "ERROR");
  }


  //----------------------------------------------------------------------------
  // GCM 메세지 받았을때
  //----------------------------------------------------------------------------
  @Override protected void onMessage(Context context, Intent intent)
  {
    LibCommon.Log("GCM. GCMIntentService.onMessage");

    String sMessage = intent.getExtras().getString("msg");
    String sLectureCode = intent.getExtras().getString("lecture_code");
    String sTeacherCode = intent.getExtras().getString("teacher_code");
    String sSendTime = intent.getExtras().getString("send_time");
    String sNotice = intent.getExtras().getString("notice");

    LibCommon.Log("message = [" + sMessage + "]");
    LibCommon.Log("lecture_code = [" + sLectureCode + "]");
    LibCommon.Log("teacher_code = [" + sTeacherCode + "]");
    LibCommon.Log("send_time = [" + sSendTime + "]");
    LibCommon.Log("notice = [" + sNotice + "]");

    /*
    Bundle bundle = intent.getExtras();
    Iterator<String> iterator = bundle.keySet().iterator();
    while(iterator.hasNext())
    {
      String sKey = iterator.next();
      String sValue = bundle.get(sKey).toString();
      LibCommon.Log("KEY = [" + sKey + "]   sValue=[" + sValue + "]");
    }
    */

    Intent intentPushActivity = new Intent(GCMIntentService.this, push_activity.class);
    intentPushActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intentPushActivity.putExtra("MESSAGE", sMessage);
    intentPushActivity.putExtra("LECTURE_CODE", sLectureCode);
    intentPushActivity.putExtra("TEACHER_CODE", sTeacherCode);
    intentPushActivity.putExtra("SEND_TIME", sSendTime);
    intentPushActivity.putExtra("NOTICE", sNotice);
    startActivity(intentPushActivity);
  }


  //----------------------------------------------------------------------------
  // GCM 등록해제 완료시
  //----------------------------------------------------------------------------
  @Override protected void onUnregistered(Context context, String regId)
  {
    LibCommon.Log("GCM. ServiceGCM.onUnregistered : ["+regId+"]");
  }


  //----------------------------------------------------------------------------
  // os 4.0.4 이하에서는, GCM 사용하려면, 기기에 google 계정이 로그인 되어 있어야한다.
  // intro_activity 에서 호출됨
  //----------------------------------------------------------------------------
  public static boolean checkAvailableGCM(Context context)
  {
    if(Integer.parseInt(Build.VERSION.SDK) >= 16) return true;

    // 사용자의 구글 계정 ID 얻기
    String sUserGoogleAccount = "";
    Account[] aryAccount = AccountManager.get(context).getAccounts();
    for(int i=0; i<aryAccount.length; i++)
    {
      if(!"com.google".equals(aryAccount[i].type)) continue;

      sUserGoogleAccount = aryAccount[i].name;
      LibCommon.Log("google account = ["+sUserGoogleAccount+"]");
      break;
    }

    // 현재 기기에 등록된 구글계정이 없을때
    if(sUserGoogleAccount.equals(""))
    {
      LibCommon.showAlertDialog(context, "", "등록된 구글계정이 없습니다\n구글계정을 먼저 등록하여 주십시요.");
      return false;
    }

    return true;
  }


  //----------------------------------------------------------------------------
  // GCM 등록하기.
  // intro_activity 에서 호출됨
  //----------------------------------------------------------------------------
  public static void registerGCM(Context context)
  {
    GCMRegistrar.checkDevice(context); // device 가 GCM 을 지원하는지 검사.
    GCMRegistrar.checkManifest(context); // manifest 가 GCM 설정이 잘 되어 있는지 검사. (개발시에만 필요함)
    String sGCM_ID = GCMRegistrar.getRegistrationId(context);
    LibCommon.Log("sGCM_ID=["+sGCM_ID+"]");
    if(sGCM_ID.equals(""))
    {
      GCMRegistrar.register(context, GCMIntentService.GCM_PROJECT_ID);
    }
    else
    {
      LibAuth.authSetGCMRegistrationID(context, sGCM_ID);
      LibCommon.Log("GCM is already registered !");
    }
  }


}


반응형

'Android 초보' 카테고리의 다른 글

HttpURLConnection getInputStream FileNotFoundException  (0) 2014.07.04
GCM. 발송서버. php  (0) 2014.07.04
GCM 준비. 안드로이드앱  (0) 2014.07.04
GCM 준비  (0) 2014.07.04
mac, intelliJ, android sdk setting  (0) 2014.07.04
Posted by 돌비
,