Search code examples
laravelionic-frameworkpush-notificationmessaging

Laravel Push Notification and Messaging


I'm trying to write front-end with the back-end ionic with laravel. What I want to do is that when the user orders the application, I want to send notifications to the application and messaging with the user from the application.

For this, I have reviewed the / laravel-push-notification package;

However, it did not work for me because I needed a dynamic messaging. Can you help with this? I do not want to use https://pusher.com


Solution

  • I have also integrated push notification in laravel using below code, hope it helps you :

    function sendPushNotification($fcm_token, $title, $message, $id="") {  
            $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" : {
                    "id" : "'.$id.'",
                    "title":"' . $title . '",
                    "description" : "' . $message . '",
                    "text" : "' . $message . '",
                    "is_read": 0
                  }
            }';
    
            $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;
        }