24

I am using below script to send notification to particular users:

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'My_API_KEY' );
$registrationIds = array( TOKENS );
// prep the bundle
$msg = array
(
    'body'  => "abc",
    'title'     => "Hello from Api",
    'vibrate'   => 1,
    'sound'     => 1,
);

$fields = array
(
    '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;
?>

Script is working fine but how can i send notification to all user who installed my app. I created a topic in my app (alerts), and i can send notification to all users via firebase console. Can anyone guide me to update above script for topic.

DIGITAL JEDI
  • 1,672
  • 3
  • 24
  • 52
  • Can you provide the code of Push notification part in the IDE? ![I have coded the php script part](https://i.stack.imgur.com/LpuFJ.png) I dont know how to send request to the Firebase server. Please share the codes in the IDE that handle the push notification ![.](https://i.stack.imgur.com/cE21r.png) – Aacs Jun 29 '17 at 11:26

6 Answers6

30

I fixed by replacing

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg
);

To

$fields = array
(
    'to'  => '/topics/alerts',
    'notification'          => $msg
);
DIGITAL JEDI
  • 1,672
  • 3
  • 24
  • 52
10

You can send the notifications without curl (which was not available on my server). I prepared a function which can send a notification to a specified topic:

sendNotification("New post!", "How to send a simple FCM notification in php", ["new_post_id" => "605"], "new_post", "YOUR_SERVER_KEY");

function sendNotification($title = "", $body = "", $customData = [], $topic = "", $serverKey = ""){
    if($serverKey != ""){
        ini_set("allow_url_fopen", "On");
        $data = 
        [
            "to" => '/topics/'.$topic,
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
            "data" => $customData
        ];

        $options = array(
            'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $data ),
                'header'=>  "Content-Type: application/json\r\n" .
                            "Accept: application/json\r\n" . 
                            "Authorization:key=".$serverKey
            )
        );

        $context  = stream_context_create( $options );
        $result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
        return json_decode( $result );
    }
    return false;
}
Ambrus Tóth
  • 552
  • 6
  • 14
3

i am work with google firebase many time and i suggest my simple code for send notification on topic.

public function test()
{
    // Method - 1
    // $fcmUrl = 'https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1';
    // $notification = [
    //     "message" => [
    //         "topic" => "foo-bar",
    //         "notification" => [
    //             "body" : "This is a Firebase Cloud Messaging Topic Message!",
    //             "title" : "FCM Message",
    //         ]
    //     ]
    // ]; 

    // Method - 2 

    $fcmUrl = 'https://fcm.googleapis.com/fcm/send';



    $notification = [
        "to" => '/topics/cbtf',
            "data" => [
                "message" : "Messaging Topic Message!",
            ]
        ]
    ];


    $headers = [
        'Authorization: key=AIza...............klQ5SSgJc',
        'Content-Type: application/json'
    ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    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($notification));
    $result = curl_exec($ch);
    curl_close($ch);

    return true;
}
Harsukh Makwana
  • 4,296
  • 3
  • 27
  • 34
2

You can send notification to any of a topic in firebase. And you can do it from any language it's just a http request but Always you have to maintain the JSON format so that you can catch notification from your android part from

 public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Timber.d("Data Payload: " + remoteMessage.getData());
}

So You need to send below JSON format

{
 "to": "\/topics\/general",
 "data": {
 "data": {
  "title": "Database Notification",
  "message": "New Data Added"
   }
 }
}

So that you can get this data set from remoteMessage.getData()

pavel
  • 1,603
  • 22
  • 19
1

With particular Topic

<?php

print "testing";

function sendPushnotification($data = array()) {

  $apiKey = '';


  $fields = array('to' => '/topics/EWAP' , 'notification' => $data);
  $headers = array('Authorization: key=' .$apiKey, 'Content-Type: application/json', 'priority' => 10);

  $url = 'https://fcm.googleapis.com/fcm/send';

  // var_dump($fields);

  $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_VERIFYPEER, false);

  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  $result = curl_exec($ch);
  curl_close($ch);

  return json_encode($result, true);
}


$data = array(
  'title' => 'Today topic',
  'body' => 'done buddy'
);

var_dump(sendPushnotification($data));


?>
Sahil Omer
  • 163
  • 9
0

With device token.

<?php

print "testing";

function sendPushnotification($to = '',$data = array()) {

  $apiKey = '';


  $fields = array('to' => $to , 'notification' => $data);
  $headers = array('Authorization: key=' .$apiKey, 'Content-Type: application/json');

  $url = 'https://fcm.googleapis.com/fcm/send';

  // var_dump($fields);

  $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_VERIFYPEER, false);

  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  $result = curl_exec($ch);
  curl_close($ch);

  return json_encode($result, true);
}


$to = "add device here token ";
$data = array(
  'title' => 'Today topic',
  'body' => 'done buddy'
);

var_dump(sendPushnotification($to, $data));


?>
Sahil Omer
  • 163
  • 9