2011-08-23 72 views
0

在我的應用程序中,我想使用C2DM來實現推送通知。爲此,我使用以下代碼獲取設備標記或標識。我註冊了推送通知並獲取了設備ID,但是我的問題是當我再次運行我的應用程序時,我得到了多個設備ID。Android中的C2DM實現

無論我從文檔中讀到什麼,它都會顯示類似於「您將獲得適用於您的應用的唯一設備令牌,您可以將其發送給服務器」。那麼爲什麼我要獲得多個設備令牌?我不明白。

以下是我的代碼。請幫忙。

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

使用equals而不是== ...然後它也可以工作.. – TacB0sS

回答

2

有一次,谷歌說,你會得到一個唯一的ID,但如果你讀幾行寫谷歌:

應用程序應存儲這個ID以備後用。請注意,谷歌 可能會定期刷新註冊ID,所以您應該設計 您的應用程序,理解可以多次調用REGISTRATION Intent 。您的應用程序需要能夠相應地響應 。

Read here under 2.

所以它不是一個唯一的ID。 我剛剛測試過推送到具有2個不同註冊ID的設備,兩個推送消息都到達設備。所以它應該沒問題。 唯一的一點是,Google可能會在幾次之後刪除註冊。 所以你必須打電話給另一個。