Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chat App using Firebase: Get notification when new message received - Android

Tags:

I am developing a chat app using Firebase Realtime Database. I have been able to send and receive messages properly. Now, I want to implement notification whenever new message is received. For that, I have created a Service which listens to database changes using ChildEventListener and creates notification. The problem is that I am creating notification in onChildAdded method and this method fires both for existing node in database and new one. This is causing notification to be created multiple times for same message whenever user navigate back and forth from app.

Here is how I am implementing it:

chatMsgsRef.orderByChild(FirebaseDBKeys.LOCATION_LAST_UPDATED).addChildEventListener(new ChildEventListener() {              @Override             public void onChildAdded(DataSnapshot dataSnapshot, String s) {                  ChatMessage message = dataSnapshot.getValue(ChatMessage.class);                  if (!message.getSenderId().equals(currentUserId)) {                      mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);                      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NotificationsService.this)                             .setSmallIcon(R.drawable.message)                             .setContentTitle("New Message from " + message.getReceipientName())                             .setContentText(message.getMessage())                             .setOnlyAlertOnce(true)                             .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));                     mBuilder.setAutoCancel(true);                     mBuilder.setLocalOnly(false);                       mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());                  }             }              @Override             public void onChildChanged(DataSnapshot dataSnapshot, String s) {              }              @Override             public void onChildRemoved(DataSnapshot dataSnapshot) {              }              @Override             public void onChildMoved(DataSnapshot dataSnapshot, String s) {              }              @Override             public void onCancelled(DatabaseError databaseError) {              }         }); 

How can I implement notifications in the way it works in other chat applications like whatsapp, etc.??

like image 686
Vaibhav Agarwal Avatar asked Sep 15 '16 18:09

Vaibhav Agarwal


People also ask

How to handle notification when app in background in firebase Android?

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

How do I get firebase notifications on Android?

Handling NotificationsGo to the app build gradle file. In the dependencies section, add the Firebase Cloud Messaging dependency and sync the project. Create a new class call PushNotificationService and have it extend the FirebaseMessagingService class. Go to the manifest file and add the service to the app.


1 Answers

The correct way to do this is to have a push data message to the client with the chat when there is a new chat sent to them.

This blog post explains how to accomplish this. Basically you need to set up a server to listen to the chat firebase ref and send push notifications when it is updated. This way your clients can be in the app or out of the app and still get the push.

If you use a service there are a number of potential issues.

First of all you will have to keep the phone awake. This will drain the battery.

Second, Android can kill your background service at any time, so your app may stop working suddenly.

Third, with Doze mode Android will block network activity and stop your app from running in the background.

like image 124
Shmuel Avatar answered Oct 13 '22 19:10

Shmuel