Php FCM推送通知api中的流明错误
在lumen中为FCM推送通知创建Api,但我收到错误 [numberTokensFailure:受保护]=>7。 我正在使用这个库: 编辑:如果我一个接一个地传递令牌而不是数组,那么它在工作,但在数组中它不工作。请检查我的代码并给我解决方案 Api代码:Php FCM推送通知api中的流明错误,php,firebase,firebase-cloud-messaging,lumen,Php,Firebase,Firebase Cloud Messaging,Lumen,在lumen中为FCM推送通知创建Api,但我收到错误 [numberTokensFailure:受保护]=>7。 我正在使用这个库: 编辑:如果我一个接一个地传递令牌而不是数组,那么它在工作,但在数组中它不工作。请检查我的代码并给我解决方案 Api代码: public function push_notification() { $optionBuilder = new OptionsBuilder(); $op
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();
}
}
$tokens=Auth::select('token')->get()->toArray();
$numberOfSuccess=0;
$numberOfFailure=0;
如果(计数($tokens)>0){
对于($index=0;$indexnumberSuccess();
$numberOfFailure=$numberOfFailure+$downstreamResponse->numberFailure();
}
}
注:
如果您有许多令牌要发送通知,这将需要时间。
因此,最好使用Laravel队列作业功能。请详细说明,正如我上面的代码所示,
$tokens
是一个令牌数组,您实际上是在传递令牌数组,但提到库支持传递数组,而不使用for逐个传递令牌,因此我想传递令牌数组,而不是令牌请dd()
$tokens
数组。。把它贴在这里