GCM - 永远不要使用Android 4.0设备输入onMessage()

时间:2013-01-24 05:42:41

标签: android push-notification google-cloud-messaging

我对Android开发并不熟悉,我将gcm包含在现有应用中。 到目前为止,我可以注册所有设备,然后收到registrationId。问题是,我从未在Android 4.0设备上输入onMessage()方法。我也用Galaxy Note 2(Android 4.1)尝试过,我可以收到消息。 我没有收到任何错误消息,我只是从不输入onMessage()。

当我发送消息时,我总是收到一条消息,因此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);
}
}

我的Manifest.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");
}

2 个答案:

答案 0 :(得分:0)

如果您通过WIFI尝试此操作,请确保网络未阻止端口5228。这就是我如何解决我在尝试使用它之后所遇到的问题而不是我想承认的问题。

答案 1 :(得分:0)

确保GCMIntentService类位于清单

中指定的包中