在流明中为FCM推送通知创建Api我得到了错误 [numberTokensFailure:protected] => 7。 我正在使用这个库:https://github.com/brozot/Laravel-FCM
编辑:如果我一个接一个地传递令牌的数组然后它的工作,但在数组中它不工作。请检查我的代码并给我解决方案。
Api代码:
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();
}
答案 0 :(得分:0)
您需要循环遍历令牌数组。
$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();
}
}
注意: 如果您有许多令牌要发送通知,这将需要一些时间。 因此,最好使用Laravel Queue Job功能。