You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP调用FCM推送iOS通知报错:InvalidRegistration 求助

Fixing FCM 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 $token variable doesn't have extra spaces, line breaks, or truncated characters. Try wrapping it in trim($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 $serverKey variable 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, false set—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, true setting includes response headers in your output. To focus on the FCM response body, you can extract it like this:
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $response_body = substr($response, $header_size);
    print_r(json_decode($response_body, true));
    
    This makes it easier to read the exact error details.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:02:45