How to Send a Firebase Notification to All Devices Via Curl

How do you send a Firebase Notification to all devices via CURL?

EDIT: It appears that this method is not supported anymore (thx to @FernandoZamperin). Please take a look at the other answers!

Instead of subscribing to a topic you could instead make use of the condition key and send messages to instances, that are not in a group. Your data might look something like this:

{
"data": {
"foo": "bar"
},
"condition": "!('anytopicyoudontwanttouse' in topics)"
}

See https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_topics_2

How can I send a notification to all devices?

using firebase u can send notification on multiple device check this link it will helpfull for u

https://firebase.google.com/docs/cloud-messaging/android/send-multiple

Send a notification to all my Android devices with REST Firebase

To achieve Push Notifications via Firebase, you need to implement FireBase Cloud Messaging to push notifications to devices.

From the docs,

An FCM implementation includes two main components for sending and receiving:

A trusted environment such as Cloud Functions for Firebase or an app server on which to build, target, and send messages.
An iOS, Android, or web (JavaScript) client app that receives messages via the corresponding platform-specific transport service.

See the documentation for server setup, or you can also use FireBase Functions. It sounds like you want to integrate Firebase Functions with Cloud Messaging. I haven't done this before, but here is a blog post doing this for a Flutter Web project:

https://medium.com/@umeshnalinde7/flutter-cloud-messaging-with-firebase-functions-firestore-android-175904a15537

Firebase Notification successfully sent via CURL and Firebase console but not received on the device

The problem is your are sending message which only works when application is on foreground , to make it works when the application is in background you should use data , try this form :

{
"data": {
"title":"FCM Message",
"Body":"This is an FCM Message"
},
"to" : "devicetoken"
}

Explanation:

  • notification - messages that goes directly to Android's Notification tray only if your application is in background/killed or gets delivered to onMessageReceived() method if your app is in foreground.

  • Data payload - Doesn't matter whether your application is in forground or background or killed, these messages will always be delivered to onMessageReceived() method.

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 do i send notification to all mobile devices when i post a new content in Drupal? also Laravel is my Authentication back-end

You should get token id from each user device and send notification via their device token as an array.
Add a new table named devices has user_id and device_token as columns then each new user open your application his device token will be sent to your server then you should save received token as new record to devices table.



Related Topics



Leave a reply



Submit