简体   繁体   English

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

[英]react-native-firebase receiving notification when app in running

I'm implementing push notification using react-native-firebase. 我正在使用react-native-firebase实现推送通知。 So, I'm testing the cloud messaging. 因此,我正在测试云消息传递。 I could receive the notification when screen is off and in background. 当屏幕关闭且处于后台时,我会收到通知。 But, I couldn't receive notification when the app on running. 但是,当应用程序运行时,我无法收到通知。 Are there any problem in my code or do i need to add some code? 我的代码中是否有任何问题,或者我需要添加一些代码?

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

Push Notifications cannot occur in App because that is actually contrary to it's a purpose. 推送通知无法在App中发生因为这实际上与它的目的相反。 A reasonable definition of a push notification is the delivery of information from a software application to a computing device without a specific request from the client. 推送通知的合理定义是无需客户端的特定请求即可将信息从软件应用程序传递到计算设备。 During app use, the client would be making specific requests to the server. 在使用应用程序期间,客户端将向服务器发出特定请求。 You should handle the notifications differently like using a numbered Icon for example. 您应该以不同方式处理通知,例如使用带编号的图标。 You could also explore Firebase In-App Messaging for a more engaging experience. 您也可以探索Firebase应用内消息传递以获得更有吸引力的体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 react-native-firebase v6,应用程序在前台收到通知时崩溃 - With react-native-firebase v6, app crashed when receive notification while in foreground React-native-firebase 后台通知(Android) - React-native-firebase notification in background (Android) react-native-firebase 通知崩溃 - react-native-firebase notification crash react-native-firebase-收到推送通知后应用崩溃(Android) - react-native-firebase - App Crash after Push Notification Received (Android) 推送通知发送到具有react-native-firebase的其他设备时的问题 - Problems When Push Notification Sending to Another Device With react-native-firebase 默认情况下如何使用react-native-firebase启用浮动通知? - How to enabled floating notification by default with react-native-firebase? @react-native-firebase/auth:应用程序未授权 signInWithPhoneNumber 错误 - @react-native-firebase/auth: app not authorized Error in signInWithPhoneNumber 如何使用 react-native-firebase/messaging(FCM) 从栏点击通知后将应用程序打开到特定页面? - How to open app to specific page after click notification from bar by using react-native-firebase/messaging(FCM)? 本机反应-应用程序被杀死/未启动时未收到通知 - react native - not receiving notification when app is killed/not started React Native Android App 在收到 FCM 通知时崩溃 - React Native Android App crashes when receiving FCM notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM