PHP Push Notification from FCM to Android

PHP Push Notification from FCM to Android

Sharing is caring!

PHP Push Notification from FCM to Android

A push notification is a simple message which appears in the Android mobile notification bar. Android provides service for send push notification from you website. Yes, you read right. You can sen notification from you php website to your android mobile.

FCM the Firebase cloud Messaging provides fully customizable service for sending the notification. You can set notification icon, notification image, notification title, notification message as per your requirement.

here is demo image of push notification for better understanding that how it looks?

Ok, let’s see how you can send notification step by step.

NOW OUR FIRST STEP IS TO GET API KEY FOR SENDING A NOTIFICATION. WE SEND NOTIFICATION USING FIREBASE SERVICE. YOU CAN FIND YOUR API KEY FROM HERE FIREBASE CONSOLE

You have to register to firebase console with your Gmail ID. Gmail Id is compulsory for Register with Firebase. If you do not have Gmail ID than go and register first with Gmail.

After register with firebase, you can show +(plus icon) on the main page. Clik on plus icon no one popup message appears with some details. Set your project name here then set your country and hit on create project button.

Now you will be redirected to the main dashboard.

 

You can show setting icon on top of the sidebar. Click on it and go to Project Settings. as I showed in the picture.

You can show some tabs. Click on the Cloud messaging tab when you can find your server key.

Now we have to set up PHP code.

I created one function for your use you need to just copy and paste in your code.

function sendNotification($devicetoken, $mesg, $title, $api_key) {
 
    $registrationIds = $devicetoken;
   #prep the bundle
    $msg = array
        (
        "body" => $mesg,
        "title" => $title,
        "sound" => "mySound", 
        
    );
    $fields = array
        (
        'to' => $registrationIds,
        'notification' => $msg,
        'priority' => 'high',
    );


    $headers = array
        (
        'Authorization: key=' . $api_key,
        'Content-Type: application/json'
    );
   #Send Reponse To FireBase Server	
    $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));
    $result = curl_exec($ch);
    curl_close($ch);
    $cur_message = json_decode($result);
    if ($cur_message->success == 1)
        return $result;
    else
        return $result
        ;
}

After running this function you can send a notification to your android mobile phone. But here is one problem that you can show the round icon. You can also set your own icon in this notification. You just add some lines in above code.

Just update your array $msg as follows. You can change icon background color.

One more thing you have to do is: you must have to save icon in your resource folder in your android application, else this notification show default android icon. I store my icon named “fcm_push_icon.png” size is 192 X 192 and .png format.

$msg = array
       (
       "body" => $mesg,
       "title" => $title,
       "sound" => "mySound",
       "icon" => "fcm_push_icon" 
       "color" => "#00a99d",     // you can change any color here    
   );

You can also send a big image using this php notification.

Just update your array as$msg follows. You can change icon background color.

$msg = array
       (
       "body" => $mesg,
       "title" => $title,
       "sound" => "mySound",
       "icon" => "fcm_push_icon",
       "color" => "#00a99d",
       "image" => "your image path", // eg. www.yourwebsite.in/advertise.png
       
   );

So this is the full code of send push notification to android device using php.

PHP Push Notification from FCM to Android

One thought on “PHP Push Notification from FCM to Android

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top