laravel-notification-channels / fcm

Firebase Cloud Messaging (FCM) notifications channel for Laravel
https://laravel-notification-channels.com/
MIT License
491 stars 125 forks source link

Updated the library version, now when trying to send a push notification, I get a 501 error #208

Open JustVlad124 opened 5 hours ago

JustVlad124 commented 5 hours ago

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.

dwightwatson commented 5 hours 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.

JustVlad124 commented 5 hours ago

[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?

dwightwatson commented 4 hours ago

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.

JustVlad124 commented 3 hours ago

@dwightwatson, сan I get more specific instructions on what to do? I don’t fully understand where exactly fcm gets credentials?

dwightwatson commented 2 hours ago

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.