fcm icon indicating copy to clipboard operation
fcm copied to clipboard

Fix for "A message can only have one of the following targets" error

Open mgsmus opened this issue 2 years ago • 18 comments

https://github.com/kreait/firebase-php/blob/6.x/src/Firebase/Messaging/CloudMessage.php#L75

It checks array keys but does not check if they are empty or not, so it causes "A message can only have one of the following targets: condition, token, topic, unknown" error.

mgsmus avatar Jun 09 '22 14:06 mgsmus

please merge this PR.

HAstaShakiba avatar Jun 10 '22 13:06 HAstaShakiba

@HAstaShakiba Meanwhile you can use https://github.com/symplify/vendor-patches to patch the error

mgsmus avatar Jun 10 '22 14:06 mgsmus

@atymic please merge this PR

veneliniliev avatar Jun 14 '22 09:06 veneliniliev

@atymic kindly merge this PR

ZedanLab avatar Jun 15 '22 12:06 ZedanLab

temporary resolution of the problem... add "kreait/firebase-php": "6.3.1" to composer.json

veneliniliev avatar Jun 15 '22 13:06 veneliniliev

up

hrep-john avatar Jun 23 '22 04:06 hrep-john

up

gabrieldebem avatar Jun 29 '22 16:06 gabrieldebem

@atymic Why is this PR taking so long time to merge?

mtawil avatar Jun 30 '22 11:06 mtawil

up

masterix21 avatar Jul 21 '22 11:07 masterix21

For real..? PR with such tiny changes takes forever to merge?

NViktors avatar Jul 21 '22 11:07 NViktors

@atymic, could you take a look here? Thanks

masterix21 avatar Jul 21 '22 12:07 masterix21

up @atymic

Saifallak avatar Jul 26 '22 18:07 Saifallak

👍

la5digital avatar Jul 28 '22 17:07 la5digital

any solutions found for this ?

Rignesh-EWW avatar Jul 29 '22 09:07 Rignesh-EWW

any solutions found for this ?

https://github.com/laravel-notification-channels/fcm/pull/116#issuecomment-1156475473

veneliniliev avatar Jul 29 '22 10:07 veneliniliev

any solutions found for this ?

#116 (comment)

"kreait/firebase-php": "6.3.1" => add composer file but not working create the same issue

Rignesh-EWW avatar Aug 01 '22 05:08 Rignesh-EWW

If the maintainer doesn't merge the PR, you could use your FcmMessage class to extend the original ones from the last version of the package.

namespace App\Notifications\Concerns;

class FcmMessage extends \NotificationChannels\Fcm\FcmMessage
{
    public function toArray(): array
    {
        $data = [
            'name' => $this->getName(),
            'data' => $this->getData(),
            'notification' => ! is_null($this->getNotification()) ? $this->getNotification()->toArray() : null,
            'android' => ! is_null($this->getAndroid()) ? $this->getAndroid()->toArray() : null,
            'webpush' => ! is_null($this->getWebpush()) ? $this->getWebpush()->toArray() : null,
            'apns' => ! is_null($this->getApns()) ? $this->getApns()->toArray() : null,
            'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null,
        ];

        if ($token = $this->getToken()) {
            $data['token'] = $token;
        }

        if ($topic = $this->getTopic()) {
            $data['topic'] = $topic;
        }

        if ($condition = $this->getCondition()) {
            $data['condition'] = $condition;
        }

        return $data;
    }
}

Then, replace your notifications to use your own FcmMessage like so:

class PushNotification extends Notification
{
    // ...

    public function toFcm($notifiable)
    {
        return \App\Notifications\Concerns\FcmMessage::create();
    }

    // ...
}

masterix21 avatar Aug 01 '22 10:08 masterix21

=> "A message can only have one of the following targets: condition, token, topic, unknown"

again the same issue generates.

Rignesh-EWW avatar Aug 01 '22 11:08 Rignesh-EWW

Any news here?

mwllgr avatar Aug 13 '22 15:08 mwllgr

@masterix21 The create function needs to be overridden as well.

namespace App\Notifications\Concerns;

class FcmMessage extends \NotificationChannels\Fcm\FcmMessage
{
    public static function create(): self
    {
        return new self;
    }

    public function toArray(): Array
    {
        $data = [
            'name' => $this->getName(),
            'data' => $this->getData(),
            'notification' => ! is_null($this->getNotification()) ? $this->getNotification()->toArray() : null,
            'android' => ! is_null($this->getAndroid()) ? $this->getAndroid()->toArray() : null,
            'webpush' => ! is_null($this->getWebpush()) ? $this->getWebpush()->toArray() : null,
            'apns' => ! is_null($this->getApns()) ? $this->getApns()->toArray() : null,
            'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null,
        ];

        if ($token = $this->getToken()) {
            $data['token'] = $token;
        }

        if ($topic = $this->getTopic()) {
            $data['topic'] = $topic;
        }

        if ($condition = $this->getCondition()) {
            $data['condition'] = $condition;
        }

        return $data;
    }
}
class PushNotification extends Notification
{
    // ...

    public function toFcm($notifiable)
    {
        return \App\Notifications\Concerns\FcmMessage::create();
    }

    // ...
}

iml885203 avatar Aug 16 '22 10:08 iml885203

Apologies for the delay, I'm not the maintainer of this specific package and had notifications off. I'll merge shortly, if anyone is interested in joining as maintainer please email me.

atymic avatar Aug 22 '22 00:08 atymic

@atymic - happy to help out with maintenance. I already look after the APN and Facebook channels as well.

dwightwatson avatar Aug 22 '22 00:08 dwightwatson