2011-08-23 4 views
0

내 응용 프로그램에서 C2DM을 사용하여 푸시 알림을 구현하려고합니다. 이를 위해 다음과 같은 코드를 사용하여 장치 토큰 또는 ID를 얻습니다. 푸시 알림을 등록하고 장치 ID를 받고 있지만 내 문제는 내가 다시 내 응용 프로그램을 실행할 때 여러 장치 ID를 얻는 것입니다.안드로이드에서 C2DM 구현

docs에서 읽은 내용은 "앱을 서버에 보낼 수있는 고유 한 장치 토큰을 얻을 것"과 같이 표시됩니다. 그렇다면 왜 여러 장치 토큰을 얻고 있습니까? 나는 이해하지 못한다.

아래 코드는 제 코드입니다. 도와주세요.

public class RegisterActivity extends Activity implements OnClickListener { 
    private Context context; 
    SharedPreferences preferences; 
    Button button; 
    /** Called when the activity is first created. */ 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     button=(Button)findViewById(R.id.button1); 
     button.setOnClickListener(this); 


     Log.i("reg key---",""+C2DMBroadcastReceiver.k); 


    } 
    @Override 
    public void onClick(View v) { 
     if(C2DMBroadcastReceiver.k!=null) 
     { 
      Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 

      registrationIntent.putExtra("app", PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(), 0)); 

      registrationIntent.putExtra("sender", "my email"); 

      getApplicationContext().startService(registrationIntent); 
     } 

    } 
} 


public class C2DMBroadcastReceiver extends BroadcastReceiver { 

    private Context context; 
    private static String KEY = "c2dmPref"; 
    private static String REGISTRATION_KEY = "registrationKey"; 
    public static String k; 


    @Override 
    public final void onReceive(Context context, Intent intent) { 
     //   // To keep things in one place. 
     //   C2DMBaseReceiver.runIntentInService(context, intent); 
     //   setResult(Activity.RESULT_OK, null /* data */, null /* extra */);  

     this.context = context; 
     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) { 
     String registration = intent.getStringExtra("registration_id"); 
     if (intent.getStringExtra("error") != null) { 
      // Registration failed, should try again later. 
      Log.d("c2dm", "registration failed"); 
      String error = intent.getStringExtra("error"); 
      if(error == "SERVICE_NOT_AVAILABLE"){ 
       Log.d("c2dm", "SERVICE_NOT_AVAILABLE"); 
      }else if(error == "ACCOUNT_MISSING"){ 
       Log.d("c2dm", "ACCOUNT_MISSING"); 
      }else if(error == "AUTHENTICATION_FAILED"){ 
       Log.d("c2dm", "AUTHENTICATION_FAILED"); 
      }else if(error == "TOO_MANY_REGISTRATIONS"){ 
       Log.d("c2dm", "TOO_MANY_REGISTRATIONS"); 
      }else if(error == "INVALID_SENDER"){ 
       Log.d("c2dm", "INVALID_SENDER"); 
      }else if(error == "PHONE_REGISTRATION_ERROR"){ 
       Log.d("c2dm", "PHONE_REGISTRATION_ERROR"); 
      } 
     } else if (intent.getStringExtra("unregistered") != null) { 
      // unregistration done, new messages from the authorized sender will be rejected 
      Log.d("c2dm=======", "unregistered"); 

     } else if (registration != null) { 
      Log.d("c2dm========", registration); 
      SharedPreferences preferences=context.getSharedPreferences("KEY", Context.MODE_PRIVATE); 
      k=preferences.getString("REGISTRATION_KEY",registration); 

      Editor editor = 
       context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit(); 
      editor.putString(REGISTRATION_KEY, registration); 
      editor.commit(); 
      // 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. 

     } 
    } 

    private void handleMessage(Context context, Intent intent) 
    { 
     //Do whatever you want with the message 
    } 
} 
+0

== ... 대신 같음을 사용하면 작동 할 수도 있습니다. – TacB0sS

답변

2

한 시간 Google은 고유 ID를 얻을 것이다라고,하지만 당신은 몇 가지 더 라인 구글 읽고 쓸 경우 :

이 응용 프로그램은 나중에 사용하기 위해이 ID를 저장해야합니다. Google 은 주기적으로 등록 ID를 새로 고칠 수 있으므로 번을 여러 번 호출 할 수 있다는 이해하에 애플리케이션을 디자인해야합니다. 귀하의 응용 프로그램은 그에 따라 응답 수 있어야합니다.

Read here under 2.

는 그래서 고유 ID 아니다. 두 개의 다른 등록 ID를 가진 장치로 푸시 테스트했습니다. 두 푸시 메시지가 모두 장치에 도착했습니다. 그래서 아무런 문제가 없어야합니다. 몇 가지 경우에만 Google에서 등록을 삭제할 수 있습니다. 그래서 다른 전화를해야합니다.