0
我想在android中使用GCM推送通知。我阅读了GCM的Google文档以及演示应用程序。我在lib.bur中包含gcm,jar,regid变量是log.i中的黑色显示。在我的代码中出现一些错误,请检查我的代码并告诉我适当的解决方案。android GCM注册ID空白
package avi.android.pushnotifications;
import static avi.android.pushnotifications.CommonUtilities.DISPLAY_MESSAGE_ACTION;
import static avi.android.pushnotifications.CommonUtilities.EXTRA_MESSAGE;
import static avi.android.pushnotifications.CommonUtilities.SENDER_ID;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.androidhive.pushnotifications.R;
import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends Activity {
// label to display gcm messages
TextView lblMessage;
final static String Tag =MainActivity.class.getSimpleName();
// Asyntask
AsyncTask<Void, Void, Void> mRegisterTask;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Connection detector
ConnectionDetector cd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
lblMessage = (TextView) findViewById(R.id.lblMessage);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
Log.v(Tag, "Registered regId"+regId);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, SENDER_ID);
Log.v(Tag, "Registered");
Toast.makeText(getApplicationContext(), " registered with GCM", Toast.LENGTH_LONG).show();
//sachin...................
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
ServerUtilities.register(context, regId);
//ServerUtilities.unregister(context, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
//.................................sachin
} else {
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
//ServerUtilities.register(context, regId);
ServerUtilities.unregister(context, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
}
/**
* Receiving push messages
* */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
lblMessage.append(newMessage + "\n");
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
// Releasing wake lock
WakeLocker.release();
}
};
@Override
protected void onDestroy() {
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
try {
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
} catch (Exception e) {
Log.e("UnRegister Receiver Error", "> " + e.getMessage());
}
super.onDestroy();
}
}
logcat的
03-04 06:25:03.723: V/MainActivity(2206): Registered regId
03-04 06:25:03.723: D/GCMRegistrar(2206): resetting backoff for
com.androidhive.pushnotifications
03-04 06:25:03.733: V/GCMRegistrar(2206): Registering app
com.androidhive.pushnotifications of senders 1099113278913
03-04 06:25:03.773: V/MainActivity(2206): Registered
03-04 06:25:04.003: D/dalvikvm(2206): GC_FOR_ALLOC freed 216K, 10% free 2996K/3300K,
paused 75ms, total 84ms
03-04 06:25:04.083: I/AndroidHive GCM(2206): registering device (regId =)
03-04 06:25:04.083: D/AndroidHive GCM(2206): Attempt #1 to register
03-04 06:25:04.163: V/AndroidHive GCM(2206): Posting
'[email protected]®Id=&name=sa' to
http://www.xyx.com/xxx/sac_images/newu.php
03-04 06:25:04.193: E/URL(2206): > http://www.xyx.com/xxx/sac_images/newu.php
03-04 06:25:04.193: W/EGL_emulation(2206): eglSurfaceAttrib not implemented
03-04 06:25:09.513: V/GCMRegistrar(2206): Setting registeredOnServer status as true
until 2014-03-11 07:25:09.509
03-04 06:25:10.143: V/GCMBroadcastReceiver(2206): onReceive:
com.google.android.c2dm.intent.REGISTRATION
03-04 06:25:10.143: V/GCMBroadcastReceiver(2206): GCM IntentService class:
com.androidhive.pushnotifications.GCMIntentService
03-04 06:25:10.163: V/GCMBaseIntentService(2206): Acquiring wakelock