<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Sendpush extends Model
{
    use HasFactory;
    function sendPushAndroid($user_id,$device_token,$message) {
        
        $path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
      
        $fields = array(
            'to' => $device_token,
            'data' => array('message' => $message),
            'priority' => 'high'
        );
        $headers = array(
            'Authorization:key=AAAAkDUoqyM:APA91bGD1ROKkH-Q7Xcv4KnWhio-tNxTtJSdCB3iGbiQ-qyKRp67m-PiRB4T0qe82XRM2RMQf22SdisAXD9rJR5Ia7RO8lnPPe8UrluMRnbhlQ0ZKq55xYIcn8EzBfix2XXqojKiTo0T',
            'Content-Type:application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
        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_IPRESOLVE, CURL_IPRESOLVE_V4 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        $result = curl_exec($ch);

        curl_close($ch);

        return $result;
    }

    //send push for ios
    function sendPushIOS($device_token, $message, $title) {
       
        $ch = curl_init("https://fcm.googleapis.com/fcm/send");
        //The device token.
        $token = $device_token; //token here
        //Title of the Notification.
        //$title = $msg;
        //Body of the Notification.
        $body = $message;
        //Creating the notification array.
        $notification = array('title' => $title, 'body' => $message['message'], 'message' => $body, "sound" => "default", "badge" => 1);
        //This array contains, the token and the notification. The 'to' attribute stores the token.
        $arrayToSend = array('to' => $token, 'notification' => $notification, 'priority' => 'high');
        //Generating JSON encoded string form the above array.
        $json = json_encode($arrayToSend);
        //Setup headers:
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Authorization: key=AAAAkDUoqyM:APA91bGD1ROKkH-Q7Xcv4KnWhio-tNxTtJSdCB3iGbiQ-qyKRp67m-PiRB4T0qe82XRM2RMQf22SdisAXD9rJR5Ia7RO8lnPPe8UrluMRnbhlQ0ZKq55xYIcn8EzBfix2XXqojKiTo0T';
        //Setup curl, add headers and post parameters.
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        //Send the request
        $response = curl_exec($ch);
        //Close request
        curl_close($ch);
        return $response;
    }
}
