laravel-notification-channels / fcm

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

SendReport for one token #152

Closed Yannick-Bruening closed 11 months ago

Yannick-Bruening commented 1 year ago

Hi,

previously we were using the array "hack" to always get a multicast response. Now with the changes introduced in #130 this is no longer possible. Is there any way to get the SendReport for a single token PushNotification?

Would be resolved by #146

The code we used is below.

class TpFcmChannel extends FcmChannel
{
    public function send($notifiable, Notification $notification)
    {
        $responses = parent::send($notifiable, $notification);

        $notificationLog = $this->createNotificationLog($notification, $notifiable);

        $reports = [];
        $tokensToDelete = [];

        /* @var MulticastSendReport $multicastReport */
        foreach ($responses as $multicastReport) {
            foreach ($multicastReport->getItems() as $sendReport) {
                $reports[] = [
                    "token" => $sendReport->target()->value(),
                    "result" => $sendReport->result(),
                    "success" => $sendReport->isSuccess(),
                    "invalidTarget" => $sendReport->messageTargetWasInvalid(),
                    "invalidMessage" => $sendReport->messageWasInvalid(),
                    "unknownToken" => $sendReport->messageWasSentToUnknownToken(),
                ];
            }

            array_push($tokensToDelete, ...$multicastReport->unknownTokens(), ...$multicastReport->invalidTokens());
        }

        $this->handleReports($reports, $notificationLog);
        $this->deleteInvalidTokens($tokensToDelete);

        return $responses;
    }
}

Thanks in advance

dwightwatson commented 1 year ago

Hi - you're right, that method was simplified. sendToMulticast is still in the channel code so you could use it with a custom channel in the meantime (or lock to a previous version that supported what you're after).

However if #146 does go through then the problem should be resolved for you as well.

Yannick-Bruening commented 1 year ago

As it seems firebase has discontinued the batchSend api. Is this package using the batch send api or is it using multiple single requests

wsamoht commented 1 year ago

As it seems firebase has discontinued the batchSend api. Is this package using the batch send api or is it using multiple single requests

If you are running the latest version of this plugin (^3.0) and have done a composer update, you will be using the new Firebase HTTP V1 API for multicast. This package uses "kreait/laravel-firebase": "^5.0" which in turn uses "kreait/firebase-php": "^7.0". In kreait/firebase-php v7.5.0, it was changed to use the new API.

dwightwatson commented 11 months ago

Thanks for this - please consider using dev-master or looking at #168.