How to Send a Firebase Cloud Messaging Notification Without Use the Firebase Console

How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

Firebase Cloud Messaging has a server-side APIs that you can call to send messages. See https://firebase.google.com/docs/cloud-messaging/server.

Sending a message can be as simple as using curl to call a HTTP end-point. See https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol

curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" \
--Header "Content-Type: application/json" \
https://fcm.googleapis.com/fcm/send \
-d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"title\":\"Hello\",\"body\":\"Yellow\"}}"

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",
}
}
})

Firebase send user to user notification without using cloud function or personal server

well this is not possible as it would require API key as mentioned by Doug in the comment. and putting that in client code would make blunder.

i m just making a guess and it is quite off topic but it might help someone in future if you dont want to read user FCM id again and agin from firestore as it might increase bill. you can create topics in FCM and subscribe the client to it. this way you can reduce the number of reads and suffice the purpose.

FCM Docs

how to send fcm notifications to a single device without using firebase console

You need an app server or some Google Chrome extention like PostMan.

First of all, you need to understand the differences between notification message and a data message, like how the app handles the incoming message if it is in the background state or on running on the foreground.

Secondly, create a payload and send a post request to FCM endpoint. It is well documented here.

Lastly, check the FCM response. Note that getting a success response doesn't mean that the message is already delivered to the app, but rather the FCM server accepted it for delivery.

If you would like to learn more about what parameters you can use and the definition of each response codes, check out the Firebase Cloud Messaging HTTP Protocol documentation.

I hope this helps.



Related Topics



Leave a reply



Submit