Elias Ghali
Elias Ghali

Reputation: 853

FCM notification for android

I am trying to send FCM notifications using a PHP script, on database insert event, I tried to send from FCM console, it worked fine using the topic option, knowing that I registered the app instance to some topic "exptopic" using the following code in the MainActivity.java OnCreate class:

FirebaseMessaging.getInstance().subscribeToTopic("exptopic");

Now the PHP script is as follows:

function sendMessage($data,$target){
    //FCM api URL
    $url = 'https://fcm.googleapis.com/fcm/send';

    //api_key available in Firebase console -> project settings -> cloud messaging -> server key

    $server_key = "API_KEY";//my api key

    $fields = array();
    $fields['data'] = array('message'=>$data);
    $fields['to'] = $target;

    $headers = array('content-type:application/json',
            'Authorization:key='.$server_key);

    $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('FCM Send error: '.curl_error($ch));
        echo 'notification not sent';
    }
    curl_close($ch);
    echo $result;
    return $result;
}

Obviously $target = "exptopic", when I run the snedMessage function, it returns the following result:

{"multicast_id":6779996340041741184,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

Can anyone help please?

Upvotes: 1

Views: 1124

Answers (2)

Geoherna
Geoherna

Reputation: 3534

You are missing the priority field in the data. Try this:

$fields["priority"] = "high" //This is required

EDIT:

I am having success with this body. Maybe give this a try:

<?php

// Get cURL resource
$ch = curl_init();

// Set url
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');

// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// Set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  "Authorization: key=API_KEY",
  "Content-Type: application/json",
 ]
);
// Create body
$json_array = [
        "notification" => [
            "title" => "TEST",
            "sound" => "default",
            "body" => $data
        ],
        "to" => $target,
        "priority" => "high"
    ]; 
$body = json_encode($json_array);

// Set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// Send the request & save response to $resp
$resp = curl_exec($ch);

if(!$resp) {
  die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
} else {
  echo "Response HTTP Status Code : " . curl_getinfo($ch,     CURLINFO_HTTP_CODE);
  echo "\nResponse HTTP Body : " . $resp;
}

// Close request to clear up some resources
curl_close($ch);

Upvotes: 0

Bob Snyder
Bob Snyder

Reputation: 38289

Based on this documentation, the to field should be /topics/exptopic

Upvotes: 1

Related Questions