0

This is what I wrote to get it and attempt to send a push notification:

<?php
    //send FCM notification
    $fcmToken = "My device token";

    $fcmKey = "My Firebase Cloud Messaging Server Key";

   //I tried curl like this but I  barely understand it and it wouldn't work
    //curl -X POST --header "Authorization: key=$fcmKey" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"$fcmToken\",\"notification\":{\"body\":\"Yellow\"},\"priority\":10}"

    echo "starting curl <hr>";      
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: key=$fcmKey","Content-Type: application/json","to: $fcmToken","notification:{\"body\":\"Yellow\"}","priority: 10" )); //setting custom header
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($curl);
    echo "curlResult: " . $result;
    curl_close($curl);
    echo "<hr>curl end";    
?>

The output I got was like this, why?:

starting curl <hr>curlResult: <HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://firebase.google.com/docs/cloud-messaging/http-server-ref">here</A>.
</BODY>
</HTML>
<hr>curl end

Thanks in advance for any help!

Rahul
  • 10,457
  • 4
  • 35
  • 55
Eric
  • 476
  • 2
  • 8
  • 20
  • Sounds like you're being issued a redirect. That's very common on the web and has too many reasons to guess why google did it here. You might want to look into getting Curl to follow the redirect: http://stackoverflow.com/questions/18474690/is-there-a-way-to-follow-redirects-with-command-line-curl – Philip Couling Oct 27 '16 at 19:11
  • Yeah, if I go there in my browser it takes me to: https://firebase.google.com/docs/cloud-messaging/http-server-ref which is what happens if I include: curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); But it doesn't make any sense, it's like I have the wrong URL but that's what the documentation says. – Eric Oct 27 '16 at 21:26
  • No Redirects are used for so many reasons, not just when something has moved. For example when you POST data to a website most websites (should) redirect you. This prevents the browser sending the information twice. The same is often true for logins. The first request sends the username and password and the server bounces you forward to another page while giving you a cookie for your session. Again the browser then never tries to login a second time even if you click "back". – Philip Couling Oct 28 '16 at 07:32

1 Answers1

0

I'm not sure what exactly made the difference but I ended up following the answer on this post to get it to work: Firebase Cloud Messaging, issues in receiving notification

For preservation this is what it said:

<?php 
function send_notification ($tokens)
{

    $url = 'https://fcm.googleapis.com/fcm/send';
    $priority="high";
    $notification= array('title' => 'Some title','body' => 'hi' );

    $fields = array(
         'registration_ids' => $tokens,
         'notification' => $notification

        );


    $headers = array(
        'Authorization:key=xxxxxxxxxxxxx',
        '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));
    // echo json_encode($fields);
   $result = curl_exec($ch);           
   echo curl_error($ch);
   if ($result === FALSE) {
       die('Curl failed: ' . curl_error($ch));
   }
   curl_close($ch);
   return $result;
}
$tokens = array('RECEIVER-TOKEN-1'
    ,'RECEIVER_TOKEN-2');

$message_status = send_notification($tokens);
echo $message_status;
Community
  • 1
  • 1
Eric
  • 476
  • 2
  • 8
  • 20