0

I want to send firebase notification to all registered token in mysql database for android devices, am using the normal php way of creating array of recipient tokens so that i can send the notification, but when i send notification non of the registered devices receive it. I want to know if there is the need of adding some piece of php codes to work with the firebase so that the job get done, If so how and where should i use it based on my codes or what is wrong with my codes and how can i fix it?

// here is the code for sending notification

 <?php
  $message =$_POST['message'];
     $title=$_POST['title'];
 //firebase server  url
  $path_to_fcm='https://fcm.googleapis.com/fcm/send';
  // Here is the server_key
   $server_key="##########################";
   $conn = mysqli_connect("localhost","db_username","password","database_name");
   $sql = "select * from fcm_info";
   $result = mysqli_query($conn,$sql);
  //HERE IS THE TOKEN 
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
    while ($row = mysqli_fetch_assoc($result)) {
        $tokens[] = $row["fcm_token"];
    }
}
///  HERE IS THE KEY SINGLE RECIPIENT IT WORKS FINE WHEN I INTRODUCE TO THE      RECIPIENT FIELD("to" field)
$key="############";
 $headers= array(
     'Authorization:key='.$server_key, 
     'Content-Type:application/json'
           );
              // here use $tokens as the replace $key
$fields= array('to'=>$token,
'notification'=>array('title'=>$title,'body'=>$message,
  'click_action'=>'com.jrcomjk.firebasemysql_TARGET_NOTIFICATION'
));
   $payload= json_encode($fields);
   $curl_session= curl_init();
   curl_setopt($curl_session,CURLOPT_URL,$path_to_fcm);
   curl_setopt($curl_session,CURLOPT_POST,true);
   curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers);
   curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,true);
   curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER,false);
   curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
   curl_setopt($curl_session,CURLOPT_POSTFIELDS,$payload);
       $result =curl_exec($curl_session);     
       curl_close($curl_session); 
   mysqli_close($conn);
?>
f2k
  • 99
  • 11
  • http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ check this – Zaki Pathan Feb 21 '17 at 06:24
  • Single device notification from Firebase console is working? First check single device notification from Firebase console and then go further – Zaki Pathan Feb 21 '17 at 06:25
  • Is there an error response? You're aiming to send to *all your users* but in your code, you're using `to` parameter which is used for a single token. See my [answer here](http://stackoverflow.com/a/39547590/4625829) to see what you can use to send to Multiple Devices. – AL. Feb 21 '17 at 06:48
  • yeah it works fine @ZakiPathan – f2k Feb 21 '17 at 06:56
  • https://firebase.google.com/docs/cloud-messaging/android/send-multiple check this doc – Zaki Pathan Feb 21 '17 at 06:57
  • ok let me check it @Zaki Pathan – f2k Feb 21 '17 at 07:09

2 Answers2

1

I see that you declare :$tokens = array(); with "s" but you are using $token without "s" here :

   $fields= array('to'=>$token'notification'=>array('title'=>$title,'body'=>$message,'click_action'=>'com.jrcomjk.firebasemysql_TARGET_NOTIFICATION'));
Seddik Fredj
  • 123
  • 2
  • 13
0

Your code should work, just make there you are configured the firebase project which on firebase console correctly and put the server key accordingly.

aznelite89
  • 2,025
  • 4
  • 21
  • 32
  • i have done that , it work fine if i define recipient token in codes, the problem is using registered token from the database@aznelite89 – f2k Feb 21 '17 at 07:06
  • yes for using register token from third party database too, i had done these kind of project previously – aznelite89 Feb 21 '17 at 07:09