在android中实现C2DM

时间:2011-08-23 10:14:30

标签: android android-c2dm

在我的应用程序中,我想使用C2DM来实现推送通知。为此,我使用以下代码获取设备令牌或ID。我已注册推送通知并获取设备ID,但我的问题是,当我再次运行我的应用时,我收到了多个设备ID。

无论我从文档中读到什么,它都会说“你会为你的应用程序获得一个独特的设备令牌,你可以把它发送到服务器”。那么为什么我要获得多个设备令牌呢?我不明白。

以下是我的代码。请帮忙。

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
    }
}

1 个答案:

答案 0 :(得分:2)

有一次谷歌说你将获得一个唯一的ID,但如果你读了几行google写道:

  

应用程序应存储此ID以供以后使用。请注意Google   可能会定期刷新注册ID,因此您应该进行设计   您的申请了解REGISTRATION Intent   可以多次调用。您的应用程序需要能够   做出相应的回应。

Read here under 2.

所以它不是唯一的ID。 我刚刚测试推送到具有2个不同注册ID的设备,两个推送消息都到达了设备。所以应该没问题。 唯一的问题是,谷歌可能会在几次后删除注册。 所以你必须再打一个。