How push notification in app using firebase

Introduction

In this article we can discuss, how push notification in app using firebase. A lots of people face this problem, so today i can write article on this topic.

Procedure

  1. First of all create an account on google.
  2. Connect this google account with firebase.
  3. Add gradule in android Studio.
  4. Create project on firebase for cloud message.
  5. And then follow my video.

Code

Dependency

implementation 'com.google.firebase:firebase-messaging:17.0.0'

MainActivity.java

1. First Add this in import place

import com.google.firebase.messaging.FirebaseMessaging;
import android.app.NotificationChannel; 
import android.app.NotificationManager; 
import android.os.Build; 
import android.support.annotation.NonNull;
import android.widget.Toast; 
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; 
import com.google.firebase.messaging.FirebaseMessaging;

2. Paste this code in MainActivity

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel cahnnel = new NotificationChannel ( "MyNotifications", "MyNotifications", NotificationManager.IMPORTANCE_DEFAULT );

            NotificationManager notificationManager = getSystemService ( NotificationManager.class );
            notificationManager.createNotificationChannel ( cahnnel );
        }




        FirebaseMessaging.getInstance ().subscribeToTopic ( "general" )
                .addOnCompleteListener ( new OnCompleteListener () {
                    @Override
                    public void onComplete(@NonNull Task task) {



                        if (!task.isSuccessful ()) {

                            Toast.makeText ( MainActivity.this,"No Connection",Toast.LENGTH_LONG ).show ();

                        }
                        if (task.isSuccessful ()) {
                            Toast.makeText ( MainActivity.this, " Connection Successful", Toast.LENGTH_LONG ).show ();
                        }

                    }
                } );

Notification.java

import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

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

public class Notification extends FirebaseMessagingService
    {

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived ( remoteMessage );
            showNotification ( remoteMessage.getNotification ().getTitle(), remoteMessage.getNotification ().getBody () );
        }

        public void showNotification(String title, String name)
        {
            NotificationCompat.Builder builder = new NotificationCompat.Builder ( this,"MyNotifications" )
                    .setContentTitle ( title )
                    .setSmallIcon ( R.drawable.ic_launcher_background )
                    .setAutoCancel ( true )
                    .setContentText ( name );

            NotificationManagerCompat managerCompat= NotificationManagerCompat.from ( this );
            managerCompat.notify ( 999,builder.build () );

        }
    }

Working

Our main work is to generate notification in app using firebase, so for generating notification we can write some which is given. After code there are some step which are very for generate notification.

  1. I use MyNotifications in main activity. This is our channel which is used in friebase to generate notification.
  2. I use R.drawable.ic_launcher in Notification class. This image show on firebase notification so replace this image with your app icon.

Please Watch My Video if you are not know to generate a notification. i will completely guide in video.

 

One thought on “How push notification in app using firebase”

Leave a Reply

Your email address will not be published. Required fields are marked *