1

I have an android application where I use a php scrip to send notifications from one device to another. Follows the script:

function send_notification ($tokens, $message)
    {
        $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
             'registration_ids' => $tokens,
             'data' => $message
            );
        $headers = array(
            'Authorization:key = Authorization_key_Here',
            '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;
    }

I sought methods in java to do the same this script up, and I have not found. I tried to translate the scrip to java, except that I do not understand anything curl (I'm watching some videos to better understand).

Do you know any method in java to do the same this scrip?

@Edit I saw this topic that was suggested to me. My poste has some fields that do not have in this topic, which are the fields I'm more in doubt, they are: curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode ($ fields));

Stark
  • 85
  • 1
  • 7
  • 3
    Possible duplicate of [What will be the equivalent to following curl command in java](http://stackoverflow.com/questions/24454164/what-will-be-the-equivalent-to-following-curl-command-in-java) – Rajdeep Paul Jun 29 '16 at 01:33
  • I saw this topic. My poste has some fields that do not have in this topic, which are the fields I'm more in doubt, they are: curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ headers); curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode ($ fields)); – Stark Jun 29 '16 at 01:46
  • You should consider using the GCM Java client library. https://mvnrepository.com/artifact/com.google.gcm/gcm-server/1.0.0 – Arthur Thompson Jun 29 '16 at 06:03
  • This is for GCM and i'm using FCM. – Stark Jun 30 '16 at 15:38

0 Answers0