【问题标题】:How to access notification data after clicking on notification (firebase cloud messaging)单击通知后如何访问通知数据(firebase 云消息传递)
【发布时间】:2020-01-28 19:56:02
【问题描述】:

我已经为我的react-native app 实现了firebase cloud messaging,现在我可以发送和接收通知了。 但是现在我想得到Notifications Data,就像messages一样,点击它就可以了。

为什么我需要这个?

因为我有一个简单的聊天应用,假设我有三个房间,room1, room2, room3

现在我的App 已关闭,我从room1 收到Notification,然后我点击它,此时我希望它打开我的应用程序并导航到room1 chatbox,以及其他房间通知.

有什么帮助吗?

注意:我使用的是 react-native-firebase v6

【问题讨论】:

    标签: javascript react-native firebase-cloud-messaging react-native-firebase


    【解决方案1】:

    云消息仅用于从手机上的服务器发送消息。

    之前,在 firebase 5 上,我们有一个名为“通知”的包,它允许我们在您单击它时管理数据的拦截。

    自 Firebase 6 以来,此软件包不再存在(嗯,在某种程度上它将成为付费服务,此服务称为 Notifee,但仍在测试中)。

    您必须使用外部包,例如 react-native-push-notifications,它允许您拦截推送通知数据。

    【讨论】:

    • 谢谢,但react-native-push-notifications 没有像作者在github上所说的那样维护。那么还有其他更新的包吗?
    【解决方案2】:

    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));
            });
        }

    【讨论】:

    • 感谢您的努力,但我只使用云消息传递您提供的源代码,我无权访问。
    猜你喜欢
    • 2016-12-25
    • 2016-12-13
    • 2021-07-22
    • 2017-07-31
    • 1970-01-01
    • 2020-07-28
    • 2021-12-19
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多