Skip to main content

Android Push Notification Using Firebase Cloud Messaging

In this tutorial, I’m gonna show you how to use the push notifications in your android applications. Before the Google I/O 2016, we were using the Google Cloud Messaging service (GCM) to send the data from the server to the clients or the android power devices but in I/O 2016, Google introduced the Firebase Cloud Messaging which is a good alternative and easier to implement.

Steps to implement FireBase push notification in Android
1: Import the code of FCM


2: go to   https://console.firebase.google.com/  and create a new project.


4: Now put your app name and select your country.

5:Now click on Add Firebase to Your Android App.





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


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

Now here Console work is finish

Adding Firebase Messaging to Your Project


1:Now come back to your android project. Go to app folder and paste google-services.json file.
Don’t forget to change the app id to your package name  in app gradle

Sending Push Notification using Firebase Console

1:Go to firebase console and select the app you created.

2:From the left menu select notification.

3:Click on new message.

4:Enter message, select single device and paste the token you copied and click on send.check your device

Congratulation you have configured the FCM in your app
If any trouble please refer the link Firebase Cloud Messaging Tutorial


 


PHP code to send push

PHP code to send push

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'message'     => 'here is a message. message',
    'title'        => 'This is a title. title',
    'subtitle'    => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'    => 1,
    'sound'        => 1,
    'largeIcon'    => 'large_icon',
    'smallIcon'    => 'small_icon'
);
$fields = array
(
    'registration_ids'     => $registrationIds,
    'data'            => $msg
);
$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,

    'Content-Type: application/json'

);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
echo $result;

Happy coding



Comments

Popular posts from this blog

Handle Scrollable Views or Controls in another Scrollview in Android

In life of any Android developer, there are times when there is a requirement in the application to use one scrollable control inside another scrollable control. In such cases, the scrolling functionality of one or all scrollable controls gets enabled and the Android application cannot identify that scroll event of which control is to be handled first. Here, UI may get stuck in some scenarios. The ideal behavior expected is when the user touches the parent scroll view, only the parent view’s scroll event is called and when the user scrolls the child scroll view, the child view’s scroll event is called. In our standard Android application development and implementation process it does not happen when we add scrollable control in another scrollable control. Below is the solution to handle the scroll event of both the controls without any glitch. Example We have a layout that has a ScrollView as parent layout and it contains some TextViews, ImageViews and ListView as child control...
Android Push Notification Using Firebase Cloud Messaging In this tutorial, I’m gonna show you how to use the push notifications in your android applications. Before the Google I/O 2016 , we were using the Google Cloud Messaging service (GCM) to send the data from the server to the clients or the android power devices but in I/O 2016, Google introduced the Firebase Cloud Messaging which is a good alternative and easier to implement. Steps to implement FireBase push notification in Android 1: Import the code of FCM   https://drive.google.com/a/edreamz.in/file/d/0B1L0RecNhNk9UDhhOXRZQjJYU0k/view?usp=sharing 2: go to    https://console.firebase.google.com/  and create a new project. 4: Now put your app name and select your country. 5: Now click on Add Firebase to Your Android App. 6: Now you have to enter your projects package name and click on ADD APP. 7:After clicking add app you will get google-services.json fil...