FCM 推送通知 API 中的流明错误



为 FCM 推送通知在流明中创建 API,但我收到错误 [数字令牌失败:受保护] => 7. 我正在使用这个库:https://github.com/brozot/Laravel-FCM

已编辑:如果我一个接一个地传递令牌而不是数组,那么它就可以工作,但在数组中它不起作用。 请检查我的代码并给我解决方案。

接口码 :

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();
}

您需要遍历令牌数组。

$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队列作业功能。

相关内容

  • 没有找到相关文章

最新更新