【问题标题】:Firebase messaging can not receive notification when app close ( React Native )应用关闭时 Firebase 消息无法收到通知(React Native)
【发布时间】:2018-07-15 04:17:24
【问题描述】:

我有一个使用 Firebase 推送通知的应用。 在我的应用中,我实现了 2 种方法:

        firebase.messaging().onMessage((message) => {        
                senName = message.data.senderName;
                senUid = message.data.senderUid;
                const notification = new 
                firebase.notifications.Notification()
                    .setNotificationId('notificationId')
                    .setTitle(message.data.title)
                    .setBody(message.data.body)
                    .android.setChannelId('channel_id_foreground')
                    .android.setSmallIcon('ic_launcher');
                firebase.notifications().displayNotification(notification)
        });

        firebase.notifications().onNotificationOpened((notificationOpen) => {
            // Get the action triggered by the notification being opened
            const action = notificationOpen.action;
            // Get information about the notification that was opened
            const notification = notificationOpen.notification;             
        });

如果我的应用程序在前台和后台运行,它将正确显示通知。如果我什么都不做,只是关闭应用程序,它就不会显示通知。

但是当我在前台点击通知时,它将运行到 onNotificationOpened 方法,然后我通过滑动关闭应用程序,它仍然正常显示通知。

因此,如果我之前录制过通知,它只会在关闭/滑动应用程序的情况下显示通知。

谁能帮帮我?

【问题讨论】:

  • 您是否尝试过提高通知优先级?

标签: firebase react-native push-notification


【解决方案1】:

安卓

要让应用在关闭时(或在后台)获得通知,它需要注册一个处理这些消息的后台任务,然后在必要时打开应用。

要创建此任务,请使用 react-native 的 AppRegistry.registerHeadlessTask

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', handler);

其中 handler 是一个返回消息处理程序的函数:

const handler = () => message => {
    // Do something with the message
}

要处理操作(在 Android 上),您需要另一个任务:

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', actionHandler);

handler 又是这样的:

const actionHandler = () => message => {
    // Do something with message
}

要使这一切正常工作,您需要使用以下内容更新清单:

<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
<receiver android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionReceiver" android:exported="true">
    <intent-filter>
        <action android:name="io.invertase.firebase.notifications.BackgroundAction"/>
    </intent-filter>
</receiver>
<service android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionsService"/>

关于在后台设置消息的文档是here,用于操作的文档是here

iOS

在 iOS 上,您无法发送仅数据通知,因此您必须在通知本身(服务器端)中包含标题和文本。

如果你这样做,那么你的手机会自动显示通知,你只需要处理正在打开的通知。

两者

你也可以在通知显示的时候做一些事情:

firebase.notifications().onNotificationDisplayed(notification => { ... })

或者当手机收到时:

firebase.notifications().onNotification(notification => { ... })

如果您想获取触发应用打开的通知,请使用以下内容:

firebase.notifications().getInitialNotification().then(notification => { ... })

所有这些的文档都可以在here找到。

【讨论】:

  • Kraylog,首先非常感谢您的有用建议。我已将后台任务注册为您的评论,但在关闭/滑动应用程序的情况下不会显示通知,除非在之前收到的通知上粘贴。目前我只是使用纯数据通知并在 Android 上实现。我曾尝试使用带有标题和正文的通知,但现在在后台不知道为什么在收到通知后,我的应用程序将崩溃。但是根据您的回答,仅数据不能在 iOS 上运行,对吗?所以我需要再次尝试使用通知消息。谢谢
  • 嗨,我怎样才能给RNFirebaseBackgroundMessage 发送一些东西?
【解决方案2】:

在版本 6 中我们需要添加

// index.js
import { AppRegistry } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import App from './App';

// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('Message handled in the background!', remoteMessage);
});

AppRegistry.registerComponent('app', () => App);

一旦您的逻辑完成以释放设备资源,处理程序必须返回一个承诺。它不得尝试更新任何 UI(例如通过状态) - 但是您可以执行网络请求、更新本地存储等。

来源:rnfirebase.io documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多