I'm working on an app and have gotten FCM to send to notifications to Android devices, but currently having issues with sending to iOS devices.
I have followed the steps to do this as listed here, which also includes the steps listed here.
Here is the FCM SDK code that I have that works with Android devices:
// This assumes that you have placed the Firebase credentials in the same directory
// as this PHP file.
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');
// get our Firebase account loaded
$firebase = (new Firebase\Factory())
->withServiceAccount($serviceAccount)
->create();
// create a message
$messaging = $firebase->getMessaging();
// Creating a notification
$notification = Notification::fromArray([
'title' => $title,
'body' => $body,
]);
$data = [
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
'fromid' => $fromid,
];
// Compose the message; notification + device token
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification($notification)
->withData($data);
// validate the message
try {
$messaging->validate($message);
} catch (InvalidMessage $e){
print_r($e->errors());
exit;
}
// send message
$report = $messaging->send($message);
I have tried sending messages through the Firebase console and using the FCM SDK; neither of which have caused a notification to appear on iOS devices. The console gives me no feedback, but the SDK gives me an error:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://fcm.googleapis.com/v1/projects/apwt-app1/messages:send
resulted in a 401 Unauthorized
response:
{
"error": {
"code": 401,
"message": "Auth error from APNS or Web Push Service",
"status": "UNAUTHENTICATE (truncated...)
in /var/www/vhosts/globalsups/composer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113