Closed JustVlad124 closed 4 weeks ago
Where's the 501 coming from - your app or the API request?
There's not really enough information to go off of here. You can listen for the NotificationFailed
event to see if that provides more information, debug the channel code or try using laravel-firebase
directly to see if your credentials are alright.
[2024-10-23 09:10:17] development.ERROR: FCM Notification Failed {"error_message":"Operation is not implemented, or supported, or enabled.","error_code":501,"notification_id":"ac5643fd-af50-4255-89f5-7a8d7870c792","notifiable_type":"App\\Models\\User","notifiable_id":"9d27eb7f-1aeb-48d0-b439-abf57a111e36","data":{"report":{"Kreait\\Firebase\\Messaging\\SendReport":[]}},"target":"...token...","timestamp":"2024-10-23 09:10:17"}
@dwightwatson This is what I receive in laravel.log file. I’ll try to send a notification using kreait/laravel-firebase and come back if the problem is not in credentials. Thanks for response anyway.
update:
use Kreait\Firebase\Factory;
$factory = (new Factory)->withServiceAccount('path/to/serviceAccount.json');
$messaging = $factory->createMessaging();
try {
$message = [
'notification' => [
'title' => 'Test',
'body' => 'Test message'
],
'token' => 'device-token'
];
$response = $messaging->send($message);
Log::info('FCM Test Response', ['response' => $response]);
} catch (\Exception $e) {
Log::error('FCM Test Error', [
'error' => $e->getMessage(),
'code' => $e->getCode()
]);
}
I sent a message using the kreait/laravel-firebase library and the notification arrived successfully. So I guess the problem is not in credentials then what is it?
Cool - can you dig into the channel then and see what's going wrong? The channel is a thin wrapper around the firebase package. Step through it and see if it's being passed the correct credentials and tokens.
@dwightwatson, сan I get more specific instructions on what to do? I don’t fully understand where exactly fcm gets credentials?
The FcmChannel
receives an instance of the Firebase messaging client.
Normally you would use the FIREBASE_CREDENTIALS
environment variable to provide the configuration. You will need to review the kreait/laravel-firebase
docs to make sure it is configured correctly.
If it is still not working, go into your app and step through the send
method in the FcmChannel
dumping out the tokens to make sure they are correct, and that the Messaging
client has the right credentials available.
@dwightwatson
Kreait/Firebase/Factory
public function withServiceAccount(string|array $value): self
{
$serviceAccount = $value;
if (is_string($value) && str_starts_with($value, '{')) {
try {
$serviceAccount = Json::decode($value, true);
} catch (UnexpectedValueException $e) {
throw new InvalidArgumentException('Invalid service account: '.$e->getMessage(), $e->getCode(), $e);
}
} elseif (is_string($value)) {
try {
$serviceAccount = Json::decodeFile($value, true);
} catch (UnexpectedValueException $e) {
throw new InvalidArgumentException('Invalid service account: '.$e->getMessage(), $e->getCode(), $e);
}
}
$factory = clone $this;
$factory->serviceAccount = $serviceAccount;
return $factory;
}
This method returns correct credentials. At this point I really don't understand what;s going on.
About FIREBASE_CREDENTIALS
I configure It in .env file and config/firebase.php too. But it doesn't work.
'projects' => [
'app' => [
'credentials' => env('FIREBASE_CREDENTIALS', env('GOOGLE_APPLICATION_CREDENTIALS')),
],
]
@JustVlad124 Hello sir, I also have the same problem, suddenly my fcmtoken doesn't work anymore, I'm using version 2.7.0 and in laravel 8.83, please help me how to fix it, because when I try to debug I get that the message is sent but the notification on the mobile doesn't appear. I really hope for help.
local.INFO: FCM Message Created: {"message":"{\"name\":null,\"data\":null,\"notification\":{\"title\":\"XXXX\",\"body\":\"Pesan default untuk notifikasi.\",\"image\":null},\"android\":null,\"webpush\":null,\"apns\":null,\"fcm_options\":null}"}
@syahroel712 It turned out that some libraries in composer that are in our project do not allow fcm to use some functions. For example, the laravel-notification-channels/fcm library is required for some/package:5.1.0 to work and there is another library that also requires this library but version 4.8.0: some/package:4.8.0. And therefore, when trying to send a notification, it calls a function that is not supported by the laravel-notification-channels/fcm library. At least I think this may be the reason.
What decision did I come to? I found a version that does not conflict with dependencies, in my case it is laravel-notification-channels/fcm:3.2.1. I hope I was able to clarify the problem a little.
@JustVlad124 ok sir, thanks for your help
Recently, push notifications stopped working in the project I'm working on. All this time, the project had laravel-notification-channels/fcm version 2.7.0. I thought the problem might be that the library was too outdated and stopped working for this reason. I decided to update the version to 3.2.0 (as much as the Laravel version allowed). Locally everything worked great, notifications were being sent and worked perfectly. I deployed everything to dev and on dev the notifications weren't coming through. I checked console.cloud.google.com to see what the error might be and saw that every request I send from dev returns a 501 error. Second part of the problem: I decided to update the Laravel version in the project from 9 to 10, and accordingly updated the laravel-notification-channels/fcm library version to the latest. After I rewrote all the legacy code, I decided to test sending notifications in the local environment. And the error repeated - when trying to send a push notification from the local environment, requests return a 501 error. Can you tell me what might be causing this behavior? I'll appreciate any response.