当应用程序运行时,react-native-firebase收到通知

时间:2018-12-09 17:42:30

标签: android firebase react-native notifications

我正在使用react-native-firebase实现推送通知。因此,我正在测试云消息传递。当屏幕关闭且处于后台时,我会收到通知。但是,当应用程序运行时,我无法收到通知。我的代码中是否有任何问题,或者我需要添加一些代码?

async createNotificationListeners() {

    this.notificationListener = firebase.notifications.onNotification((notification) => {
        const { title, body } = notification;
        this.showAlert(title, body);
    });

    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
        const { title, body } = notificationOpen.notification;
        this.showAlert(title, body);
    });

    const notificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
        const { title, body } = notificationOpen.notification;
        this.showAlert(title, body);
    }

    this.messageListener = firebase.messaging().onMessage((message) => {
        //process data message
        console.log(JSON.stringify(message));
    });
}

componentWillUnmount() {
    this.notificationListener();
    this.notificationOpenedListener();
}

1 个答案:

答案 0 :(得分:0)

推送通知不能在App中出现 ,因为这实际上与它的目的相反。合理的definition推送通知是在没有来自客户端的特定请求的情况下,将信息从软件应用程序传递到计算设备。在使用应用程序期间,客户端将向服务器发出特定请求。您应该以不同方式处理通知,例如使用带编号的图标。您也可以探索Firebase In-App Messaging以获得更吸引人的体验。