首页 > 解决方案 > 在后台收听通知并启动应用程序

问题描述

如 react-native-firebase 文档(https://rnfirebase.io/docs/v5.xx/messaging/receiving-messages)中所述,我们可以注册一个 headlessTask 并在应用程序在后台时收听通知。遗憾的是,一旦在后台模式下收到 FCM 通知,我还没有找到在前台启动应用程序的方法,这可能吗?下面是在 react-native-firebase 上显示的用于在后台监听通知的代码

bgMessaging.js

// @flow
import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';

export default async (message: RemoteMessage) => {
    // handle your message

    return Promise.resolve();
}

//This handler method must return a promise and resolve within 60 seconds.

index.js

import bgMessaging from './src/bgMessaging'; // <-- Import the file you created in (2)

// Current main application
AppRegistry.registerComponent('ReactNativeFirebaseDemo', () => bootstrap);
// New task registration
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); // <-- Add this line

任何帮助都会非常有帮助

标签: react-nativereact-native-firebase

解决方案


当您收到通知时,使用以下库启动您的应用程序: https ://github.com/lvlrSajjad/react-native-launch-application

例如,在您的 ** bgMessaging** 中:

//PackageName Must Be String For example "com.domain.application"
export default async (message: RemoteMessage) => {
    SajjadLaunchApplication.open(".domain.application");
}

推荐阅读