Krunal
Krunal

Reputation: 938

Lumen error in FCM push notification api

Create Api in lumen for FCM push notification bu I got the error [numberTokensFailure:protected] => 7. I am using this library : https://github.com/brozot/Laravel-FCM

Edited: If I pass token one by one insted of array then its working but in array its not working. please check my code and give me solution.

Api code :

public  function push_notification() {
                    $optionBuilder = new OptionsBuilder();
                    $optionBuilder->setTimeToLive(60*20);

                    $notificationBuilder = new PayloadNotificationBuilder('my title');
                    $notificationBuilder->setBody('Hello world')
                    ->setSound('default');

                    $dataBuilder = new PayloadDataBuilder();
                    $dataBuilder->addData(['a_data' => 'my_data']);

                    $option = $optionBuilder->build();
                    $notification = $notificationBuilder->build();
                    $data = $dataBuilder->build();

                    // You must change it to get your tokens
                    $tokens = Auth::select('token')->get()->toArray();

                    $downstreamResponse = FCM::sendTo($tokens, $option, $notification, $data);

                    $downstreamResponse->numberSuccess();
                    $downstreamResponse->numberFailure();
                    print_r($downstreamResponse);
                    $downstreamResponse->numberModification();

                    //return Array - you must remove all this tokens in your database
                    $downstreamResponse->tokensToDelete();

                    //return Array (key : oldToken, value : new token - you must change the token in your database )
                    $downstreamResponse->tokensToModify();

                    //return Array - you should try to resend the message to the tokens in the array
                    $downstreamResponse->tokensToRetry();

                    // return Array (key:token, value:errror) - in production you should remove from your database the tokens present in this array
                    $downstreamResponse->tokensWithError();
                }

Upvotes: 1

Views: 1174

Answers (1)

Mujahid Abbas
Mujahid Abbas

Reputation: 11

You need to loop through the array of tokens.

 $tokens = Auth::select('token')->get()->toArray();
 $numberOfSuccess = 0;
 $numberOfFailure = 0;
   if(count($tokens)>0){
        for ($index = 0; $index < count($tokens); $index++) {
                    $singleToken = array($tokens[$index]['token']);

                    $downstreamResponse = FCM::sendTo($singleToken, $option, $notification, $data);

                    $numberOfSuccess = $numberOfSuccess + $downstreamResponse->numberSuccess();
                    $numberOfFailure = $numberOfFailure + $downstreamResponse->numberFailure();      

            }
        }

Note: This will take time if you have many tokens to send notification. So, its better to use Laravel Queue Job functionality.

Upvotes: 1

Related Questions