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
}
}
使用equals而不是== ...然后它也可以工作.. – TacB0sS