Home » Php » laravel – Kreait php-firebase Notifications doesn't work despite succesful send message

laravel – Kreait php-firebase Notifications doesn't work despite succesful send message

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m using this Kreait/laravel-firebase via ajax and Notifications doesn’t work despite succesful send message……………………………………..

/**
     * @param Request $request
     * @throws Firebase\Exception\FirebaseException
     * @throws Firebase\Exception\MessagingException
     */
    public function fcmSendMessageToAll(Request $request)
    {

        $firebaseInstance = new FirebaseInstance();

        //$title,$body,$imageUrl
        if ($request->input('title')==null || $request->input('body')==null){
            throw new InvalidArgumentException("Le champ titre ou texte est vide");
        }

        $firebaseUserRepository = new FirebaseUserRepo();
        try {
            $allUsers = $firebaseUserRepository->getAll();
        }catch (\Exception $exception){
            dd('erreur : '.$exception->getMessage());
        }


        $arrayTokens = array();
        foreach ($allUsers as $key => $firebaseUser) {
            if($firebaseUser->getToken() != null){
                echo 'token : '.$firebaseUser->getToken().PHP_EOL;
                $arrayTokens[] = $firebaseUser->getToken();
            }
        }

        //dd($arrayTokens);//test

        if (count($arrayTokens)>0){

            //$messaging = (new Firebase\Factory())->createMessaging();
            $messaging = $firebaseInstance->factory->createMessaging();

            $message = CloudMessage::new();


            $notification = Notification::fromArray([
                'title' => $request->input('title'),
                'body' => $request->input('body'),
                'image' => $request->input('imageurl'),
            ]);

            $message->withNotification($notification);

            try {
                $report = $messaging->sendMulticast($message, $arrayTokens);
                echo 'Successful sends: '.$report->successes()->count().PHP_EOL;
            }catch(Firebase\Exception\MessagingException $messagingException){
                echo 'Failed sends: '.$report->failures()->count().PHP_EOL;
                dd('erreur : '.$messagingException->getMessage());
            }

            if ($report->hasFailures()) {
                foreach ($report->failures()->getItems() as $failure) {
                    echo $failure->error()->getMessage().PHP_EOL;
                }
            }

        }

    }

unfortunately it says “successful sends : 1” (I’ve got just one test user with token) but no notification is in the firebase console or received on my phone ?
Do you see where is my error or is it a bug ?

How to&Answers: