Firebase: Firebase is a mobile platform, that helps you to quickly develop high-quality apps.
For the push notification implementation using Firebase Cloud Messaging, we will have to log in in the Firebase console. To add Firebase to your Application, you will need a Firebase project and Firebase configuration file.
Steps to follow are mentioned below:
- Create a new project in Firebase console.
- Name the project in Firebase console.
- After creating the project, you will get Firebase overview page. Select “Add Firebase to your Android Application”.
- After adding an Application to your Firebase project, enter your Application’s package name, when prompted. Here SHA-1 signing certificate is optional.
- After setting the package name of your Application , you’ll download a Google-Services.json file.
- Now, just move the downloaded Google-Services.json into your Android app module root directory. Path : app/.
- Update the root level build.gradle file by adding Google-Services plugin in the dependencies block.
- In your module Gradle file (usually the app/build.gradle) file, add the apply plugin line at the bottom of the file to enable the Gradle plugin.
- Send your first message to the single device (On the basis of FCM Token) or you can broadcast the message to the multiple devices as per your Application's package name.
- To the group of devices (On the basis of application package name).
- To the single device (On the basis of FCM token).
Source code is given below:
MainActivity.java
- package com.abhijeet.firebasenotificationdemo;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
-
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
-
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
-
- if (id == R.id.action_settings) {
- return true;
- }
-
- return super.onOptionsItemSelected(item);
- }
- }
FirebaseMessagingServiceDemo.java - package com.abhijeet.firebasenotificationdemo;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
-
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
-
-
- public class FirebaseMessagingServiceDemo extends FirebaseMessagingService {
-
- private static final String TAG = "FirebaseMsgServiceDemo";
-
- @Override
- public void onMessageReceived(RemoteMessage remotemsg) {
-
- Log.d(TAG, "From -> " + remotemsg.getFrom());
- Log.d(TAG, "Demo Notification Body -> " + remotemsg.getNotification().getBody());
- sendNotification(remotemsg.getNotification().getBody());
- }
-
-
- private void sendNotification(String messageBody) {
- Intent intent = new Intent(this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
- PendingIntent.FLAG_ONE_SHOT);
-
- Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("Demo Notification")
- .setContentText(messageBody)
- .setAutoCancel(true)
- .setSound(soundUri)
- .setContentIntent(pendingIntent);
-
- NotificationManager notificationManager =
- (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0, notificationBuilder.build());
- }
- }
FirebaseInstanceIDServiceDemo.java - package com.abhijeet.firebasenotificationdemo;
- import android.util.Log;
- import com.google.firebase.iid.FirebaseInstanceId;
- import com.google.firebase.iid.FirebaseInstanceIdService;
-
- public class FirebaseInstanceIDServiceDemo extends FirebaseInstanceIdService {
- private static final String TAG = "FirebaseIIDServiceDemo";
- public String[] name;
-
- @Override
- public void onTokenRefresh() {
- String token = FirebaseInstanceId.getInstance().getToken();
- System.out.println("my firebase token " + token );
- }
- private void sendRegistrationToServer(String token) {
-
- }
- }
Output
You can also check the analytics of all the notifications in the Firebase console.