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));

4
  • 3
    Possible duplicate of What will be the equivalent to following curl command in java Commented Jun 29, 2016 at 1: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)); Commented Jun 29, 2016 at 1:46
  • You should consider using the GCM Java client library. mvnrepository.com/artifact/com.google.gcm/gcm-server/1.0.0 Commented Jun 29, 2016 at 6:03
  • This is for GCM and i'm using FCM. Commented Jun 30, 2016 at 15:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.