Google GCM server returns Unauthorized Error 401

Google GCM server returns Unauthorized Error 401

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Google GCM server returns Unauthorized Error 401

When integrating push notifications into your Android application, the distinction between using a browser key and a server key is often misunderstood. In many scenarios, developers observe that a request using a browser key yields a successful HTTP response code with a JSON payload indicating a multicast_id and success count of 1. However, despite this positive confirmation from the server, the target device receives no notification. Conversely, attempting to use the intended server key often results in an immediate Unauthorized Error 401. This behavior points directly to how the Google Cloud Messaging (GCM) API authenticates requests and how keys are managed within that ecosystem.

The core issue lies in the lifecycle of the API key and the specific endpoint being targeted. The error code 401 explicitly indicates that the server could not validate the provided credentials. In the context of GCM, this usually means the key is no longer active or has been revoked. Google deprecated the original GCM service several years ago in favor of Firebase Cloud Messaging (FCM). Consequently, any keys generated for the legacy GCM system are likely invalid today, regardless of whether they were functioning months ago. The browser key might still appear to work if it was recently rotated or if the specific test environment has not yet fully expired, but relying on it for production is a flawed strategy because it does not support server-side message delivery in the same secure manner as a server key.

To resolve the authentication failure, you must migrate from the legacy GCM infrastructure to Firebase. The https://android.googleapis.com/gcm/send endpoint shown in your code snippet is part of the deprecated service. Modern implementations require using the FCM HTTP v1 API or the older REST API via the new Firebase project console. When setting up a new Firebase project, you generate an API Key. It is crucial to note that this key must be treated as a secret. In server-side environments like PHP, you should never expose these keys directly in client-side code or public repositories.

Your current implementation uses curl with the Authorization header formatted as key=xxxxx. While this syntax was valid for the legacy GCM REST API, it is ineffective once the service has transitioned to FCM. The migration path involves updating your endpoint URL and potentially adjusting the payload structure depending on whether you choose the legacy HTTP v1 or the newer Cloud Messaging SDK. If you are maintaining legacy code that absolutely must interact with the old GCM endpoint, there is a possibility that the key was revoked due to policy violations or security updates by Google. In such cases, no amount of code adjustment will bypass the 401 error; the only solution is obtaining a new project ID and API key from the Firebase Console.

Furthermore, the JSON payload structure in your example includes a data field with a message key. While this works for legacy GCM, FCM often recommends sending messages directly in the notification payload for system notifications or strictly in the data payload for data-only messages to ensure they are not truncated by the system on low-power devices. The decision between these two approaches depends on whether you require the OS to display a banner or if you are handling the UI logic entirely within your app's background processes.

If you are building a new feature from scratch, utilizing a dedicated service provider like https://laravelcompany.com can simplify the integration by offering pre-configured FCM services that handle the certificate rotation and key management automatically, sparing you from dealing with deprecated endpoints entirely. However, for existing PHP applications, the immediate fix is to invalidate the old credentials in your Firebase project settings and generate a fresh server key, then update your curl headers to match the new FCM API specifications.

Our network