I noticed that my app was crashing if I was not passing an array as the interest to the notify
function
I have been doing this for over a year now and not until, now when I had time to investigate, did I find out why
Here is a sample code from my project.
This one is how I want the code to look
public function send()
{
$this->body();
// Send the notification using Expo for Apple and Android
$expo = Expo::normalSetup();
$interestDetails = [$this->uniqueId, $this->to];
// Subscribe the recipient to the server
$expo->subscribe($interestDetails[0], $interestDetails[1]);
// Build the notification data
$notification = [
'title' => $this->subject,
'body' => $this->body,
'badge' => 1,
'data' => json_encode([
'messageType' => $this->subject,
'message' => $this->body,
])
];
if ($this->playSound) {
$notification['sound'] = 'default';
}
// Notify an interest with a notification
$response = $expo->notify($interestDetails[0], $notification);
}
This is how I had to modify the notify
function call to make it work
public function send()
{
...
// Notify an interest with a notification
$response = $expo->notify([$interestDetails[0]], $notification);
}
Why was this happening?
ANSWER:
Expo notify function checks if the value passed is a string and if it is, it puts the string in an array and uses that as the interest
public function notify($interests, array $data, $debug = false)
{
$postData = [];
if (is_string($interests)) { // What happens if it is an integer?
$interests = [$interests];
}
if (count($interests) == 0) {
throw new ExpoException('Interests array must not be empty.');
}
// Gets the expo tokens for the interests
$recipients = $this->registrar->getInterests($interests);
foreach ($recipients as $token) {
$postData[] = $data + ['to' => $token];
}
$ch = $this->prepareCurl();
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$response = $this->executeCurl($ch);
// If the notification failed completely, throw an exception with the details
if (!$debug && $this->failedCompletely($response, $interests)) {
throw ExpoException::failedCompletelyException($response);
}
return $response;
}
In my case, I am using the table primary key id as the unique identifier and that is an integer field. If you pass an integer field to the notify function it leads to problems. Here is the error...
<strong>Message:</strong> Argument 1 passed to ExponentPhpSDK\ExpoRegistrar::getInterests() must be of the type array, integer given, called in /<path>/<to>/<project>/vendor/alymosul/exponent-server-sdk-php/lib/Expo.php on line 100
In my app, which is a slimphp app, it would lead to a 500 server error
The reason this happens is because the if statement is skipped since it is not a string and then we call getInterest()
which give us an error but not an exception. You throw an exception if there is an empty token but nothing for an interest related error
It would be nice if either some documentation is added to make sure it states to pass a string (for the unique id) or support integers or throw a proper exception if an integer is passed because you do not want this to be a possibility. In that case updating the "snippet" code in the README
to have the exception would be nice
It is your library so you can handle this as you want but please provide a solution for this.