SubscribeSubscribe with Androidlift to get new post on mail.

Android firebase cloud messaging ,Google Cloud messaging tutorial

Android Firebase cloud messaging,Hi friends we have covered a some tutorials with GCM but now google has an alternative which is easier than gcm. In this post we will use FCM (Firebase Cloud Messaging). So today in this Firebase Cloud Messaging Tutorial I will teach you how to send push notification to android devices using Firebase Cloud Messaging. So lets begin.

Android Firebase cloud messaging Demo (FCM):

 

Creating an Android Studio Project

As we do always ? create a new android studio project. Once the project is loaded copy the package name you can get it from the AndroidManifest.xml file.

Go to firebase console and create a new project.

Android Firebase cloud messaging
Android Firebase cloud messaging
  • Now put your app name and select your country.
Android Firebase cloud messaging
Android Firebase cloud messaging

Now click on Add Firebase to Your Android App.

Android Firebase cloud messaging
Android Firebase cloud messaging

Now you have to enter your projects package name and click on ADD APP.

firebase-cloud-messaging-tutorial-2

After clicking add app you will get google-services.json file.

Adding Firebase Messaging to Your Project

  • Now come back to your android project. Go to app folder and paste google-services.json file.
Android Firebase cloud messaging-3
Android Firebase cloud messaging
  • Now go to your root level build.gradle file and add the following code.
  • Inside app level build.gradle file make the following changes.
Now sync your project.

Implementing Firebase Cloud Messaging

  • Create a class named  FirebaseInstanceIDService.java and write the following code.
  • Now create FirebaseMessagingService.java and write the following code.
  • Now we have to define the above services in our AndroidManifest.xml file. So go to manifest and modify as follows.

PHP Scripts to insert token in server database table:

Database script

after creating database assign database user to your database and note-down database username, password, name so we can use that for connection.

 

register.php

push_notification.php

To get authorization key from firebase console follow below steps:-

  • Goto your firebase console => select your project => project setting => cloud messeging => there you need to copy “server key”.
  • then put that key in push_notification.php file otherwise you will get the outhorization error.
  • Thats all. Now run your app (Make sure your device is having google play service or else it won’t work).
  • If you want to send notification to register user then you can sent it following to ways =
    • By hitting the push_notification.php url in browser.
    • From firebase console.(for this u will need the token to send the push).
      • Sending Push Notification using Firebase Console(Google messaging service, FCM)

        • Go to firebase console and select the app you created.
        • From the left menu select notification.
        • Click on new message.
        • Enter message, select single device and paste the token you copied and click on send. The same as I did on the video, and check your device
        • screenshot-Android Firebase cloud messaging
          Android Firebase cloud messaging
  • Awesome! It is working absolutely fine.

 

 

(24)