嗨,我試圖從tutorial整合GCM。我有兩個包第一個是com.wpeden.social.circle
,第二個是com.wpeden.social.circle.gcm
。在第一個包中,我添加了我的MainActivity
,其中的SignUp
類。我在我的項目中添加了google-play-services
和gcm.jar
。我使用tutorial到com.wpeden.social.circle.gcm
的所有GCM
要求AlertDialogManager,ConnectionDetector,CommonUtilities,ServerUtilities
。在我的SignUp
類中,我使用以下代碼使設備註冊並保存數據庫的註冊碼,名稱,密碼。但它不能獲得註冊ID並只保存用戶名和密碼。在我的清單,我用以下,,Android GCM:空註冊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。在此先感謝
check logcat any errar is coming – 2014-10-20 05:56:20
also check this http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/ – 2014-10-20 05:56:55
不,它不顯示任何錯誤 – user3384985 2014-10-20 05:58:29