success = []; $output->invalid = []; $output->needUpdate = []; $url = 'https://fcm.googleapis.com/fcm/send'; $api_key = $this->_config['api_key']; $headers = array( 'Authorization' => 'key=' . $api_key, 'Content-Type' => 'application/json', ); // Set notification $notification = $message->getMetadata(); $subject = $message->getSubject(); $content = $message->getContent(); if ($subject !== '' || $content !== '') { $notification['title'] = $subject; $notification['body'] = $content; } if (count($notification)) { $notification['sound'] = isset($notification['sound']) ? $notification['sound'] : 'default'; } $chunked_token = array_chunk($tokens, 500); foreach($chunked_token as $token_unit) { $data = [ 'registration_ids' => $token_unit, 'priority' => 'normal', 'data' => $message->getData() ?: new \stdClass, ]; if (count($notification)) { $data['notification'] = $notification; } $response = HTTP::request($url, 'POST', json_encode($data), $headers); if($response->getStatusCode() === 200) { $decoded_response = json_decode($response->getBody()); if(!$decoded_response) { $message->addError('FCM error: Invalid Response: '. $response); return $output; } $results = $decoded_response->results ?: []; foreach($results as $i => $result) { if($result->error) { $message->addError('FCM error: '. $result->error); $output->invalid[$token_unit[$i]] = $token_unit[$i]; } else if($result->message_id && $result->registration_id) { $output->needUpdate[$token_unit[$i]] = $result->registration_id; } else { $output->success[$token_unit[$i]] = $result->message_id; } } } else { $message->addError('FCM error: HTTP ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); } } return $output; } }