A. Inbar A. Inbar - 1 month ago 5
Android Question

How to notify a user through Firebase Database?

I am trying to do a real-time messaging app (not chat app like WhatsApp, I have one publisher and a lot of subscribers).

I want it to send a notification when I get new message.

That's my code(with errors):

mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Message message = postSnapshot.getValue(Message.class);
messages.add(0, message.getTitle() + '\n' + message.getMessage(), NotificationCompat.Builder mBuilder = //cannot resolve symbol 'mBuilder'
new NotificationCompat.Builder(this) //builder (android.content.Context) in Builder cannot be applied to (anonymous com.firebase.client.ChildEventListener)
.setSmallIcon(R.drawable.logo_he_digita_homepage)
.setContentTitle(message.getTitle())
.setContentText(message.getMessage()); //')' expected
Intent resultIntent = new Intent(this, updates.class); //Cannot resolve constructor 'Intent(anonymous com.firebase.client.ChildEventListener, java.lang.Class<com.inbar.amit.ksharet>)'
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); //create (android.content.Context) in TaskStackBuilder cannot be applied to (anonymous com.firebase.client.ChildEventListener)
stackBuilder.addParentStack(updates.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(124453245, mBuilder.build()););

}
adapter.notifyDataSetChanged();

}


I'm using Android Studio.

Thanks for the answers.

Answer

First of all, note that onChildAdded will be called for each child already in the database, and each child added after you start listening, so you will end up with a notification for every message in your database already.

UPDATE

So you need to create a new function for this code, because it doesn't work in a listener class. Here is code that should work for you:

mRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
        Message message = postSnapshot.getValue(Message.class);
        messages.add(0, message.getTitle() + '\n' + message.getMessage());
        sendNotification(message);

    }
        adapter.notifyDataSetChanged();

    }

Outside of other methods, add this method:

public void sendNotification(Message message) {
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this) 
            .setSmallIcon(R.drawable.logo_he_digita_homepage)
            .setContentTitle(message.getTitle())
            .setContentText(message.getMessage());
        Intent resultIntent = new Intent(this, updates.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(updates.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = 
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(124453245, mBuilder.build());

}