Skip to content

Commit

Permalink
Uses curl_multi for apple push
Browse files Browse the repository at this point in the history
  • Loading branch information
wess committed Sep 14, 2023
1 parent fcde4b4 commit 2bb0922
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
46 changes: 43 additions & 3 deletions src/Utopia/Messaging/Adapters/Push/APNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,55 @@ private function notify(array $to, array $payload): array

$response = '';

$mh = curl_multi_init();
$handles = [];

// Create a handle for each request
foreach ($to as $token) {
curl_setopt($ch, CURLOPT_URL, $this->endpoint.'/3/device/'.$token);

$response = curl_exec($ch);
$handle = curl_copy_handle($ch);
curl_multi_add_handle($mh, $handle);

$handles[] = $handle;
}

$active = null;
$status = CURLM_OK;

// Execute the handles
while ($active && $status == CURLM_OK) {
$status = curl_multi_exec($mh, $active);
}

// Check each handle's result
$responses = [];
foreach ($handles as $ch) {
$urlInfo = curl_getinfo($ch);
$device = basename($urlInfo['url']); // Extracts deviceToken from the URL

if (! curl_errno($ch)) {
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responses[] = [
'device' => $device,
'status' => 'success',
'statusCode' => $statusCode,
];
} else {
$responses[$device] = [
'status' => 'error',
'error' => curl_error($ch),
];
}

curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}

curl_close($ch);
curl_multi_close($mh);
curl_share_close($sh);

return $this->formatResponse($response);
return $responses;
}

private function formatResponse(string $response): array
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/Push/APNSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function testSend(): void
badge: '1'
);

$response = (array) \json_decode($adapter->send($message));
$responses = \json_decode($adapter->send($message));

$this->assertEquals('200', $response['status']);
$this->assertArrayHasKey('apns-id', $response);
$this->assertArrayHasKey('apns-unique-id', $response);
foreach ($responses as $response) {
$this->assertEquals('success', $response->status);
}
}
}

0 comments on commit 2bb0922

Please sign in to comment.