markharrop
markharrop

Reputation: 876

Receiving a notification when Firebase node updates

I have 2 apps a customer booking app and an admin receiver app. they are both connected to the same Firebase database. When a customer makes a booking I can view this in my admin app. But how can I make it so that I receive a notification in the admin app once a booking is made in the customer app?

I have found this code but how could I implement it so that it shows the notification even if the admin app not open?

 Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(subject)
            .setContentText(object.getString("body"))
            .setAutoCancel(true)
            .setSound(notificationSoundURI)
            .setContentIntent(resultIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, mNotificationBuilder.build());

    ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, ToneGenerator.MAX_VOLUME);
    toneG.startTone(ToneGenerator.TONE_CDMA_HIGH_L, 3000);
    ((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(2000);

Edit

My Firebase tree looks like this

{
"Bookings",
"UserID Appears here",
"User booking info appears here"}
}

The bookings node is a constant and is always there, the user id appears once a booking is made. Could I have some sort of service that runs even when app closed that listens for the "UserID" node to update? then fire the notification method above? I've never worked with notification and services.

Upvotes: 0

Views: 371

Answers (2)

asim
asim

Reputation: 553

Code for your admin app

// Minimal Fcm Service
class FcmService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        remoteMessage.data.isNotEmpty().let {
            // data message, handle it or start a service
            Intent(this, ConnectionService::class.java).also { intent -> startService(intent) }
        }

        remoteMessage.notification?.let {
        // notification message, you can define your custom notification here or just leave it that way
        }
    }
}

And thats how you register it in your manifest

<service
    android:name="com.yourpackage.appname.FcmService"
    android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

Edit:

Notification message:

FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

Data message:

Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

Edit2: Java Code

public class FcmService extends FcmService {
    @Override
    public void onMessageReceived(@NotNull RemoteMessage remoteMessage) {
        if (!remoteMessage.getData().isEmpty()){
            // data message
            // start a service or handle it the way you want
        }
        
        if (remoteMessage.getNotification() != null){
            // notification message
        }
    }
}

Link to Official Fcm Service in java

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 600061

This is a typical use-case for Cloud Functions and Firebase Cloud Messaging. In fact, this scenario is so common that there's an example of it in the Firebase documentation called: notify users when something interesting happens:

enter image description here

Cloud Functions allow you to respond to events in your Firebase projects by running pieces of (Node.js) code on Google's servers. Since this code runs on Google's servers, it will be active even when the application is not active. And this code can then use the Firebase Admin SDK, to call other Firebase services, such as Cloud Messaging here.

Firebase Cloud Messaging allows you to send messages to an application that is installed on a device, even when that application is not actively being used. You'd typically use the code in your question in response to receiving such a message to then show a local notification on that device. Then when the user clicks on the notification, you open the app, and use the Realtime Database API to read the (rest of the) data from the server

For more on this, also see:

Upvotes: 1

Related Questions