Wednesday 23 August 2017

DoubleSideDrawer Android Example

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:app="http://schemas.android.com/apk/res-auto"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/drawer_layout"    
    android:layout_width="match_parent"     
    android:layout_height="match_parent"    
    android:fitsSystemWindows="true"    
    tools:openDrawer="start">

    <include        
      layout="@layout/app_bar_main"        
      android:layout_width="match_parent"        
      android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView        
       android:id="@+id/nav_view"        
       android:layout_width="wrap_content"         
       android:layout_height="match_parent"        
       android:layout_gravity="start"   
       android:fitsSystemWindows="true"  
       app:headerLayout="@layout/nav_header_main"   
       app:menu="@menu/activity_main_drawer" />

    <android.support.design.widget.NavigationView     
        android:id="@+id/nav_right_view"  
        android:layout_width="wrap_content"  
        android:layout_height="match_parent"    
        android:layout_gravity="end"  
        android:fitsSystemWindows="true"   
        app:headerLayout="@layout/nav_header_main"  
        app:menu="@menu/activity_right_drawer" />
    </android.support.v4.widget.DrawerLayout>




app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"    
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.doublesidedrawerexample.MainActivity">

    <android.support.design.widget.AppBarLayout      
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar          
            android:id="@+id/toolbar"      
            android:layout_width="match_parent"  
            android:layout_height="?attr/actionBarSize"   
            android:background="?attr/colorPrimary"  
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>


content_main.xml
<?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout 
              xmlns:android="http://schemas.android.com/apk/res/android"    
              xmlns:app="http://schemas.android.com/apk/res-auto"    
              xmlns:tools="http://schemas.android.com/tools"    
              android:id="@+id/content_main"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin" 
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              tools:context="com.doublesidedrawerexample.MainActivity"
              tools:showIn="@layout/app_bar_main">

    <TextView     
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content" 
        android:text="Hello World!" />
</RelativeLayout>


nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
               xmlns:app="http://schemas.android.com/apk/res-auto"
               android:layout_width="match_parent"
               android:layout_height="@dimen/nav_header_height"
               android:background="@drawable/side_nav_bar"
               android:gravity="bottom"
               android:orientation="vertical"
               android:paddingBottom="@dimen/activity_vertical_margin"
               android:paddingLeft="@dimen/activity_horizontal_margin"
               android:paddingRight="@dimen/activity_horizontal_margin"
               android:paddingTop="@dimen/activity_vertical_margin"
               android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />

</LinearLayout>



activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
     <menu xmlns:android="http://schemas.android.com/apk/res/android">

      <group android:checkableBehavior="single">
        <item  
            android:id="@+id/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item            
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:title="Tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>


activity_right_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="Account">
            <menu>
                <item
                    android:id="@+id/nav_settings"
                    android:icon="@drawable/ic_menu_share"
                    android:title="Settings" />
                <item
                    android:id="@+id/nav_logout"
                    android:icon="@drawable/ic_menu_send"
                    android:title="Logout" />
            </menu>
        </item>

        <item
            android:id="@+id/nav_help"
            android:icon="@drawable/ic_menu_camera"
            android:title="Help" />
        <item
            android:id="@+id/nav_about"
            android:icon="@drawable/ic_menu_gallery"
            android:title="About" />
    </group>

</menu>



main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_openRight"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        android:icon="@drawable/ic_menu_manage"
        app:showAsAction="always" />
</menu>


side_nav_bar.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="135"
        android:centerColor="#4CAF50"
        android:endColor="#2E7D32"
        android:startColor="#81C784"
        android:type="linear" />
</shape>

MainActivity.class
package com.doublesidedrawerexample;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    NavigationView navigationView,rightNavigationView;
    DrawerLayout drawer;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        rightNavigationView = (NavigationView) findViewById(R.id.nav_right_view);
        rightNavigationView.setNavigationItemSelectedListener(this);
    }

    @Override    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_openRight) {
            drawer.openDrawer(GravityCompat.END);
        /*Opens the Right Drawer*/
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.        
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action        
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }
        else if (id == R.id.nav_settings) {

        } else if (id == R.id.nav_logout) {

        } else if (id == R.id.nav_help) {

        } else if (id == R.id.nav_about) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}


build.gradle
compile 'com.android.support:design:24.2.1'

Thursday 20 April 2017

Android YouTube Integration Example


YouTube Integration





  • Create Application

First step in registering application is create a new application developer console. 
So, go to Google Developer Console. Create a new project, named YoutubeVideo.

  • Enable YouTube API

To use API, first you need to enable them in the dashboard.
So, click on the Enable and manage APIs.
This will redirect you to list of all the Google APIs. Here, click on the YouTube Data API to enable YouTube API for your project.

In the resulting screen, click on Enable button which will enable the API andwill ask you to create credentials for your application.



After enabling the API, you need to create the credentials to authenticate app. To create credentials,
click on Go To Credentials button in the dashboard.




This will redirect you to create Android Key form.





  • Download Library

First step in integrating YouTube Player API is downloading the YouTube Android Player API Library. Use YouTube Android Player API link to download the latest library and move it to app’s libs folder. Now, you need to add the library as application dependency. To do so, open app’s build.gradle file and add the below line as dependency :
androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support:support-annotations:25.2.0'

  • AndroidManjfest.xml

<uses-permission android:name="android.permission.INTERNET"/>

  • Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.youtubevideo.MainActivity"> <com.google.android.youtube.player.YouTubePlayerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/youtube_player_view"> </com.google.android.youtube.player.YouTubePlayerView> </RelativeLayout>

  • MainActivity.java

package com.youtubevideo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.google.android.youtube.player.YouTubeBaseActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayerView; public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { //AIzaSyB6dzPRjIZNe_OE6kJRes6370TV4vrTnL0 public static final String API_KEY = "AIzaSyB6dzPRjIZNe_OE6kJRes6370TV4vrTnL0"; //https://www.youtube.com/watch?v=<VIDEO_ID> public static final String VIDEO_ID = "3VxWcqo2sqs"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initializing YouTube player view YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player_view); youTubePlayerView.initialize(API_KEY, this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { if(null== player) return; // Start buffering if (!wasRestored) { player.cueVideo(VIDEO_ID); } // Add listeners to YouTubePlayer instance player.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() { @Override public void onAdStarted() { } @Override public void onError(YouTubePlayer.ErrorReason arg0) { } @Override public void onLoaded(String arg0) { } @Override public void onLoading() { } @Override public void onVideoEnded() { } @Override public void onVideoStarted() { } }); player.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() { @Override public void onBuffering(boolean arg0) { } @Override public void onPaused() { } @Override public void onPlaying() { } @Override public void onSeekTo(int arg0) { } @Override public void onStopped() { } }); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { Toast.makeText(this, "Failed to initialize.", Toast.LENGTH_LONG).show(); } }








Friday 31 March 2017

Android Push Notification Using Firebase





Creating a new Android Project
  • Create a new Android Studio Project with an empty Activity
  • Once your project is loaded click on firebase from toolsmenu

  • After clicking on Firebase an assistant will open showing all the available firebase features.

  • Click on Cloud Messaging

  • You will see a link “Set Up Firebase Cloud Messaging”



  • Now you will see all option required “Set up Firebase Cloud Messaging”


  • Click on “Connect to Firebase” button. It will start a dialog. Insert project name and country select.
  • Now click on Connect to Firebase. And wait for a few seconds.

  • Again click on the button Add FCM to your app and you will see a dialog

  • Click on Accept Changes. And firebase is setup in your project now.


Generating Device Token

          MyFirebaseInstanceIDService.java



package com.pushnotification;

/**
 * Created by JAINISH on 2/12/2017.
 */

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

//Class extending FirebaseInstanceIdService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

   
private static final String TAG = "MyFirebaseIIDService";

   
@Override
   
public void onTokenRefresh() {

       
//Getting registration token
       
String refreshedToken = FirebaseInstanceId.getInstance().getToken();

       
//Displaying token on logcat
       
Log.d(TAG, "Refreshed token: " + refreshedToken);

       
//calling the method store token and passing token
       
storeToken(refreshedToken);
    }

   
private void storeToken(String token) {
       
//we will save the token in sharedpreferences later
       
SharedPrefManager.getInstance(getApplicationContext()).saveDeviceToken(token);
    }
}

AndroidManifest.xml

Service we need to define this class in side AndroidManifest.xml. inside AndroidManifest.xml and write code inside application tag.

<service
   
android:name=".MyFirebaseInstanceIDService">
    <
intent-filter>
        <
action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </
intent-filter>
</
service>

             SharedPrefManager.java


package com.pushnotification;

/**

 * Created by JAINISH on 2/12/2017.

 */

import android.content.Context;

import android.content.SharedPreferences;


public class SharedPrefManager {

    private static final String SHARED_PREF_NAME = "FCMSharedPref";

    private static final String TAG_TOKEN = "tagtoken";

    private static SharedPrefManager mInstance;

    private static Context mCtx;



    private SharedPrefManager(Context context) {

        mCtx = context;

    }

    public static synchronized SharedPrefManager getInstance(Context context) {

        if (mInstance == null) {

            mInstance = new SharedPrefManager(context);

        }

        return mInstance;

    }

    //this method will save the device token to shared preferences

    public boolean saveDeviceToken(String token){

        SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString(TAG_TOKEN, token);

        editor.apply();

        return true;

    }

    //this method will fetch the device token from shared preferences

    public String getDeviceToken(){

        SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);

        return  sharedPreferences.getString(TAG_TOKEN, null);

    }

}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">


    <Button

        android:layout_centerVertical="true"

        android:text="Display Token"

        android:id="@+id/buttonDisplayToken"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


    <TextView

        android:id="@+id/textViewToken"

        android:layout_alignParentBottom="true"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />



</RelativeLayout>


·         MainActivity.java

package com.pushnotification;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //defining views

    private Button buttonDisplayToken;

    private TextView textViewToken;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //getting views from xml

        textViewToken = (TextView) findViewById(R.id.textViewToken);

        buttonDisplayToken = (Button) findViewById(R.id.buttonDisplayToken);

        //adding listener to view

        buttonDisplayToken.setOnClickListener(this);

    }

    @Override

    public void onClick(View view) {

        if (view == buttonDisplayToken) {

            //getting token from shared preferences

            String token = SharedPrefManager.getInstance(this).getDeviceToken();

            //if token is not null

            if (token != null) {

                //displaying the token

                textViewToken.setText(token);

            } else {

                //if token is null that means something wrong

                textViewToken.setText("Token not generated");

            }

        }

    }

}

Creating Push Notification

·                   MyFirebaseMessagingService.java

package com.pushnotification;

/**
 * Created by JAINISH on 2/12/2017.
 */

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
   
private static final String TAG = "MyFirebaseMsgService";
   
@Override
   
public void onMessageReceived(RemoteMessage remoteMessage) {

    }
}

·         AndroidManifest.xml

Service we need to define this class in side AndroidManifest.xml. inside AndroidManifest.xml and write code inside application tag.

<service

    android:name=".MyFirebaseMessagingService">

    <intent-filter>

        <action android:name="com.google.firebase.MESSAGING_EVENT"/>

    </intent-filter>

</service>

·         MyNotificationManager.java
package com.pushnotification;

/**

 * Created by JAINISH on 2/12/2017.

 */

import android.content.Intent;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;

import com.google.firebase.messaging.RemoteMessage;

import org.json.JSONException;

import org.json.JSONObject;


public class MyFirebaseMessagingService extends FirebaseMessagingService {


    private static final String TAG = "MyFirebaseMsgService";

    @Override

    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage.getData().size() > 0) {

            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {

                JSONObject json = new JSONObject(remoteMessage.getData().toString());

                sendPushNotification(json);

            } catch (Exception e) {

                Log.e(TAG, "Exception: " + e.getMessage());

            }

        }

    }

    //this method will display the notification

    //We are passing the JSONObject that is received from

    //firebase cloud messaging

    private void sendPushNotification(JSONObject json) {

        //optionally we can display the json into log

        Log.e(TAG, "Notification JSON " + json.toString());

        try {

            //getting the json data

            JSONObject data = json.getJSONObject("data");

            //parsing json data

            String title = data.getString("title");

            String message = data.getString("message");

            String imageUrl = data.getString("image");

            //creating MyNotificationManager object

            MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());

            //creating an intent for the notification

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);

            //if there is no image

            if(imageUrl.equals("null")){

                //displaying small notification

                mNotificationManager.showSmallNotification(title, message, intent);

            }else{

                //if there is an image

                //displaying a big notification

                mNotificationManager.showBigNotification(title, message, imageUrl, intent);

            }

        } catch (JSONException e) {

            Log.e(TAG, "Json Exception: " + e.getMessage());

        } catch (Exception e) {

            Log.e(TAG, "Exception: " + e.getMessage());

        }

    }

}

Creating Database



·         add library build.gradle

compile 'com.android.volley:volley:1.0.0'

·         activity_main.xml

we will modify the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">



    <EditText

        android:layout_above="@+id/buttonRegister"

        android:hint="Enter email"

        android:id="@+id/editTextEmail"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />



    <Button

        android:layout_centerVertical="true"

        android:text="Register Device"

        android:id="@+id/buttonRegister"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />



</RelativeLayout>

·         MainActivity.java

package com.pushnotification;


import android.app.ProgressDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ProgressBar;

import android.widget.TextView;

import android.widget.Toast;

import com.android.volley.AuthFailureError;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.StringRequest;

import com.android.volley.toolbox.Volley;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.HashMap;

import java.util.Map;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //defining views

    private Button buttonRegister;

    private EditText editTextEmail;

    private ProgressDialog progressDialog;

    //URL to RegisterDevice.php

    private static final String URL_REGISTER_DEVICE = "http://jrinfoway.com/Android_Eaxmple/push_notification/RegisterDevice.php";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        //getting views from xml

        editTextEmail = (EditText) findViewById(R.id.editTextEmail);

        buttonRegister = (Button) findViewById(R.id.buttonRegister);



        //adding listener to view

        buttonRegister.setOnClickListener(this);

    }



    //storing token to mysql server

    private void sendTokenToServer() {

        progressDialog = new ProgressDialog(this);

        progressDialog.setMessage("Registering Device...");

        progressDialog.show();



        final String token = SharedPrefManager.getInstance(this).getDeviceToken();

        final String email = editTextEmail.getText().toString();



        if (token == null) {

            progressDialog.dismiss();

            Toast.makeText(this, "Token not generated", Toast.LENGTH_LONG).show();

            return;

        }



        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGISTER_DEVICE,

                new Response.Listener<String>() {

                    @Override

                    public void onResponse(String response) {

                        progressDialog.dismiss();

                        try {

                            JSONObject obj = new JSONObject(response);

                            Toast.makeText(MainActivity.this, obj.getString("message"), Toast.LENGTH_LONG).show();

                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    }

                },

                new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {

                        progressDialog.dismiss();

                        Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();

                    }

                }) {



            @Override

            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();

                params.put("email", email);

                params.put("token", token);

                return params;

            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(stringRequest);

    }



    @Override

    public void onClick(View view) {

        if (view == buttonRegister) {

            sendTokenToServer();

        }

    }

}

·         Getting Firebase Server Key

Go to Firebase Console and inside your project settings go to cloud messaging.
Find the Cloud Messaging  Server Key here, Copy it.
Now go inside Config.php file and put your Server Key.




·         EndPoints.java

package com.pushnotification;

/**
 * Created by JAINISH on 2/14/2017.
 */

public class EndPoints {
   
public static final String URL_REGISTER_DEVICE = "http://jrinfoway.com/Android_Eaxmple/push_notification/RegisterDevice.php";
   
public static final String URL_SEND_SINGLE_PUSH = "http://jrinfoway.com/Android_Eaxmple/push_notification/sendSinglePush.php";
   
public static final String URL_SEND_MULTIPLE_PUSH = "http://jrinfoway.com/Android_Eaxmple/push_notification/sendMultiplePush.php";
   
public static final String URL_FETCH_DEVICES = "http://jrinfoway.com/Android_Eaxmple/push_notification/GetRegisteredDevices.php";
}

·         activity_main.xml

we will modify the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context=".MainActivity">

    <
EditText
       
android:layout_above="@+id/buttonRegister"
       
android:hint="Enter email"
       
android:id="@+id/editTextEmail"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content" />

    <
Button
       
android:layout_centerVertical="true"
       
android:text="Register Device"
       
android:id="@+id/buttonRegister"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content" />

   
<!-- this button is added -->
   
<Button
       
android:text="Send Notification"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/buttonRegister"
       
android:layout_centerHorizontal="true"
       
android:id="@+id/buttonSendNotification" />

</
RelativeLayout>

·         MainActivity.java

package com.pushnotification;


import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   
//defining views
   
private Button buttonSendPush;
   
private Button buttonRegister;
   
private EditText editTextEmail;
   
private ProgressDialog progressDialog;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
//getting views from xml
       
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
       
buttonRegister = (Button) findViewById(R.id.buttonRegister);
       
buttonSendPush = (Button) findViewById(R.id.buttonSendNotification);

       
//adding listener to view
       
buttonRegister.setOnClickListener(this);
       
buttonSendPush.setOnClickListener(this);
    }

   
//storing token to mysql server
   
private void sendTokenToServer() {
       
progressDialog = new ProgressDialog(this);
       
progressDialog.setMessage("Registering Device...");
       
progressDialog.show();

       
final String token = SharedPrefManager.getInstance(this).getDeviceToken();
       
final String email = editTextEmail.getText().toString();

       
if (token == null) {
           
progressDialog.dismiss();
            Toast.makeText(
this, "Token not generated", Toast.LENGTH_LONG).show();
           
return;
        }

        StringRequest stringRequest =
new StringRequest(Request.Method.POST, EndPoints.URL_REGISTER_DEVICE,
               
new Response.Listener<String>() {
                   
@Override
                   
public void onResponse(String response) {
                       
progressDialog.dismiss();
                       
try {
                            JSONObject obj =
new JSONObject(response);
                            Toast.makeText(MainActivity.
this, obj.getString("message"), Toast.LENGTH_LONG).show();
                        }
catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
               
new Response.ErrorListener() {
                   
@Override
                   
public void onErrorResponse(VolleyError error) {
                       
progressDialog.dismiss();
                        Toast.makeText(MainActivity.
this, error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }) {

           
@Override
           
protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params =
new HashMap<>();
                params.put(
"email", email);
                params.put(
"token", token);
               
return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(
this);
        requestQueue.add(stringRequest);
    }

   
@Override
   
public void onClick(View view) {
       
if (view == buttonRegister) {
            sendTokenToServer();
        }

       
//starting send notification activity
       
if(view == buttonSendPush){
            startActivity(
new Intent(this, ActivitySendPushNotification.class));
        }
    }
}

·         activity_send_push_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_send_push_notification"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context=".ActivitySendPushNotification">

    <
RadioGroup
       
android:id="@+id/radioGroup"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:orientation="horizontal">

        <
RadioButton
           
android:id="@+id/radioButtonSendAll"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="Send to All" />

        <
RadioButton
           
android:id="@+id/radioButtonSendOne"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="Send to One" />

    </
RadioGroup>

    <
Spinner
       
android:id="@+id/spinnerDevices"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"></Spinner>

    <
EditText
       
android:id="@+id/editTextTitle"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="Enter Message Title" />

    <
EditText
       
android:id="@+id/editTextMessage"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="Enter Message" />

    <
EditText
       
android:id="@+id/editTextImageUrl"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="Enter Image URL (Optional) " />

    <
Button
       
android:id="@+id/buttonSendPush"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="Send Push" />

</
LinearLayout>

·         MyVolley.java

package com.pushnotification;

/**
 * Created by JAINISH on 2/15/2017.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class MyVolley {

   
private static MyVolley mInstance;
   
private RequestQueue mRequestQueue;
   
private static Context mCtx;

   
private MyVolley(Context context) {
       
mCtx = context;
       
mRequestQueue = getRequestQueue();
    }

   
public static synchronized MyVolley getInstance(Context context) {
       
if (mInstance == null) {
           
mInstance = new MyVolley(context);
        }
       
return mInstance;
    }

   
public RequestQueue getRequestQueue() {
       
if (mRequestQueue == null) {
           
// getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
           
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
       
return mRequestQueue;
    }

   
public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

}

·         ActivitySendPushNotification.java

package com.pushnotification;



import android.app.ProgressDialog;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.text.TextUtils;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioGroup;

import android.widget.Spinner;

import android.widget.Toast;



import com.android.volley.AuthFailureError;

import com.android.volley.Request;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.StringRequest;



import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;



import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;



public class ActivitySendPushNotification extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {



    private Button buttonSendPush;

    private RadioGroup radioGroup;

    private Spinner spinner;

    private ProgressDialog progressDialog;



    private EditText editTextTitle, editTextMessage, editTextImage;



    private boolean isSendAllChecked;

    private List<String> devices;





    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_send_push_notification);



        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        spinner = (Spinner) findViewById(R.id.spinnerDevices);

        buttonSendPush = (Button) findViewById(R.id.buttonSendPush);



        editTextTitle = (EditText) findViewById(R.id.editTextTitle);

        editTextMessage = (EditText) findViewById(R.id.editTextMessage);

        editTextImage = (EditText) findViewById(R.id.editTextImageUrl);



        devices = new ArrayList<>();



        radioGroup.setOnCheckedChangeListener(this);

        buttonSendPush.setOnClickListener(this);



        loadRegisteredDevices();

    }



    //method to load all the devices from database

    private void loadRegisteredDevices() {

        progressDialog = new ProgressDialog(this);

        progressDialog.setMessage("Fetching Devices...");

        progressDialog.show();



        StringRequest stringRequest = new StringRequest(Request.Method.GET, EndPoints.URL_FETCH_DEVICES,

                new Response.Listener<String>() {

                    @Override

                    public void onResponse(String response) {

                        progressDialog.dismiss();

                        JSONObject obj = null;

                        try {

                            obj = new JSONObject(response);

                            if (!obj.getBoolean("error")) {

                                JSONArray jsonDevices = obj.getJSONArray("devices");



                                for (int i = 0; i < jsonDevices.length(); i++) {

                                    JSONObject d = jsonDevices.getJSONObject(i);

                                    devices.add(d.getString("email"));

                                }



                                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(

                                        ActivitySendPushNotification.this,

                                        android.R.layout.simple_spinner_dropdown_item,

                                        devices);



                                spinner.setAdapter(arrayAdapter);

                            }

                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    }

                },

                new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {



                    }

                }) {



        };

        MyVolley.getInstance(this).addToRequestQueue(stringRequest);



    }



    //this method will send the push

    //from here we will call sendMultiple() or sendSingle() push method

    //depending on the selection

    private void sendPush() {

        if(isSendAllChecked){

            sendMultiplePush();

        }else{

            sendSinglePush();

        }

    }



    private void sendMultiplePush(){

        final String title = editTextTitle.getText().toString();

        final String message = editTextMessage.getText().toString();

        final String image = editTextImage.getText().toString();

        final String email = spinner.getSelectedItem().toString();



        progressDialog.setMessage("Sending Push");

        progressDialog.show();



        StringRequest stringRequest = new StringRequest(Request.Method.POST, EndPoints.URL_SEND_SINGLE_PUSH,

                new Response.Listener<String>() {

                    @Override

                    public void onResponse(String response) {

                        progressDialog.dismiss();



                        Toast.makeText(ActivitySendPushNotification.this, response, Toast.LENGTH_LONG).show();

                    }

                },

                new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {



                    }

                }) {

            @Override

            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();

                params.put("title", title);

                params.put("message", message);



                if (!TextUtils.isEmpty(image))

                    params.put("image", image);

                params.put("email", email);

                return params;

            }

        };

        MyVolley.getInstance(this).addToRequestQueue(stringRequest);

    }

    private void sendSinglePush(){

        final String title = editTextTitle.getText().toString();

        final String message = editTextMessage.getText().toString();

        final String image = editTextImage.getText().toString();

        progressDialog.setMessage("Sending Push");

        progressDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, EndPoints.URL_SEND_MULTIPLE_PUSH,

                new Response.Listener<String>() {

                    @Override

                    public void onResponse(String response) {

                        progressDialog.dismiss();



                        Toast.makeText(ActivitySendPushNotification.this, response, Toast.LENGTH_LONG).show();

                    }

                },

                new Response.ErrorListener() {

                    @Override

                    public void onErrorResponse(VolleyError error) {

                    }

                }) {

            @Override

            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();

                params.put("title", title);

                params.put("message", message);



                if (!TextUtils.isEmpty(image))

                    params.put("image", image);

                return params;

            }

        };



        MyVolley.getInstance(this).addToRequestQueue(stringRequest);

    }

    @Override

    public void onCheckedChanged(RadioGroup radioGroup, int i) {

        switch (radioGroup.getCheckedRadioButtonId()) {

            case R.id.radioButtonSendAll:

                isSendAllChecked = true;

                spinner.setEnabled(false);

                break;

            case R.id.radioButtonSendOne:

                isSendAllChecked = false;

                spinner.setEnabled(true);

                break;

        }

    }



    @Override

    public void onClick(View view) {

        //calling the method send push on button click

        sendPush();

    }

}

PHP Files

·         Config.php

<?php
 define('DB_USERNAME','your user name');
 define('DB_PASSWORD','your password');
 define('DB_NAME','your database name');
 define('DB_HOST','localhost');
//defined a new constant for firebase api key
 define('FIREBASE_API_KEY', ‘YOUR_SERVER_KEY');


·         DbConnect.php
<?php
//Class DbConnect
class DbConnect
{
    //Variable to store database link
    private $con;
    //Class constructor
    function __construct()
    {
    }
    //This method will connect to the database
    function connect()
    {
        //Including the config.php file to get the database constants
        include_once dirname(__FILE__) . '/Config.php';
        //connecting to mysql database
        $this->con = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
        //Checking if any error occured while connecting
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        //finally returning the connection link
        return $this->con;
    }
}

·         DbOperation.php
<?php
class DbOperation
{
    //Database connection link
    private $con;
    //Class constructor
    function __construct()
    {
        //Getting the DbConnect.php file
        require_once dirname(__FILE__) . '/DbConnect.php';
        //Creating a DbConnect object to connect to the database
        $db = new DbConnect();
        //Initializing our connection link of this class
        //by calling the method connect of DbConnect class
        $this->con = $db->connect();
    }
    //storing token in database
    public function registerDevice($email,$token){
        if(!$this->isEmailExist($email)){
            $stmt = $this->con->prepare("INSERT INTO devices (email, token) VALUES (?,?) ");
            $stmt->bind_param("ss",$email,$token);
            if($stmt->execute())
                return 0; //return 0 means success
            return 1; //return 1 means failure
        }else{
            return 2; //returning 2 means email already exist
        }
    }
    //the method will check if email already exist
    private function isEmailexist($email){
        $stmt = $this->con->prepare("SELECT id FROM devices WHERE email = ?");
        $stmt->bind_param("s",$email);
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }
    //getting all tokens to send push to all devices
    public function getAllTokens(){
        $stmt = $this->con->prepare("SELECT token FROM devices");
        $stmt->execute();
        $result = $stmt->get_result();
        $tokens = array();
        while($token = $result->fetch_assoc()){
            array_push($tokens, $token['token']);
        }
        return $tokens;
    }
    //getting a specified token to send push to selected device
    public function getTokenByEmail($email){
        $stmt = $this->con->prepare("SELECT token FROM devices WHERE email = ?");
        $stmt->bind_param("s",$email);
        $stmt->execute();
        $result = $stmt->get_result()->fetch_assoc();
        return array($result['token']);       
    }
    //getting all the registered devices from database
    public function getAllDevices(){
        $stmt = $this->con->prepare("SELECT * FROM devices");
        $stmt->execute();
        $result = $stmt->get_result();
        return $result;
    }
}
?>
·         Firebase.php
<?php
class Firebase {
    public function send($registration_ids, $message) {
        $fields = array(
            'registration_ids' => $registration_ids,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }  
    /*
    * This function will make the actuall curl request to firebase server
    * and then the message is sent
    */
    private function sendPushNotification($fields) {    
        //importing the constant files
        require_once 'Config.php';      
        //firebase server url to send the curl request
        $url = 'https://fcm.googleapis.com/fcm/send';
        //building headers for the request
        $headers = array(
            'Authorization: key=' . FIREBASE_API_KEY,
            'Content-Type: application/json'
        );
        //Initializing curl to open a connection
        $ch = curl_init();
        //Setting the curl url
        curl_setopt($ch, CURLOPT_URL, $url);
        //setting the method as post
        curl_setopt($ch, CURLOPT_POST, true);
        //adding headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //disabling ssl support
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);       
        //adding the fields in json format
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        //finally executing the curl request
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        //Now close the connection
        curl_close($ch);
        //and return the result
        return $result;
    }
}
?>
·         GetRegisteredDevices.php
<?php  
 require_once 'DbOperation.php';
 $db = new DbOperation();  
 $devices = $db->getAllDevices();
 $response = array();  
 $response['error'] = false;
 $response['devices'] = array();

 while($device = $devices->fetch_assoc()){
 $temp = array();
 $temp['id']=$device['id'];
 $temp['email']=$device['email'];
 $temp['token']=$device['token'];
 array_push($response['devices'],$temp);
 }
 echo json_encode($response);
?>

·         Push.php
<?php
class Push {
    //notification title
    private $title;
    //notification message
    private $message;
    //notification image url
    private $image;
    //initializing values in this constructor
    function __construct($title, $message, $image) {
         $this->title = $title;
         $this->message = $message;
         $this->image = $image;
    }   
    //getting the push notification
    public function getPush() {
        $res = array();
        $res['data']['title'] = $this->title;
        $res['data']['message'] = $this->message;
        $res['data']['image'] = $this->image;
        return $res;
    }
}
?>


·         RegisterDevice.php
<?php
 require_once 'DbOperation.php';
 $response = array();  
 if($_SERVER['REQUEST_METHOD']=='POST'){
 $token = $_POST['token'];
 $email = $_POST['email'];
 $db = new DbOperation();  
 $result = $db->registerDevice($email,$token);
 if($result == 0){
 $response['error'] = false;
 $response['message'] = 'Device registered successfully';
 }elseif($result == 2){
 $response['error'] = true;
 $response['message'] = 'Device already registered';
 }else{
 $response['error'] = true;
 $response['message']='Device not registered';
 }
 }else{
 $response['error']=true;
 $response['message']='Invalid Request...';
 }
 echo json_encode($response);
?>


·         SendMultiplePush.php
<?php
//importing required files
require_once 'DbOperation.php';
require_once 'Firebase.php';
require_once 'Push.php';
$db = new DbOperation();
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){          
            //hecking the required params
            if(isset($_POST['title']) and isset($_POST['message'])) {
                        //creating a new push
                        $push = null;
                        //first check if the push has an image with it
                        if(isset($_POST['image'])){
                                    $push = new Push(
                                                            $_POST['title'],
                                                            $_POST['message'],
                                                            $_POST['image']
                                                );
                        }else{
                                    //if the push don't have an image give null in place of image
                                    $push = new Push(
                                                            $_POST['title'],
                                                            $_POST['message'],
                                                            null
                                                );
                        }
                        //getting the push from push object
                        $mPushNotification = $push->getPush();
                        //getting the token from database object
                        $devicetoken = $db->getAllTokens();
                        //creating firebase class object
                        $firebase = new Firebase();
                        //sending push notification and displaying result
                        echo $firebase->send($devicetoken, $mPushNotification);
            }else{
                        $response['error']=true;
                        $response['message']='Parameters missing';
            }
}else{
            $response['error']=true;
            $response['message']='Invalid request';
}
echo json_encode($response);
·         SendSinglePush.php
<?php
//importing required files
require_once 'DbOperation.php';
require_once 'Firebase.php';
require_once 'Push.php';  
$db = new DbOperation();
$response = array();

if($_SERVER['REQUEST_METHOD']=='POST'){
 //hecking the required params
 if(isset($_POST['title']) and isset($_POST['message']) and isset($_POST['email'])){
 //creating a new push
 $push = null;
 //first check if the push has an image with it
 if(isset($_POST['image'])){
 $push = new Push(
 $_POST['title'],
 $_POST['message'],
 $_POST['image']
 );
 }else{
 //if the push don't have an image give null in place of image
 $push = new Push(
 $_POST['title'],
 $_POST['message'],
 null
 );
 }
 //getting the push from push object
 $mPushNotification = $push->getPush();

 //getting the token from database object
 $devicetoken = $db->getTokenByEmail($_POST['email']);

 //creating firebase class object
 $firebase = new Firebase();

 //sending push notification and displaying result
 echo $firebase->send($devicetoken, $mPushNotification);
 }else{
 $response['error']=true;
 $response['message']='Parameters missing';
 }
}else{
 $response['error']=true;
 $response['message']='Invalid request';
}

echo json_encode($response);