How to Send Push Notifications Without Using Firebase Console

Sending Push Notifications to all devices without Firebase Console

You can send notifications to all devices, provided all devices are subscribed to a topic.

You can create a php file that is responsible for sending the information to firebase using the api token.

If it has served you, I would appreciate your approval of my comment, regards.

Notification.php:

    $token = 'test'; // Topic or Token devices here
$url = "https://fcm.googleapis.com/fcm/send";

// api key
$serverKey = ''; // add api key here

$notification = array('title' => 'Welcome StackOverFlow' , 'body' => 'Testing body', 'sound' => 'default', 'badge' => '1');
$data = array('extraInfo'=> 'DomingoMG');
$arrayToSend = array('to' => $token, 'notification' => $notification, 'priority'=>'high', 'data'=> $data);
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);

//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

How to send push notifications without using Firebase console

There is an API for sending messages with Firebase Cloud Messaging. Instead of repeating it here, I recommend you check out the FCM server documentation. It basically takes the form of a HTTP POST request, such as this example from the docs:

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{
"message":{
"token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "This is an FCM notification message!",
"title" : "FCM Message",
}
}
}

To send messages to devices you will need to specify the so-called FCM server key. As its name applies, this key should only be used in trusted environments, such as a server that you control, your development machine, or Cloud Functions for Firebase. The reason for this is that someone who has your FCM server key can send unlimited messages to all users of your app.

There is also a Firebase Admin SDK that makes it easier to call the FCM server API to send messages on platforms that it supports. For more on this option, see the FCM Admin SDK documentation. That turns the above into something like this (on Node.js):

admin.messaging().send({
"message":{
"token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "This is an FCM notification message!",
"title" : "FCM Message",
}
}
})

Pushing Notification to all users through Firebase WITHOUT BACKEND - static PWA

It is not possible to securely send a message through FCM directly from your client-side application code. As the document that you show points out, sending messages requires a trusted environment - such as your development machine, a server you control, or Cloud Functions.

Calling to the FCM API to send a message requires that you specify the FCM server key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. If you were to include this key in your client-side application code, a malicious user can find it and you're putting your users at risk.

On your specific questions:

  1. There is no way to subscribe to a topic directly from within client-side JavaScript code. see How to subscribe to topics with web browser using Firebase Cloud Messaging

  2. Sending a message requires a trusted environment. For some examples, see How can I send a Firebase Cloud Messaging notification without use the Firebase Console?, How to send device to device messages using Firebase Cloud Messaging? and How to send one to one message using Firebase Messaging



Related Topics



Leave a reply



Submit