FCM Send Push Notification Using PHP

Firebase Cloud Messaging (FCM) is the new version of GCM. We will see here how to send push notification from PHP server to android app client in short time and simple way with single device id. Developers requires a little knowledge to understand following code. I will discuss here only important coding modules and functions.

It has more features than GCM like Analytics, Firebase Remote Config, Easy Implementation, Cross-platform support, Web Push support. We strongly recommend  that you migrate to Firebase Cloud Messaging Platform . Google recommends upgrading from GCM to FCM in their APIs for Android. Initially many developers gets panic to migrate from GCM to FCM.

See the following coding snippet of important files and functions

MainActivity.java :

There is no change in MainActivity.java file.

AndroidManifest.xml:

MyInstanceIDListenerService.java:

Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised. Note that this is also called when the InstanceID token is initially generated, so this is where you retrieve the token.

MyFcmListenerService.java:

changes in build.gradle (Module:app):

Here you require only change in dependencies :

dependencies in build.grandle(Project: Project name):

 google-services.json :

This file contains developer authentication credentials and configuration settings, which is needed to verify while connecting with GoogleApiClient. You have to put it under root directory of the app i.e under ProjectName/app/. Though your service is working fine with your test device as it is detecting your developer account but after releasing your app in public, it will not work without the json file. So don’t delete it. Create your FCM project here and download the google-services.json file.

Here it is sample look of google-services.json:

Pushnote.php:

Enter FCM refresh token manually and refresh this php page and check your app receives notification or not. If want to know how to insert FCM refresh token into database then see this post Android Insert Data to MySQL Database Using PHP (Android , MySQL , PHP , PDO)

In following code I have mentioned ‘API_ACCESS_KEY’ which is provided by google.  You can create your  FCM Project here and get API_ACCESS_KEY.

The variable $registrationIds is android device id. Add your ids in array.

The $msg is your notification format . You can custom as per your choice.

Output:

FCM Send Push Notification Using PHP output
FCM Send Push Notification Using PHP output

Understand above code and if you facing any problem comment bellow.

Leave a Reply

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