1

我在 GCMIntentService 类中有以下两个方法,但是在注册 regID 时会调用 onRegistered() 方法,但是,当我发送消息时,不会调用 onMessage()。

@Override
protected void onRegistered(Context context, String registrationId)
{
    Log.i(tag, "onRegistered called");
    GCMRegistrar.setRegisteredOnServer(context, true);
}

@Override
protected void onMessage(Context context, Intent intent)
{
    Log.i(TAG, "Received message");
    sendNotification("A", "B");
}

下面是我的清单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.myApp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
    <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.myApp" />
        </intent-filter>
    </receiver>

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

我得到这样的注册ID:

            regid = getRegistrationId(context);

            //Check if we have registered.
            if (regid.length() == 0) {
                GCMRegistrar.checkDevice(myActivity.this);
                GCMRegistrar.checkManifest(myActivity.this);
                GCMRegistrar.register(myActivity.this, SENDER_ID);
            } else {
                Log.i(tag, "You are already registered.");
                Log.i(tag, "The registration ID is : " + regid);
            }
4

2 回答 2

1

在您的清单中,使用以下代码接收消息:

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

<uses-permission android:name="com.MyApp.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
于 2013-08-26T12:12:43.937 回答
1

你的清单中有这个:

<uses-permission android:name="com.myApp.permission.C2D_MESSAGE" />

但是你忘记了这一点:

 <permission android:name="com.myApp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
于 2013-08-26T09:57:02.253 回答