Title: Send Push Notifications from Laravel Server to Android App via Firebase
Tags: php;laravel;firebase;push-notification;firebase-cloud-messaging
Question: is it possible to send a push notification to android but through laravel based dashboard, rather than through the firebase console?
And if possible, what are the steps?
Thank you.
Comment: I'd check some of the questions and answers here: https://stackoverflow.com/search?q=%5Bfirebase-cloud-messaging%5D+%5Blaravel%5D+send This question for example, seems to go a long way: https://stackoverflow.com/questions/57554177/laravel-fcm-send-data-payload-only as does this: https://github.com/kreait/laravel-firebase


Here is another answer: You can use this function
public function send_fcm_notification($title,$body,$target,$chid){
define( 'API_ACCESS_KEY', 'ENTER_API_KEY_HERE' );
$fcmMsg = array(
'title' =&gt; $title,
'body' =&gt; $body,
'channelId' =&gt; $chid
);
$fcmFields = array(
'to' =&gt; $target, //tokens sending for notification
'notification' =&gt; $fcmMsg,
);
$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, true );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fcmFields ) );
$result = curl_exec($ch );
curl_close( $ch );
//echo $result . &quot;\n\n&quot;;

Here is another answer: You can create helper function for it.
Try this:-
function notifications($token=null,$title=null,$message=null,$user_id=null)
$path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' =&gt; $token,
'notification' =&gt; array('title' =&gt; $title, 'body' =&gt; $message, 'sound' =&gt; 'default', 'click_action' =&gt; 'FLUTTER_NOTIFICATION_CLICK', 'icon' =&gt; 'fcm_push_icon'),
'data' =&gt; array('title' =&gt; $title, 'body' =&gt; $message, 'sound' =&gt; 'default', 'icon' =&gt; 'fcm_push_icon'),
);
$headers = array(
'Authorization:key=' . 'Your-fcm-key',
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
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 $result;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/01/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><laravel><firebase><push-notification><firebase-cloud-messaging>', 'Title': 'Send Push Notifications from Laravel Server to Android App via Firebase', 'ViewCount': 1050, 'id': 66897208, 'user_AboutMe': None, 'user_AccountId': 17827644.0, 'user_DisplayName': 'Astituti', 'user_Reputation': 41.0, 'user_Views': 35.0, 'user_id': 12948482.0}}