PHP Send Firebase Push Notification

Created At: 2023-02-07 02:50:29 Updated At: 2023-02-07 07:20:47

For a real world app, you would not send Firebase push notification from your app, but you would do it through a server. 

Here I am using PHP as a backend server language and using it to do push notification in connection with Firebase.

There are below steps that I followed to realize PHP send Firebase push notification.

  1. get sender information (from front end)
  2. get receiver information (from front end)
  3. find receiver from the database
  4. find receiver's device token ID (FCM token)
  5. initialize Firebase messaging
  6. set up for android notification
  7. set up for iOS notification
  8. send notification

At the beginning of the method, we get some objects through Request object. These are to make sure about the notification sender and receiver information.

In general, once you have the users's information you will need to query, whether the receiver exist or not. If the receiver exist then you should also look for receiver's FCM device token. That's what we did in the code.

If the FCM device token exist, then we initialize the Firebase messaging mechanism using the below line

$messaging = app('firebase.messaging');

To be well work with this make sure, you have Firebase integrated in your code base. Here I showed you how to integrate Firebase SDK for PHP.

Here I send notification for different kind of communication like

  1. voice call
  2. video call
  3. text message

In the code I also showed to seperate Android and iOS.

 public function send_notice(Request $request){
      $user_token = $request->user_token;
      $user_avatar = $request->user_avatar;
      $user_name = $request->user_name;
      $to_token = $request->input("to_token");
      $to_name = $request->input("to_name");
      $to_avatar = $request->input("to_avatar");
      $call_type = $request->input("call_type");
      $doc_id = $request->input("doc_id");
      if(empty($doc_id)){
          $doc_id = "";
      }
      ////1. voice 2. video 3. text, 4.cancel
      $res =DB::table("users")->select("avatar","name","token","fcmtoken")->where("token","=",$to_token)->first();
      if(empty($res)){
          return ["code" => -1, "data" => "", "msg" => "user not exist"];  
      }
      
      $deviceToken = $res->fcmtoken;
        try {
       
        if(!empty($deviceToken)){

        $messaging = app('firebase.messaging');
        if($call_type=="cancel"){
           $message = CloudMessage::fromArray([
         'token' => $deviceToken, // optional
         'data' => [
            'token' => $user_token,
            'avatar' => $user_avatar,
            'name' => $user_name,
            'doc_id' => $doc_id,
            'call_type' => $call_type,
        ]]);  
        
         $messaging->send($message);
            
        }else if($call_type=="voice"){
           
        $message = CloudMessage::fromArray([
         'token' => $deviceToken, // optional
        'data' => [
            'token' => $user_token,
            'avatar' => $user_avatar,
            'name' => $user_name,
            'doc_id' => $doc_id,
            'call_type' => $call_type,
        ],
        'android' => [
            "priority" => "high",
            "notification" => [
                "channel_id"=> "com.dbestech.chatty.call",
                'title' => "Voice call made by ".$user_name,
                'body' => "Please click to answer the voice call",
                ]
            ],
            'apns' => [
            // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
            'headers' => [
                'apns-priority' => '10',
            ],
            'payload' => [
                'aps' => [
                    'alert' => [
                       'title' => "Voice call made by ".$user_name,
                       'body' => "Please click to answer the video call",
                    ],
                    'mutable-content'=>1, 
                    'content-available'=>1,
                    'badge' => 1,
                    'sound' =>'task_cancel.caf'
                ],
            ],
        ],
        ]);
        
       $messaging->send($message);
    
        }else if($call_type=="video"){
       $message = CloudMessage::fromArray([
         'token' => $deviceToken, // optional
        'data' => [
            'token' => $user_token,
            'avatar' => $user_avatar,
            'name' => $user_name,
            'doc_id' => $doc_id,
            'call_type' => $call_type,
        ],
        'android' => [
            "priority" => "high",
            "notification" => [
                "channel_id"=> "com.dbestech.chatty.call",
                'title' => "Video call made by ".$user_name,
                'body' => "Please click to answer the video call",
                ]
            ],
            'apns' => [
            // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
            'headers' => [
                'apns-priority' => '10',
            ],
            'payload' => [
                'aps' => [
                    'alert' => [
                        'title' => "Video call made by ".$user_name,
                        'body' => "Please click to answer the video call",
                    ],
                    'mutable-content'=>1, 
                    'content-available'=>1,
                    'badge' => 1,
                    'sound' =>'task_cancel.caf'
                ],
            ],
        ],
        ]);
        
       $messaging->send($message);
             
         }else if($call_type=="text"){
             
              $message = CloudMessage::fromArray([
         'token' => $deviceToken, // optional
        'data' => [
            'token' => $user_token,
            'avatar' => $user_avatar,
            'name' => $user_name,
            'doc_id' => $doc_id,
            'call_type' => $call_type,
        ],
        'android' => [
            "priority" => "high",
            "notification" => [
                "channel_id"=> "com.dbestech.chatty.message",
                'title' => "Message made by ".$user_name,
                'body' => "Please click to answer the Message",
                ]
            ],
            'apns' => [
            // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
            'headers' => [
                'apns-priority' => '10',
            ],
            'payload' => [
                'aps' => [
                    'alert' => [
                        'title' => "Message made by ".$user_name,
                        'body' => "Please click to answer the Message",
                    ],
                    'mutable-content'=>1, 
                    'content-available'=>1,
                    'badge' => 1,
                    'sound' =>'ding.caf'
                ],
            ],
        ],
        ]);
        
       $messaging->send($message);
             
             
         }
        
        return ["code" => 0, "data" => "", "msg" => "success"]; 
    
       }else{
         return ["code" => -1, "data" => "", "msg" => "fcmtoken empty"];  
       }
       
       
      }catch (\Exception $exception){
          return ["code" => -1, "data" => "", "msg" => "Exception"];  
        }
  }

Comment

  • d
    dylan

    2024-02-03 16:09:28

    It's a complex integration, why don't you try our course on udemy

  • p
    parthsharma

    2024-01-31 05:04:42

    can you please provide all details from database to code everything & where i can get this tokens and fcm key i need everything plase reply me as soon as possible

Add Reviews