Thursday, August 27, 2020

Push Notification in Laravel

    Push Notification in Laravel


To send push notifications on mobile in the Laravel we need below required parameters

  • FCM TOKEN
  • Message Title for notification
  • Message Body
  • NOTIFICATION KEY
Please find below example code that will send notification on Mobile if you have all required fields

function sendPushNotification($fcm_token, $title, $message) {  
        $push_notification_key = Config::get('settings.PUSH_NOTIFICATION_KEY');    
        $url = "https://fcm.googleapis.com/fcm/send";            
        $header = array("authorization: key=" . $push_notification_key . "",
            "content-type: application/json"
        );    

        $postdata = '{
            "to" : "' . $fcm_token . '",
                "notification" : {
                    "title":"' . $title . '",
                    "text" : "' . $message . '"
                },
            "data" : {
                "title":"' . $title . '",
                "description" : "' . $message . '",
                "text" : "' . $message . '"
              }
        }';

        $ch = curl_init();
        $timeout = 120;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        // Get URL content
        $result = curl_exec($ch);    
        // close handle to release resources
        curl_close($ch);

        return $result;
    }

You can found the same here also ....Send Push Notification

No comments:

Post a Comment

Integration of Queue in Laravel

Integration of Queue in Laravel Introduction Laravel Queue is to enhances the laravel application performance and provides a smooth proc...