即使是iOS设备也可以从FCM控制台接收通知。
控制器函数:
public function push(Request $request)
{
$validator = Validator::make($request->all(), [
'title' = > 'required',
'body' = > 'required',
'token' = > 'required',
'type' = > 'required',
'id' = > 'required',
]);
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
$title = $request['title'];
$body = $request['body'];
$type = $request['type'];
$id = $request['id'];
$dataarray = array(
"id" = >$id,
"type" = >$type,
'title' = >$title,
'body' = >$body,
'image' = >'321451_v2.jpg',
);
$token = $request['token'];
return $push = Push::sendpush($title, $body, $dataarray, $token);
}
push model function :
public static function sendpush($title, $body, $dataarray, $token)
{
$optionBuiler = new OptionsBuilder();
$optionBuiler->setTimeToLive(60 * 20);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notificationBuilder->setBody($body)
->setSound('');
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData($dataarray);
$option = $optionBuiler->build();
$notification = $notificationBuilder->build();
$data = $dataBuilder->build();
$token = $token;
$downstreamResponse = FCM::sendTo($token, $option, $notification, $data);
return new JsonResponse(array('status' = >'1', 'sucess' = >$downstreamResponse->numberSuccess(), 'fail' = > $downstreamResponse->numberFailure(), 'msg' = >$downstreamResponse->tokensWithError()), 200);
}
Response:
{
"status": "1",
"sucess" : 0,
"fail" : 1,
"msg" : []
}