private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = " ";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(Main.this);
}
regId = gcm.register(Config.GOOGLE_PROJECT_ID);
Log.d("RegisterActivity", "registerInBackground - regId: " + regId);
msg = "Device registered, registration ID=" + regId;
String result= ShareExternalServer.shareRegIdWithAppServer(Main.this,regId);
storeRegistrationId(Main.this, regId);
Log.i("RegId Sent To Server", result);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
Log.d("RegisterActivity", "Error: " + msg);
}
Log.d("RegisterActivity", "AsyncTask completed: " + msg);
return msg;
}
@Override
protected void onPostExecute(String msg) {
Log.d("RegisterActivity", "registerInBackground - regId: "+ regId); //Device Registration Id .. Copy it.
Toast.makeText(getApplicationContext(),"Registered with GCM Server." + msg, Toast.LENGTH_LONG).show();
}
}.execute(null, null, null);
}
//This method saves the generated device id in your shred prefrences
private void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getSharedPreferences(Main.class.getSimpleName(), Context.MODE_PRIVATE);
Log.i("Mudit Sen", "Saving regId on app version ");
SharedPreferences.Editor editor = prefs.edit();
editor.putString(REG_ID, regId);
try {
editor.putInt("appVersion", context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
editor.commit();
}
我假設你知道去哪裏粘貼 「REG設備ID」 在你的PHP文件。
關注此鏈接https://developers.google.com/cloud-messaging/android/client –