How to Send Notification through Firebase-Android

Sending Your Daily Updates and Any Data to Android Application can be Easily Done as Dynamically Through the Usage of FireBase Cloud Messaging Service provided by Google services.

This is Maintained as Free of Cost untill your usage crosses the limit of 1GB.Let us see how we can add Notification services to your application.


Method-1.


Step-1

Open your android application and go to Tools->Firebase



Clicking the Firebase link Opens Up the New Window as Below


Choose Cloud Messaging in that Window


Click on Setup Firebase Cloud Messaging


Another Window Will displayed above this Window


Step-2

A)Click on Connect to Firebase


B)It Opens Up the Another Window


I)Select Create new Firebase project and give the Name.This name should not to be equal as your play store Name.This name just for your identification.


II)Select your Country Region.

III)Click Connect to Firebase Button.

This will take few minutes to create your application into Firebase Console.

Once it is successfully connected it shows a green button.


C)click on Add FCM to your app,This adds gradle dependencies to your application



Click on Accept Changes

This will take few minutes to add dependencies and to sync your gradle.


C)Now you have to Create the Registration token of your application to your firebase.

i)Create two Java Files .


  • MyFirebaseMessagingService
  • MyFirebaseInstanceIdService.
You can Give any name for your java file.as for my reference i gave above ids.

In MyFirebaseMessagingService java file write the below code




public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {


    @Override    public void onMessageReceived(RemoteMessage remoteMessage) {

        Intent intent=new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("SMT NOTIFICATION CENTER");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());

    }


}

In the line 1 MainActivity.Class represents the activity to be
launched when Notication clicked.

In the line 8 R.mipmap.ic_launcher is the symbol to be represented

Add Two String in the String folder under values,,,res->values->strings

<string name="FCM_PREF">packagename.fcm_pref</string>
<string name="FCM_TOKEN">packagename.fcm_token</string>

Here fcm_pref,fcm_token will be a keyword.Package name is mentioned on 
gradle file.copy and paste it.


In MyFirebaseInstanceIdService write the below code

public class MyFirebaseInstanceIdService extends com.google.firebase.iid.FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override    public void onTokenRefresh() {

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

        SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(getString(R.string.FCM_TOKEN),refreshedToken);
        editor.commit();

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


    }

}

IV)Add Service dependencies to Manifest file.

<service android:name="packagename.MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name="packagename.MyFirebaseInstanceIdService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

Here packagename represents your app package name.
MyFirebaseMessagingService,MyFirebaseInstanceId is the java class you created.


V)Sending an Message to application.


1)goto firebase.google.com

2)goto Firebase console.
3)Choose project
3) notification in the side menu.





4)click new Message




5)Type the Message




6)Choose package



Press send Message



Output





Thankyou.For any queries comment in the screen.

Comments

Popular posts from this blog

How to Add Animation to Image in Android

Splash Screen-Android