简体   繁体   中英

Send Push Notifications from Laravel Server to Android App via Firebase

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.

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' => $token,
        'notification' => array('title' => $title, 'body' => $message, 'sound' => 'default', 'click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'icon' => 'fcm_push_icon'),
        'data' => array('title' => $title, 'body' => $message, 'sound' => 'default', 'icon' => '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;
}

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' => $title,
             'body' => $body,
             'channelId' => $chid
            );
  $fcmFields = array(
                'to' => $target, //tokens sending for notification
                'notification' => $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 . "\n\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM