В моем приложении я хочу использовать C2DM для реализации push-уведомлений.Для этого я использую следующий код, который получает токен устройства или идентификатор.Я зарегистрировался для push-уведомлений и получения идентификатора устройства, но моя проблема в том, что при повторном запуске приложения я получаю несколько идентификаторов устройств.
Что бы я ни читал из документов, оно говорит что-то вроде: «Вы получитеуникальный маркер устройства для вашего приложения, который вы можете отправить на сервер ".Так почему я получаю несколько токенов устройства?Я не понимаю.
Ниже мой код.Пожалуйста, помогите.
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
}
}