尝试取消注册并再次注册您的设备。在 DemoActivity.java 中放
final String regId = GCMRegistrar.getRegistrationId(this);
GCMRegistrar.unregister(this);
GCMRegistrar.unregister(this);
然后,在第二次启动时移除 。
更新
应用程序中的通知:
创建类
public class DemoApplication extends Application {
private class NotifyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "RECEIVE MESSAGE", Toast.LENGTH_SHORT).show();
}
}
private NotifyReceiver notifyReceiver = new NotifyReceiver();
@Override
public void onCreate() {
registerReceiver(notifyReceiver, new IntentFilter("GCM_MESSAGE"));
super.onCreate();
}
@Override
public void onTerminate() {
unregisterReceiver(notifyReceiver);
super.onTerminate();
}
}
然后放
<application
android:name=".DemoApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
在 AndroidManifest.xml 中发送广播
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = getString(R.string.gcm_message);
displayMessage(context, message);
context.sendBroadcast(new Intent("GCM_MESSAGE"));
// notifies user
generateNotification(context, message);
}
作为替代情况,您可以在清单、活动或前台服务中注册广播接收器