Currently I am working on a project which was created with Symfony 2.8 and have some problems when trying to migrate to the FCM HTTP V1 API. I have no idea what is the procedure and would be very grateful for your help in solving this problem. Here are the relevant code snippets for the current FCM implementation:
class HomeController extends Controller
{
function send_notificationToken ($tokens, $message,$key)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = '.$key,
'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_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
function send_notification ($tokens, $message,$key)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => '/topics/Flixo',
'data' => $message
);
$headers = array(
'Authorization:key = '.$key,
'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_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
public function privacypolicyAction() {
$em = $this->getDoctrine()->getManager();
$setting = $em->getRepository("AppBundle:Settings")->findOneBy(array(), array());
return $this->render("AppBundle:Home:privacypolicy.html.twig", array("setting" => $setting));
}
public function apprefundpolicyAction() {
$em = $this->getDoctrine()->getManager();
$setting = $em->getRepository("AppBundle:Settings")->findOneBy(array(), array());
return $this->render("AppBundle:Home:apprefundpolicy.html.twig", array("setting" => $setting));
}
public function notifChannelAction(Request $request)
{
$imagineCacheManager = $this->get('liip_imagine.cache.manager');
$em=$this->getDoctrine()->getManager();
$defaultData = array();
$form = $this->createFormBuilder($defaultData)
->setMethod('GET')
->add('title', TextType::class)
->add('message', TextareaType::class)
->add('object', 'entity', array('class' => 'AppBundle:Channel'))
->add('icon', UrlType::class,array("label"=>"Large Icon","required"=>false))
->add('image', UrlType::class,array("label"=>"Big Picture","required"=>false))
->add('send', SubmitType::class,array("label"=>"Send notification"))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$selected_channel = $em->getRepository("AppBundle:Channel")->find($data["object"]);
$message = array(
"type"=> "channel",
"id"=> $selected_channel->getId(),
"title"=> $data["title"],
"message"=>$data["message"],
"image"=> $data["image"],
"icon"=>$data["icon"]
);
$setting = $em->getRepository('AppBundle:Settings')->findOneBy(array());
$key=$setting->getFirebasekey();
$message_image = $this->send_notification(null, $message,$key);
$this->addFlash('success', 'Operation has been done successfully ');
}
return $this->render('AppBundle:Home:notif_channel.html.twig',array(
"form"=>$form->createView()
));
}
Thanks in advance.