Home > front end >  Don't show a notification from firebase in android studio
Don't show a notification from firebase in android studio

Time:05-12

This is the code in firebase functions:

exports.androidPushNotification = functions.database
.ref("/chat/{pushId}")
.onCreate((snapshot, context) => {
    admin.messaging().sendToTopic("notification", {
        data: {
            senderId: snapshot.val().senderId,
        },

        notification: {
            title: `${snapshot.val().name} has sent a message.`,
            body: snapshot.val().message,
        },
    });
});

This is onMessageReceived in android studio:

override fun onMessageReceived(remoteMessage: RemoteMessage) {

    if(remoteMessage.notification == null){
        return
    }

    val mAuth: FirebaseAuth = FirebaseAuth.getInstance()

    if(mAuth.currentUser?.uid == null){
        return
    }

    if(remoteMessage.data["senderId"] == mAuth.currentUser?.uid){
        return
    }

    generateNotification(remoteMessage.notification?.title!!, remoteMessage.notification?.body!!)
}

I have everyone subscribed to a topic called "notification".

This is for a chat app where everyone is in one big chat. I don't want the person who sent the message to receive the notification.

The code is working where it returns if the user sent it but the sender still receives the notification.

CodePudding user response:

If you're sending to a topic, there is no way to exclude one subscriber from receiving the message.

What you can do however is send a data only message and handle that in code. If you include a notification node however (as you do), the system will display that notification if the app is not actively being used. So the filtering you do only works if you only have a data node in the message, and no notification node.

  • Related