Firebase跨Android/iOS推送仅单设备接收问题求助
Hey there! Let's break down why only one device is getting your notifications and fix it step by step.
The Core Issue
From your code snippet, it looks like you're handling a single device type and token at a time, but you mentioned you have an array of device IDs paired with their types. The most likely problem is that you're not iterating through every entry in that array—you're only processing one device instead of all of them.
Fix: Iterate Through Your Device Array
First, let's assume your device array follows a structure like this (adjust to match your actual data):
$deviceList = [ ['type' => 'iOS', 'token' => 'apple_device_token_123'], ['type' => 'Android', 'token' => 'android_fcm_token_456'], ['type' => 'iOS', 'token' => 'apple_device_token_789'] ];
Now, loop through each device in the array and send the notification accordingly. I added a lowercase conversion to avoid case-mismatch issues (since your original code checks for "ios" but your device type might be stored as "iOS"):
$message = "Your notification content here"; foreach ($deviceList as $device) { $deviceType = strtolower($device['type']); $deviceToken = $device['token']; if ($deviceType === "ios") { $this->send_ios_notfication($deviceToken, $message, 'New Message'); } elseif ($deviceType === "android") { $this->send_android_notfication($deviceToken, $message, 'New Message'); } }
Additional Checks to Ensure All Devices Receive Notifications
- Validate Token Validity: Tokens can expire (iOS APNs tokens) or become invalid (Android FCM tokens after app uninstall/reinstall). Add logging inside your
send_ios_notficationandsend_android_notficationmethods to capture Firebase/APNs responses—this will tell you if certain tokens are failing. - Check Method Implementation: Make sure your notification methods aren't stopping execution early (e.g., no unexpected
returnstatements that exit the loop before processing all devices). - Batch Sending Optimization: For better performance, consider using Firebase's batch messaging APIs instead of sending individual requests. Android FCM supports sending to up to 500 tokens in one request, and iOS APNs also allows bulk payloads.
内容的提问来源于stack exchange,提问作者Vishal Hira

