且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 Firebase 从 Laravel 服务器向 Android 应用程序发送推送通知

更新时间:2022-12-21 16:03:53

您可以为其创建辅助函数.

You can create helper function for it.

试试这个:-

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;
}