1

我对 Android 开发不是很有经验,我会将 gcm 包含到现有应用程序中。到目前为止,我可以注册所有设备并收到registrationId。问题是,我从来没有在我的 Android 4.0 设备上输入 onMessage() 方法。我还使用 Galaxy Note 2 (Android 4.1) 进行了尝试,我可以在那里收到消息。我没有收到任何错误消息,我只是从不输入 onMessage()。

当我发送消息时,我总是得到一个 messageId,因此 gcm “接受”了该消息。但它永远不会交付给设备。

我怎样才能找出问题所在???提前致谢

GCMIntentService.java

public class GCMIntentService extends GCMBaseIntentService {

public GCMIntentService() {
    super("82........");
}

@Override
protected void onError(Context arg0, String error) {
    System.out.println("Not able to register or unregister because: "
            + error);

}

@Override
protected void onMessage(Context context, Intent intent) {

    String message = intent.getStringExtra("message");

    System.out.println("GCM DELIVERED MESSAGE TO DEVICE:" + message);
}

@Override
protected void onRegistered(Context arg0, String regId) {
    System.out.println("Device is registered  with RegisterId: " + regId);
}

@Override
protected void onUnregistered(Context arg0, String regId) {
    System.out.println("Device with RegisterId: " + regId
            + " has unregistered");
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    System.out.println("Entered onRecoverableError");
    return super.onRecoverableError(context, errorId);
}
}

我的清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activities"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<permission
    android:name="com.example.activities.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.activities.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <!-- Activity which is called first to decide which Activity comes next -->
    <activity
        android:name=".StartUp"
        android:label="@string/title_activity_start_up" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:label="@string/title_activity_login" >
    </activity>
    <activity
        android:name=".DisplayContent"
        android:label="@string/title_activity_display_content" >
    </activity>
    <activity
        android:name=".AllSigns"
        android:label="@string/title_activity_all_signs" >
    </activity>

    <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.example.activities" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMIntentService" />

</application>

我的第一个活动 (Startup.java) 的 onCreate() 中的代码

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);

if (regId.equals("")) {
System.out.println("Id ist leer, Device wird neu registriert");
GCMRegistrar.register(this, "825134311331");
} else {
System.out.println("This device is already registered");
}
4

2 回答 2

0

如果您通过 WIFI 尝试此操作,请确保端口 5228 没有被网络阻塞。这就是我解决这个问题的方法,我试图让它工作的时间比我想承认的要多。

于 2013-04-23T01:39:43.340 回答
0

确保 GCMIntentService 类位于清单中指定的包中

于 2013-12-22T19:02:29.570 回答