In my application i want to use C2DM to implement push notifications. For this I am using following code which gets device token or id. I have register for push notification and getting the device id, but my problem is that I am getting multiple device IDs when I run my app again.
Whatever I read from docs it says something like "you will get a unique device token for your app which you can send it to server". So why I am getting multiple device tokens? I don't understand.
Below is my code. Please help.
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
}
}
One time Google says you will get a unique ID, but if you read few more lines google write:
The application should store this ID for later use. Note that Google may periodically refresh the registration ID, so you should design your application with the understanding that the REGISTRATION Intent may be called multiple times. Your application needs to be able to respond accordingly.
So it's not an unique ID. I've just tested to push to a device with 2 different registration IDs, both push messages arrived at the device. So it should be no problem. The only thing is, that Google maybe delete the registration if after a couple of time. So you have to call for another one.