Я не получаю push-уведомление на Android Oreo, но на более низких версиях я получаю - PullRequest
0 голосов
/ 07 ноября 2018

Я отправляю push-уведомление от laravel, которое я использую в качестве бэкэнда для моего приложения. Я не получаю push-уведомление на 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 } ?>
...