Home > Back-end >  how do i send notification to all mobile devices when i post a new content in Drupal? also Laravel i
how do i send notification to all mobile devices when i post a new content in Drupal? also Laravel i

Time:11-02

I am using Laravel for authentication and druapl for content management, and now using firebase for notification to mobile devices... how do i send notification to all mobile devices when i post a new content in druapl? this is my send function in Laravel

public function sendNotification($device_token, $message)
    {
        
        
        $SERVER_API_KEY = 'myKey';
  
        $data = [
            "to" => $device_token, // for single device id
            "notification" => $message
        ];
        $dataString = json_encode($data);

        $headers = [
            'Authorization: key=' . $SERVER_API_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_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
               
        $response = curl_exec($ch);
      
        curl_close($ch);
      
        return $response;
    }

CodePudding user response:

You should get token id from each user device and send notification via their device token as an array. Add a new table named devices has user_id and device_token as columns then each new user open your application his device token will be sent to your server then you should save received token as new record to devices table.

  • Related