Sunday, August 21, 2016

Android Firebase Cloud Messaging

this titorial is written with the consideration that you are familiar  with GCM. if you are new to firebase please follow the official documentaion.


1 . go to the Firebase Console and create new project or you can import existing gcm project into firebase.

2. after project creating go to the project setting from top left cornet (click setting icon).
3. enter your package name , and download the google-services.json
4. copy google-services.json into your app directory .
5. add rules to your root-level build.gradle file, to include the google-services plugin:

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

6. Then, in your module Gradle file (usually the app/build.gradle), add the
apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin: 'com.android.application' android {   // ... } dependencies {   // ... com.google.firebase:firebase-messaging:9.4.0} // ADD THIS AT THE BOTTOM apply plugin: 'com.google.gms.google-services'
Note : please check libraries are available for the different Firebase features.
https://firebase.google.com/docs/android/setup

7. Retrieve the current registration token
 When you need to retrieve the current token, call FirebaseInstanceID.getToken().
 This method returns null if the token has not yet been generated.

public class FirebaseInstanceIDService extends 
FirebaseInstanceIdService {
 private static final String TAG = "MyFirebaseIIDService";  
 @Override   
 public void onTokenRefresh() {
    // Get updated InstanceID token.   
   String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the    
// Instance ID token to your app server.    
// send this token to your server so you can start 
// downstream messages to client.
   }
}
8.  Handle Message receiving 

create new service which extends FirebaseMessagingService . 
here i've created MyFirebaseMessagingService.java

example 

public class MyFirebaseMessagingServiceextends 
       FirebaseMessagingService  {

/** * Called when message is received.
* * @param remoteMessage Object representing the 
message received from Firebase Cloud Messaging. 
*/// [START receive_message]
@Overridepublic void onMessageReceived(RemoteMessage remoteMessage) 
{
     Log.d(TAG, "From: " + remoteMessage.getFrom());
   // Check if message contains a data payload.  
   if (remoteMessage.getData().size() > 0) {
      Log.d(TAG, "Message data payload: " + remoteMessage.getData());
   }
    // Check if message contains a notification payload.  
   if (remoteMessage.getNotification() != null) {
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
   }
}


9.  AndroidManifest.xml

<service    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
<service    android:name=".FirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
10.  Your MainActivity

//get token
String token = FirebaseInstanceId.getInstance().getToken();

//start subsription to firebase
FirebaseMessaging.getInstance().subscribeToTopic("news");

//for unsubscribtion
FirebaseMessaging.getInstance().unsubscribeFromTopic("news");
11. SERVER CODE FOR SENDING MESSAGES

please refer following link

https://github.com/firzan/php/edit/master/Gcm.php