1

I created a simple php code to send a notification to my android devices which have google log in method from firebase implementation. Their tokens are already stored in my database. When I execute my php, it doesn't send the notification. Whoever , if i send a notification trough firebase notification console it works. This is my php code .

function sendGCM($message, $registration_ids) {
    //FCM URL
    $url = "https://fcm.googleapis.com/fcm/send";
//prepare data
$fields = array (
    'registration_ids' => array ($registration_ids),
    'data' => array ("message" => $message)
);
$fields = json_encode ( $fields ); 

//header data
$headers = array ('Authorization: key=<YOUR_API_KEY>', 'Content-Type: application/json');

//initiate curl request
$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_POSTFIELDS, $fields );

// execute curl request
$result = curl_exec ( $ch );

//close curl request
curl_close ( $ch );

//return output
return $result;
}

Error: when I execute this php file, throw this error:

{

    "multicast_id": 5359746182596118281,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
    {
    "error": "InvalidRegistration"
    }
                ]
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Have you look on google for your problem ? There is a whole page about firebase error code [here](https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes) You should try to do some research before posting on Stackoverflow. Also, It's ok not to show your API key here, but make sure it's set in your code, in the headers. – Nicolas Mar 28 '17 at 19:27
  • Possible duplicate of [Where to find Reference\_Ids for Firebase Cloud Messaging?](http://stackoverflow.com/questions/41278459/where-to-find-reference-ids-for-firebase-cloud-messaging) – AL. Mar 28 '17 at 22:42
  • Possibly because you are passing an array in registartion ids so that it becomes array of array which is incorrect. Try sending a single string token id and it should work. i have already got it working. – hablema Mar 29 '17 at 05:57
  • single string? that sounds interesting, I will try it and tell you if it works –  Mar 29 '17 at 20:29
  • I already tried it and it is not that. –  Mar 29 '17 at 23:40

1 Answers1

2

Try this:-

    <?php

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'your_key' );
$registrationIds = array($id); //$id is string not array


// prep the bundle
$notification = array
(
    'title'     => 'title',
    'body'      => 'body',
    'icon'      => 'logo',
    'sound'     => 'default',
    'tag'       => 'tag',
    'color'     => '#ffffff'

);

$data = array
(
    'message' => 'message body',
    'click_action' => "PUSH_INTENT"
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'      => $notification,
    'data'              => $data,
    'priority'          => 'normal'

);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

?>
hablema
  • 540
  • 1
  • 5
  • 17
  • Thank you , I will aply it to my php file and tell if it works –  Mar 29 '17 at 20:28
  • Hello, i already tried but It says de same error. invalid registration, define( 'API_ACCESS_KEY', 'your_key' ); here i just need to change the word my key? –  Mar 29 '17 at 23:41
  • @xx_name : Yes, you need to input you key you got from Firebase console. You can find it in the Firebase console settings under Cloud messaging. I currently use the legacy server key, but firebase recommends usage of server key. you can try both. – hablema Mar 30 '17 at 06:15
  • Hello everyone, I already saw my error, it was because i was using googles log in tokens instead firebase tokens, my bad. I misunderstood the documentation, thank you helped me –  Apr 05 '17 at 20:56