Android GCM:空注册ID

时间:2014-10-20 05:55:16

标签: android push-notification

您好我正在尝试整合来自tutorial的GCM。我有两个包,第一个是com.wpeden.social.circle,第二个是com.wpeden.social.circle.gcm。在第一个包中,我刚刚添加了MainActivity来调用SignUp类。我在项目中添加了google-play-servicesgcm.jar。我使用tutorialGCM的所有AlertDialogManager,ConnectionDetector,CommonUtilities,ServerUtilities要求com.wpeden.social.circle.gcm。在我的SignUp类中,我使用以下代码使设备注册并将注册码,名称,密码保存到数据库。但它无法获得注册ID并仅保存用户名和密码。在我的清单中,我使用了以下,

<permission android:name="com.wpeden.social.circle.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.wpeden.social.circle.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>

<service android:name="com.wpeden.social.circle.gcm.GCMIntentService" />

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.wpeden.social.circle.gcm" />
        </intent-filter>
    </receiver>
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

在我的SignUp班级

wpedenSignUpButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            userName = wpedenSignUpUserName.getText().toString();
            password = wpedenSignUpPassword.getText().toString();
            GCMRegistrar.checkDevice(SignUp.this);

            GCMRegistrar.checkManifest(SignUp.this);
            registrationId = GCMRegistrar.getRegistrationId(SignUp.this);

            if (registrationId.equals("")) {
                GCMRegistrar.register(SignUp.this, SENDER_ID);
            } else {
                if (GCMRegistrar.isRegisteredOnServer(SignUp.this)) {
                    Toast.makeText(SignUp.this.getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
                } else {
                    final Context context = SignUp.this.getApplicationContext();
                    mRegisterTask = new AsyncTask<Void, Void, Void>() {

                        @Override
                        protected Void doInBackground(Void... params) {
                            ServerUtilities.register(context, userName, password, registrationId);
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void result) {
                            mRegisterTask = null;
                        }

                    };
                    mRegisterTask.execute(null, null, null);
                }
            }

            System.out.println("this is my reg id:" +registrationId);
            new SignUpUser().execute();
        }
    });

class SignUpUser extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SignUp.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id", userName));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("reg_id", registrationId));
        params.add(new BasicNameValuePair("type", type));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(wpedenSignUpUrl,
                "POST", params);

        String result = "";
        try {
            result = json.getString("message");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return result;
    }

    protected void onPostExecute(String result) {
        // dismiss the dialog once done
        pDialog.dismiss();
        if (result.equals(TAG_SUCCESS)) {
            Toast.makeText(SignUp.this, "Successfully saved",
                    Toast.LENGTH_LONG).show();
            run();
            Intent intent = new Intent(SignUp.this, UserProfileTab.class);
            startActivity(intent);
        } else {
            Toast.makeText(SignUp.this, "Failed to save", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

它可以保存用户名,密码但获取空注册ID。提前致谢

0 个答案:

没有答案
相关问题