0

I'm making an existing application which includes notification when there is new content.

Notification is sent to all the gadgets that have installed the application.

How to do it using FCM and PHP?

AL.
  • 36,815
  • 10
  • 142
  • 281
  • Firebase Notifications is a web console only, it does not have an API. If you want to send messages programmatically, you should use Firebase Cloud Messaging. – Frank van Puffelen Jan 24 '17 at 07:00

1 Answers1

1

Here is the PHP code to send notification using FCM.

<?php
define('API_ACCESS_KEY', ''); // API access key from Firebase Console
$registrationIds = array(''); //Token IDs of devices

$msg = array
(
    'text'  => 'Test Text',
    'title'     => 'Test Title',
);

$fields = array
(
    'to'    => $registrationIds[0],
    'notification'      => $msg,
    "priority"=> "high"
);

$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;
?>
Dhruv
  • 1,801
  • 1
  • 15
  • 27
  • 1
    if this could be used to send notifications to 4000 users at once? – Ruslan Wahyudi Jan 24 '17 at 08:26
  • @user7332664 You can modify the payload above and use [Topics Messaging](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging). It can send to all of the users subscribed to a topic. – AL. Jan 24 '17 at 10:00
  • hi,on client side i could not able to receive the notification info---------my client side code----self.addEventListener('push', function(event) { console.log(event.data.json()); if (event.data) { console.log("#######"); } }); – MAK Jan 24 '17 at 10:30
  • @MAK - Are you using `JavaScript` for `Cordova`? – Dhruv Jan 24 '17 at 12:37
  • Ok I will try to use Topics Messaging.. :) – Ruslan Wahyudi Jan 25 '17 at 04:07