我一直在寻找几天,似乎很多人遇到了同样的问题。
我尝试在我的设备中注册我的应用程序以从GCM获取ID。我提到androidhive。
我无法使用GCMRegistrar.register(this,SENDER_ID)获取ID,我仔细检查了我的发件人ID,这是正确的。
public class MainActivity extends Activity {
boolean flag=false;
AsyncTask<Void, Void, Void> mRegisterTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Device is already registered on GCM
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
Log.v("RegId", regId);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
if (GCMRegistrar.isRegisteredOnServer(this)) {
Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Map<String, String> prms=new HashMap<String, String>();
prms.put("regId", regId);
try {
post(SERVER_URL,prms);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
GCM intent服务写在主包
中public class GCMIntentservice extends GCMBaseIntentService{
private static final String TAG = "GCMIntentService";
public GCMIntentservice() {
super(SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onRegistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.i(TAG, "Device registered: regId = " + arg1);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.i(TAG, "Device unregistered"); } }
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fbbookresale"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<permission android:name="com.example.fbbookresale.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.fbbookresale.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="MainActivity"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.fbbookresale.MainActivity"
android:label="MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
<activity android:name="com.facebook.LoginActivity" android:label="@string/app_name">
</activity>
<activity
android:name="com.example.fbbookresale.BuyNSell"
android:label="" >
</activity>
<activity
android:name="com.example.fbbookresale.Edit"
android:label="" >
</activity>
<activity
android:name="com.example.fbbookresale.Add"
android:label="Buy" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.fbbookresale" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentservice" />
</application>
logcat正在显示
11-23 09:34:54.193: D/GCMRegistrar(987): resetting backoff for com.example.fbbookresale
11-23 09:34:54.193: V/GCMRegistrar(987): Registering app com.example.fbbookresale of senders 742646281XXX
11-23 11:17:27.821: V/GCMBroadcastReceiver(1046): onReceive: com.google.android.c2dm.intent.REGISTRATION
11-23 11:17:27.821: V/GCMBroadcastReceiver(1046): GCM IntentService class: com.example.fbbookresale.GCMIntentService
11-23 11:17:27.831: V/GCMBaseIntentService(1046): Acquiring wakelock
它甚至没有在详细信息中显示Log.v("RegId", regId);
。
答案 0 :(得分:1)
当我将意图服务类重命名为GCMIntentService时,以及在清单.GCMIntentService中,我能够在详细信息中获取注册ID
答案 1 :(得分:0)
GCMRegistrar
属于不推荐使用的库。它应该仍然有效,但你正确地使用它。 GCMRegistrar.register
是异步的。注册ID通过BroadcastReceiver返回onRegistered
GCMIntentService
方法。但是,您的onRegistered
方法对该注册ID不执行任何操作,因此GCMRegistrar
永远不会将其保存在本地,也不会发送到您的服务器。
您不应该使用旧教程作为示例。使用new GCM Client Demo,其中显示了如何使用新注册方法(使用GoogleCloudMessaging.register
库的GooglePlayServices
)。