0

I am trying to implement a php curl fcm topic broadcasting and it just displays {"message_id":4731721763997571462} and does not deliver.

I have gone through a lot of search and to no end, I can't seem to find the problem.

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Legacy_server_key' );

// prep the bundle
$msg = array
(
'message'   => 'here is a message. message',
'title'     => 'This is a title. title',

);
$fields = array
(
  'to'  => "/topics/hello",
  'notification'            => $msg
);

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

I just expect to get a notification on the device and maybe there is a way I can trace successful notifications on the google console?

samstag
  • 1
  • 2
  • You need to add registration id, you are not adding it into your field or header you are sending. – Badrinath Oct 02 '19 at 07:13
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Oct 02 '19 at 22:39

2 Answers2

0

You need to add registration ids with field parameter

// API access key from Google API's Console
    define( 'API_ACCESS_KEY', 'Legacy_server_key' );
    $registrationIds = array( "/topics/obajemusagmailcom" );
    // prep the bundle
    $msg = array
    (
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',

    );
    $fields = array
    (
      //'to'  => "/topics/hello",
      'registration_ids' => $registrationIds,
      'notification'            => $msg
    );

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

and $registrationIds = array( "/topics/obajemusagmailcom" ); is not registration id of any app, please correct it.

Badrinath
  • 501
  • 5
  • 14
  • please where do i get this registeration id, is it the server id ? and where do i put the topic i'm trying to broadcast – samstag Oct 02 '19 at 07:21
  • from your application you get individual apps registration id, See the tutorial for more info https://firebase.google.com/docs/cloud-messaging/android/client – Badrinath Oct 02 '19 at 07:28
  • it's still same result after adding the token , it just keeps printing {"message_id":7769633103336292306} plus where do i specify the topic i'm trying to send on the server side, i saw you commented out the to fragment – samstag Oct 02 '19 at 07:34
  • $registrationIds = array( "token" ); thats what i did – samstag Oct 02 '19 at 07:37
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Oct 02 '19 at 22:39
0

Finally after much horror and tries, turned out the array fields were incorrect and were meant to be

$msg = array
(
    'body'   => $body,
    'title'=>$title
);
$data = array
(
    'to'  => "/topics/".$topic,

    'notification' => $msg
);
Dharman
  • 30,962
  • 25
  • 85
  • 135
samstag
  • 1
  • 2