Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

M-K's avatar
Level 2

help with Firebase php Notification;

hi, i'm trying to send / recive notification using firebase

i know how to upload cloude messging but how i can recive the notificaion after that ?

firebase controller.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Database;
use Kreait\Firebase\Messaging\Message;
use Kreait\Firebase\Messaging\MessageToTopic;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage;

class FirebaseController extends Controller
{
    public function index(){
        $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'asdasd-firebase-adminsdk-6rxwm-58430d1f78.json');
        $firebase         = (new Factory)
                        ->withServiceAccount($serviceAccount)
                        ->withDatabaseUri('https://asdasd-e12f2.firebaseio.com')
                        ->create();
        $database       = $firebase->getDatabase();
        $newPost          = $database
                            ->getReference('blog/posts')
                            ->push(['title' => 'Post title','body' => 'This should probably be longer.']);
        echo"<pre>";
        
        
        print_r($newPost->getvalue());
        
 
        
        $topic = 'a-topic';
        
        $message = CloudMessage::withTarget('topic', $topic);
        $message = CloudMessage::fromArray([
             'topic' => $topic
       
        ]);
        
        
        $registrationTokens = [
                $topic
        ];
        
        

        $messaging = $firebase->getMessaging();
        
        $messaging->subscribeToTopic($topic, $registrationTokens);
        
        $messaging->send($message);
        
        
        
        $title = 'My Notification Title';
        $body = 'My Notification Body';
        
        $notification = Notification::fromArray([
            'title' => $title,
            'body' => $body
        ]);
        
        $notification = Notification::create($title, $body);
       
       
       
       $data = [
            'first_key' => 'First Value',
            'second_key' => 'Second Value',
        ];
        $message = $message->withNotification($notification);
        
        $message = $message->withData($data);         
                
        
    }
} 
 

0 likes
0 replies

Please or to participate in this conversation.