How to send FCM Push Notifications to topics in codeigniter 4

Admin
1 minute read
0

 


Topic according send all device notification easy code.
    
    public function notification($id=null)
    {
        $session = session();
        $data = [];
        $data['title'] = "News notification sent";  
        $data['session'] = $session;
        $model = new NewsModel();
        $result = $model->find($id);  
          
    
            $topic ="news"; // news | all
            $API_ACCESS_KEY = 'AAAAIjwpj4Y:APA91bF00KkPSL5Y8Y2bmsqHTOc1JZRSO5t6urNeCmq2GB3M3BT4_s0rwEIofmcvWZvykey9r84a68GNqzPaqwiupO0JFuRlrPDjI-bbJxt4GFthM2VKri2C5qY3nYrc_RmLt8ICunNU';
            $msg = array
            (
                'title' => $result['title'],
                'body' => '',
                'image' => base_url().'/public/uploads/news/'.$result['path']  
            );

$fields = array('to' => '/topics/' . $topic, 'priority' => 'high', 'data' => $msg);
$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, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$pushResult = curl_exec($ch);
curl_close($ch);
 
 
    
    $session->setFlashdata('msg', '<div class="alert alert-success"> Notification Successfully Sent. </div>');
    return redirect()->to('/admin/news/active');    
        
    }
    
   

Post a Comment

0 Comments
Post a Comment (0)
Our website uses cookies to fast experience.
Accept !