I am very new to PHP and trying to send firebase push notification from my server using php . for that i call a function from another class to fetch the firebase token from my server and send firebase notification
<?php
class sendAdminpush {
private $db;
function __construct()
{
//importing required files
require_once 'DbOperationF.php';
require_once 'Firebase.php';
require_once 'push.php';
$db = new DbOperationF();
}
public function sendNotificationtoAdmin($title, $message,$usertype){
$notId = rand(10,1000);
$sound = "notification";
$image= "ic_waterlogo";
//creating a new push
$push = null;
$push = new Push(
$title,
$message,
$image,
$notId,
$sound
);
//getting the push from push object
$mPushNotification = $push->getPush();
//getting the token from database object
$devicetoken = $db->getAllTokens($usertype);
//creating firebase class object
$firebase = new Firebase();
//echo "tok:".$devicetoken."and p".$mPushNotification;
//sending push notification and displaying result
echo $firebase->send($devicetoken, $mPushNotification);
}
}
//class end
?>
i called sendAdminpush from my another class but it gives an error like
PHP Fatal error: Uncaught Error: Call to a member function getAllTokens() on null in /home/ihdi/public_html/tupo.in/tupo/includes/Firebase/sendAdminpush.php:36
and my DbOperationF class
<?php
class DbOperationF
{
//Database connection link
private $conn;
//Class constructor
function __construct()
{
// //Getting the DbConnect.php file
require_once dirname(__FILE__) . '/../DbConnect.php';
//require_once '../DbConnect.php';
// //Creating a DbConnect object to connect to the database
$db = new DbConnect();
// //Initializing our connection link of this class
// //by calling the method connect of DbConnect class
$this->conn = $db->connect();
}
//getting all tokens to send push to all devices
public function getAllTokens($usertype){
echo "ui:".$token;
$stmt = $this->conn->prepare("SELECT token from fcm_token WHERE user_type=?");
$stmt->bind_param("s", $usertype);
$stmt->execute();
//$stmt->bind_result($token);
$result = $stmt->get_result();
$tokens = array();
while($token = $result->fetch_assoc()){
array_push($tokens, $token['token']);
}
return $tokens;
}
}
}
}
Help me to sort out this error and sorry for my bad english.
You are not using the $db
instance that you have created, rather you are trying to access $db
of the enclosed function. To fix this you need to use $this->db
to access the global $db
. An example:
<?php
class sendAdminpush
{
private $db;
function __construct()
{
//importing required files
require_once 'DbOperationF.php';
require_once 'Firebase.php';
require_once 'push.php';
$this->db = new DbOperationF();
}
public function sendNotificationtoAdmin($title, $message, $usertype)
{
$notId = rand(10, 1000);
$sound = "notification";
$image = "ic_waterlogo";
//creating a new push
$push = null;
$push = new Push($title, $message, $image, $notId, $sound);
//getting the push from push object
$mPushNotification = $push->getPush();
//getting the token from database object
$devicetoken = $this->db->getAllTokens($usertype);
//creating firebase class object
$firebase = new Firebase();
//echo "tok:".$devicetoken."and p".$mPushNotification;
//sending push notification and displaying result
echo $firebase->send($devicetoken, $mPushNotification);
}
}
//class end
?>