tomatophp / filament-alerts

Send notification to users using notification templates and multi notification channels, it's support Filament Native Notification Service with macro, and a full integration to FCM service worker notifications
https://tomatophp.com/en/open-source/filament-alerts
MIT License
22 stars 5 forks source link

No registration tokens provided in /vendor/kreait/firebase-php/src/Firebase/Messaging/RegistrationTokens.php:61 #3

Closed momostafa closed 4 months ago

momostafa commented 4 months ago

Hi,

I am getting below error when trying to send using FCM

No registration tokens provided in /vendor/kreait/firebase-php/src/Firebase/Messaging/RegistrationTokens.php:61

Kreait\Firebase\Exception\Messaging\InvalidArgument: No registration tokens provided in /vendor/kreait/firebase-php/src/Firebase/Messaging/RegistrationTokens.php:61
Stack trace:
#0 /vendor/kreait/firebase-php/src/Firebase/Messaging.php(74): Kreait\Firebase\Messaging\RegistrationTokens::fromValue(Array)
#1 /vendor/tomatophp/fcm-notifications/src/FcmChannel.php(46): Kreait\Firebase\Messaging->sendMulticast(Object(Kreait\Firebase\Messaging\CloudMessage), Array)
#2 [internal function]: NotificationChannels\Fcm\FcmChannel->NotificationChannels\Fcm\{closure}(Object(Illuminate\Support\Collection), 0)
#3 /vendor/laravel/framework/src/Illuminate/Collections/Arr.php(605): array_map(Object(Closure), Array, Array)
#4 /vendor/laravel/framework/src/Illuminate/Collections/Collection.php(758): Illuminate\Support\Arr::map(Array, Object(Closure))
#5 /vendor/tomatophp/fcm-notifications/src/FcmChannel.php(46): Illuminate\Support\Collection->map(Object(Closure))
#6 /vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(148): NotificationChannels\Fcm\FcmChannel->send(Object(App\Models\User), Object(TomatoPHP\FilamentFcm\Notifications\FCMNotificationService))
#7 /vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(106): Illuminate\Notifications\NotificationSender->sendToNotifiable(Object(App\Models\User), 'b9305c97-2d7b-4...', Object(TomatoPHP\FilamentFcm\Notifications\FCMNotificationService), 'NotificationCha...')
#8 /vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\Notifications\NotificationSender->Illuminate\Notifications\{closure}()
#9 /vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(101): Illuminate\Notifications\NotificationSender->withLocale(NULL, Object(Closure))
#10 /vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(79): Illuminate\Notifications\NotificationSender->sendNow(Object(Illuminate\Database\Eloquent\Collection), Object(TomatoPHP\FilamentFcm\Notifications\FCMNotificationService))
#11 /vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php(39): Illuminate\Notifications\NotificationSender->send(Object(Illuminate\Database\Eloquent\Collection), Object(TomatoPHP\FilamentFcm\Notifications\FCMNotificationService))
#12 /vendor/laravel/framework/src/Illuminate/Notifications/RoutesNotifications.php(18): Illuminate\Notifications\ChannelManager->send(Object(App\Models\User), Object(TomatoPHP\FilamentFcm\Notifications\FCMNotificationService))
#13 /vendor/tomatophp/filament-fcm/src/Jobs/NotifyFCMJob.php(57): App\Models\User->notify(Object(TomatoPHP\FilamentFcm\Notifications\FCMNotificationService))
#14 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob->handle()
#15 /vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#16 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#17 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#18 /vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#19 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\Container\Container->call(Array)
#20 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}(Object(TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob))
#21 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob))
#22 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#23 /vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): Illuminate\Bus\Dispatcher->dispatchNow(Object(TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob), false)
#24 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}(Object(TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob))
#25 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob))
#26 /vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(123): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#27 /vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(71): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware(Object(Illuminate\Queue\Jobs\RedisJob), Object(TomatoPHP\FilamentFcm\Jobs\NotifyFCMJob))
#28 /vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(102): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\RedisJob), Array)
#29 /vendor/laravel/framework/src/Illuminate/Queue/Worker.php(439): Illuminate\Queue\Jobs\Job->fire()
#30 /vendor/laravel/framework/src/Illuminate/Queue/Worker.php(389): Illuminate\Queue\Worker->process('redis', Object(Illuminate\Queue\Jobs\RedisJob), Object(Illuminate\Queue\WorkerOptions))
#31 /vendor/laravel/framework/src/Illuminate/Queue/Worker.php(333): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\RedisJob), 'redis', Object(Illuminate\Queue\WorkerOptions))
#32 /vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(139): Illuminate\Queue\Worker->runNextJob('redis', 'default', Object(Illuminate\Queue\WorkerOptions))
#33 /vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(122): Illuminate\Queue\Console\WorkCommand->runWorker('redis', 'default')
#34 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#35 /vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#36 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#37 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#38 /vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#39 /vendor/laravel/framework/src/Illuminate/Console/Command.php(213): Illuminate\Container\Container->call(Array)
#40 /vendor/symfony/console/Command/Command.php(279): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#41 /vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#42 /vendor/symfony/console/Application.php(1047): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#43 /vendor/symfony/console/Application.php(316): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#44 /vendor/symfony/console/Application.php(167): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#45 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(196): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#46 /artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#47 {main}
momostafa commented 4 months ago

I managed to fix above error but no notifications received at all and i am getting below error at google developer tools

Violation] Only request notification permission in response to a user gesture.

token-manager.ts:167 Uncaught (in promise) DOMException: Failed to execute 'subscribe' on 'PushManager': The provided applicationServerKey is not valid.
    at getPushSubscription (https://www.gstatic.com/firebasejs/10.12.2/firebase-messaging.js:1:20047)
    at async getTokenInternal (https://www.gstatic.com/firebasejs/10.12.2/firebase-messaging.js:1:19920)
    at async https://bulksms.test/admin/user-notifications:7101:29

Appreciate your response on this issues thanks

momostafa commented 4 months ago

firebase-messaging-sw.js

// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');

try
{
    // Initialize the Firebase app in the service worker by passing in
    // your app's Firebase config object.
    // https://firebase.google.com/docs/web/setup#config-object
    firebase.initializeApp({
        apiKey: "{{ apiKey }}",
        authDomain: "{{ authDomain }}",
        databaseURL: "{{ databaseURL }}",
        projectId: "{{ projectId }}",
        storageBucket: "{{ storageBucket }}",
        messagingSenderId: "{{ messagingSenderId }}",
        appId: "{{ appId }}",
        measurementId: "{{ measurementId }}"
    });

    // Retrieve an instance of Firebase Messaging so that it can handle background
    // messages.
    const messaging = firebase.messaging();

    messaging.onBackgroundMessage((payload) => {
    //
        {{ sound }}

        let options = {
            body: "",
            icon: "",
            image: "",
            tag: "alert",
        };

        if(payload.data.body){
            options.body = payload.data.body;
        }

        if(payload.data.image){
            options.icon = payload.data.image;
        }

        let notification = self.registration.showNotification(
            payload.data.title,
            options
        );

        if(payload.data.url){
            // link to page on clicking the notification
            notification.onclick = (payload) => {
                window.open(payload.data.url);
            };
        }
    });
}
catch(e) {
    console.log(e)
}
3x1io commented 4 months ago

@momostafa it's easy, your user don't have a token, that's a problem that mean you need to add

use InteractsWithFCM; 

to your user model

momostafa commented 4 months ago

@momostafa it's easy, your user don't have a token, that's a problem that mean you need to add

use InteractsWithFCM; 

to your user model

I already had this in place, I have removed your plugin and implemented my own. Thanks for your reply