PHP调用FCM推送iOS通知报错:InvalidRegistration 求助
InvalidRegistration Error for iOS Push Notifications Hey there, let's figure out why you're getting that InvalidRegistration error when sending FCM push notifications to iOS devices. This error almost always ties back to issues with your device token, server key, or payload structure—let's break down each possible fix step by step:
1. Validate Your Device Token
This is the #1 cause of InvalidRegistration:
- Double-check the token format: iOS FCM tokens are 86-character Base64 strings (for modern APNs tokens). Make sure your
$tokenvariable doesn't have extra spaces, line breaks, or truncated characters. Try wrapping it intrim($token)to eliminate accidental whitespace. - Ensure the token is active: Tokens expire or change when a user uninstalls/reinstalls your app, or when they restore their device. Test with a fresh token generated directly from your iOS app's FCM setup code.
- Confirm token is linked to your FCM project: Tokens are tied to specific Firebase projects—if you copied a token from a different project, it won't work here.
2. Verify Your FCM Server Key
Don't mix up keys! The Server Key is specific to FCM:
- Go to your Firebase Console → Project Settings → Cloud Messaging tab. Copy the Server Key (not the "Legacy Server Key" unless you're using an older setup, and definitely not the Web API Key).
- Make sure the key in your
$serverKeyvariable is exactly as shown—no missing characters, extra spaces, or typos.
3. Simplify Your Payload to Test
Sometimes overcomplicating the payload can trigger issues. Let's start with a minimal working payload:
$arrayToSend = array( 'to' => trim($token), 'notification' => array( 'title' => $title, 'body' => $body, 'sound' => 'default', 'badge' => '1' ), 'priority' => 'high' );
Once this works, you can re-add data and content_available fields one by one to isolate any issues. Note that content_available: true is mainly for silent notifications—if you're sending a visible alert, it might not be necessary (though it won't break things if included correctly).
4. Tweak Your cURL Settings (Minor Checks)
- You have
CURLOPT_SSL_VERIFYPEER, falseset—while this works for testing, enable it in production (true) for security. This isn't causing your current error, but it's a good practice. - The
CURLOPT_HEADER, truesetting includes response headers in your output. To focus on the FCM response body, you can extract it like this:
This makes it easier to read the exact error details.$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $response_body = substr($response, $header_size); print_r(json_decode($response_body, true));
Example Fixed Code
Here's a cleaned-up version of your code incorporating the above fixes:
$url = "https://fcm.googleapis.com/fcm/send"; $token = trim("xxx"); // Trim any accidental whitespace $serverKey = 'xxx'; // Use your valid FCM Server Key $title = "Title"; $body = "Body of the message"; $notification = array( 'title' => $title, 'body' => $body, 'sound' => 'default', 'badge' => '1' ); $arrayToSend = array( 'to' => $token, 'notification' => $notification, 'priority' => 'high' ); $json = json_encode($arrayToSend); $headers = array( 'Content-Type: application/json', 'Authorization: key=' . $serverKey ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Enable for production curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); // Extract and print only the response body $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $response_body = substr($response, $header_size); print_r(json_decode($response_body, true)); curl_close($ch);
Start with these checks—9 times out of 10, the issue is a bad token or incorrect server key. If you still run into problems, let's dig deeper!
内容的提问来源于stack exchange,提问作者sgnm

