如何在android.mk项目中集成GCM并解决通知接收失败问题
Hey there! Let's work through this GCM integration snag in your Android.mk project. Since you already got it running smoothly in Android Studio with Gradle, the issue is almost certainly missing some critical steps that Gradle handles automatically—but Android.mk requires you to set up manually. Here's what you need to check and fix:
1. Properly Unpack & Reference the GCM AAR
Android.mk doesn’t automatically process AAR files like Gradle does, so you’ll need to manually unpack the GCM-specific AAR (not the full Google Play Services bundle, if possible) and configure your build files:
- Unzip your
google-play-services-gcm.aarinto a subfolder underlibs, e.g.,libs/google-play-services-gcm/ - Update your Android.mk to reference the extracted resources, manifest, and classes.jar:
# Add GCM as a static library dependency LOCAL_STATIC_JAVA_LIBRARIES += google_play_services_gcm # Define the GCM static library include $(CLEAR_VARS) LOCAL_MODULE := google_play_services_gcm LOCAL_SRC_FILES := libs/google-play-services-gcm/classes.jar LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/libs/google-play-services-gcm/res LOCAL_MANIFEST_FILE := $(LOCAL_PATH)/libs/google-play-services-gcm/AndroidManifest.xml include $(BUILD_STATIC_JAVA_LIBRARY) - Add this line to your main module’s Android.mk to enable resource merging (critical for GCM’s required resources):
LOCAL_AAPT_FLAGS += --auto-add-overlay
2. Manually Add GCM’s Manifest Requirements
Gradle auto-merges GCM’s manifest entries, but Android.mk doesn’t. You must add these directly to your app’s AndroidManifest.xml:
Required Permissions
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- Replace YOUR_PACKAGE_NAME with your app's actual package name --> <permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" />
GCM Service Components
Add these inside the <application> tag:
<!-- GCM Listener Service (handles incoming messages) --> <service android:name="com.google.android.gms.gcm.GcmListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> <!-- Instance ID Listener Service (handles token refresh) --> <service android:name="com.google.android.gms.iid.InstanceIDListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID" /> </intent-filter> </service> <!-- Your custom registration service (matches the one in your code) --> <service android:name=".your.package.GcmRegistrationIntentService" android:exported="false"> </service>
3. Validate Google Play Services Initialization
GCM won’t work unless you confirm Google Play Services is available on the device. Add this check in your Application class or launch Activity:
@Override public void onCreate() { super.onCreate(); GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { // Show dialog to update Google Play Services apiAvailability.getErrorDialog(this, resultCode, 9000).show(); } else { Log.e(TAG, "This device doesn't support Google Play Services."); finish(); } } }
4. Double-Check Token Generation & Server Setup
- Ensure your token generation code is correct (in your
GcmRegistrationIntentService):String token = InstanceID.getInstance(this).getToken( getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null ); // Send this token to your backend server - Confirm your server uses the legacy GCM endpoint (
https://android.googleapis.com/gcm/send) with the correct API key in theAuthorizationheader (key=YOUR_SERVER_API_KEY). Also verify the token sent to the server matches what your app generates, and your app’s package name/signature matches the one registered in the Google Cloud Console.
5. Debugging Tips
- Filter Logcat for tags
GCM,InstanceID, andGooglePlayServicesto spot errors like failed initialization, token retrieval issues, or blocked broadcast receivers. - Use
aapt dump badging your.apkto confirm your APK includes all required GCM permissions and components. - Make sure your test device has a compatible version of Google Play Services (not outdated).
内容的提问来源于stack exchange,提问作者Prasanna Kumar Peddinti




