Muhammad
Muhammad

Reputation: 2804

How to access notification data after clicking on notification (firebase cloud messaging)

I have implemented firebase cloud messaging for my react-native app and now I am able to send and receive Notifications. But now I want to get the Notifications Data like messages, right after clicking on it.

Why I need this ?

Because I have A simple chat app, and suppose I have three rooms, room1, room2, room3.

Now my App is closed and I receive Notification from room1, then I click on that, At this time I expected it open my app and navigate to the room1 chatbox, and the other rooms notifications too.

Any help?

note: I am using react-native-firebase v6

Upvotes: 0

Views: 1383

Answers (2)

ANKIT DETROJA
ANKIT DETROJA

Reputation: 2065

async componentDidMount() {
        this.createNotificationListeners();
    }
    
    async createNotificationListeners() {
        this.notificationListener = firebase.notifications().onNotification((notification) => {
            console.log(':::::::::::::::::::::::::::: APPLICATION OPEN MODE :::::::::::::::::::::::::::');
            console.log(notification, 'APPLICATION OPEN');
            // Manage Notifiacation

            // firebase.notifications().removeDeliveredNotification(notification._notificationId);
        });

        const channel = new firebase.notifications.Android.Channel('fcm_FirebaseNotifiction_default_channel', 'JobApp', firebase.notifications.Android.Importance.High)
            .setDescription('DEMO NOTIFICATION DESCRIPTION');

        firebase.notifications().android.createChannel(channel);
        this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
            console.log(':::::::::::::::::::::::::::: APPLICATION WORKING IN BACKGROUND MODE  :::::::::::::::::::::::::::');
            console.log(notificationOpen.notification.data);
            const { notificationType } = notificationOpen.notification.data;
            console.log(notificationType)

            firebase.notifications().removeDeliveredNotification(notificationOpen.notification._notificationId);
        });


        const notificationOpen = await firebase.notifications().getInitialNotification();
        if (notificationOpen) {
            console.log(':::::::::::::::::::::::::::: APPLICATION CLOSED  :::::::::::::::::::::::::::');
            console.log(notificationOpen);
        }
        this.messageListener = firebase.messaging().onMessage((message) => {
            console.log(JSON.stringify(message));
        });
    }

Upvotes: 0

Viktor Jovanovic
Viktor Jovanovic

Reputation: 571

Cloud Messaging is only used to send messages from a server on the phone.

Before, on firebase 5, we had a package called "notifications" which allowed us to manage the interception of data when you clicked on it.

Since Firebase 6, this package doesn't exist anymore (well, in a way it will become paying and this service is called Notifee but it is still in test).

You have to use external packages such as react-native-push-notifications which allows you to intercept push notifications data.

Upvotes: 1

Related Questions