我正在从laravel发送推送通知,我正在将其用作我的应用程序的后端。 我没有在Android Oreo上获得推送通知,但是在较低版本上却得到了。 要在android oreo中获取通知,我应该做些什么更改?
这是fcm.php的代码
<?php
class Fcm {
//Define SendNotification function
function sendNotification($dataArr) {
$fcmApiKey = 'SERVER_KEY';//App API Key(This is google cloud messaging api key not web api key)
$url = 'https://fcm.googleapis.com/fcm/send';//Google URL
$registrationIds = $dataArr['device_id'];//Fcm Device ids array
$message = $dataArr['message'];//Message which you want to send
$title = $dataArr['title'];
$image=$dataArr['image'];
$news_id=$dataArr['news_id'];
$random=$dataArr['random_id'];
// prepare the bundle
$msg = array('message' => $message,'title' => $title,"image"=>$image,"news_id"=>$news_id,"random_id"=>$random);
$fields = array('registration_ids' => $registrationIds,'data' => $msg);
$headers = array(
'Authorization: key=' . $fcmApiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, $url );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
$_SESSION['result']=$result;
return $result;
}
}
这是send.php的代码
<html><head><script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script></head><body>
<?php
include 'config.php';
//echo '<pre>';
$title=$_POST['title'];
$msg=$_POST['msg'];
$news_id=$_POST['news'];
//echo $news_id;
//echo msg;
//echo $image;
$sql="SELECT image FROM `app_news` where id='".$news_id."' ";
$check= mysqli_query($con, $sql);
$resultcheck= mysqli_fetch_array($check,MYSQLI_ASSOC);
$random=rand(0,999);
$image=$resultcheck['image'];
$data=array("title"=>$title,"message"=>$msg,"image"=>$image,"news_id"=>$news_id,"random_id"=>$random,"priority" => 10);
///echo'<pre><h1> Data sent through request:<br>';var_dump($data); echo '</h1><br>';
////////////////////////////////GETTING DEVICE DATA///////////////////////////////////
$sql="SELECT * FROM `devices`";
$checkdevices= mysqli_query($con, $sql);
$DeviceIdsArr= array();
//Prepare device ids array
while($rowData = mysqli_fetch_array($checkdevices,MYSQLI_ASSOC)) {
$DeviceIdsArr[] = $rowData['token'];
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////SENDING NOTIFICATION//////////////////////////////////////////
require("fcm.php");
$fcm = new Fcm();//Create Fcm class object
$dataArr = array();
$dataArr['device_id'] = $DeviceIdsArr;//fcm device ids array which is created previously
$dataArr['message'] = $data['message'];//Message which you want to send
$dataArr['image'] = $data['image'];
$dataArr['title'] = $data['title'];
$dataArr['news_id'] = $data['news_id'];
$dataArr['random_id'] = $data['random_id'];
$dataArr[ 'priority'] = $data['priority'];
//Send Notification
$fcm->sendNotification($dataArr);
//var_dump($_SESSION['result']);//
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
$results[]=json_decode($_SESSION['result']);
///////////////showing results//////////////
$multicast_id=$results[0]->multicast_id;
$success=$results[0]->success;
$fail=$results[0]->failure;
$message_id=$results[0]->results[0]->message_id;
///////////////////////////////////////////////////
if($success!=''){?>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script type="text/javascript">
swal({
title: "Notification sent",
text: "successfully ",
icon: "success",button: "close"
}).then(function() {
//Redirect the user
window.location.href = "notification.php";
//console.log('The Ok Button was clicked.');
});
</script>
<?php } ?>