You are here
Home > Android > Send Push Notification to Our App Using FCM in PHP

Send Push Notification to Our App Using FCM in PHP

fcm-push-notification-php-featured

Hello Friends, today we’ll see how to send push notification to our app using FCM in PHP. Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost. Notification can be sent from Firebase Console also see screenshot below but if we want to send notification directly from our web server to achieve some automation or just to add some feature then this code will help.

sending-notification-firebase-console

To send push notification we need Server Key and device registration ids if we want to send notification to specific devices. But we can send to a topic also where all the devices will receive notification registered to that topic. Server Key we can get from Firebase Console -> Project Settings -> Cloud Messaging after creating project in Firebase Console and device registration id from the app itself (see logcat screenshot below) that will be sent to our server, which we can store to database for further use. But in this example I am not saving it to database just for the simplicity but you can use according to your need. We also need an app to test our push notification, so I’ll share android app code as well in this post.

firebase-registration-token

Let’s start coding but before starting code, here is the Lifecycle Flow of Firebase Cloud Messaging (FCM)

  • Register devices to receive messages from FCM.
  • Send and receive downstream messages.

PushNotification.php

There are two functions I have created in the PushNotification class here one sendNotification and another one registerForNotification. Just as name suggests sendNotification used to send notification while registerForNotification registers device id to a topic. This PushNotfication.php will be included in both register.php and index.php to use this class and its functions.

register.php

index.php (send notification ui)

Android App Code

Our server side code is ready to send notification now but we need an app to test whether our code is working or not. Let’s move to the app part, firstly we need a firebase configuration file to allow our app to interact with firebase. And to get configuration file we have to setup project and app on firebase console. But here I’ll not explain how to setup project, app and download configuration file as it is better explained on official website. Hence, we’ll see only coding and adding dependency part here and to setup project, app and downloading and pasting configuration file in our android project below link can help.

https://firebase.google.com/docs/android/setup#console

Now here is the android app code

MainActivity.java

Here in the MainActivity class, I have overridden onNewIntent method because this will be called from PendingIntent of notification when we click on notification and our app is running but when our app is not running then custom data will be received in extras of the intent of our launcher activity which we are extracting in onCreate method here. For more info kindly check below link

https://firebase.google.com/docs/cloud-messaging/android/receive

activity_main.xml

JsonParser.java

MyFirebaseMessagingService.java

AndroidManifest.xml

build.gradle (Project Level)

build.gradle (App Level)

 

Screenshots

send-push-notification-step-1 send-push-notification-step-2 send-push-notification-step-3
Sending Notification to Topic
send-push-notification-step-4 send-push-notification-step-5 send-push-notification-step-6
Sending Notification to Registration Id
send-push-notification-step-7 send-push-notification-step-8 send-push-notification-step-9
Sending Data with Push Notification
send-push-notification-step-10 send-push-notification-step-11
Data Received From Notification when App is Running and when Not Running

NetBeans Project Download

AndroidStudio Project Download

 

Thanks friends
Your queries & suggestions are welcome in comments section
Please don’t forget to share if you find this helpful

Rajesh Kumar Sahanee
I am a passionate Java Developer and I like Computer Programming. I love to do some interesting experiments and listening music in my free time.
https://www.zatackcoder.com

Comments

zatackcoder
Top

  Â