应用关闭时,Firebase消息无法接收通知(React Native)

时间:2018-07-15 04:17:25

标签: firebase react-native push-notification

我有一个使用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方法,然后我通过滑出关闭应用程序,它仍会正常显示通知。

因此,如果我以前在通知上进行录音,它只会在关闭/滑动应用程序的情况下显示通知。

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

Android

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

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

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', handler);

处理程序是返回消息处理程序的函数:

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

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

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', actionHandler);

处理程序再次类似于:

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中找到。

答案 1 :(得分:0)

在第 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