在后台收听通知并启动应用

时间:2019-11-22 15:27:01

标签: react-native react-native-firebase

如react-native-firebase docs(https://rnfirebase.io/docs/v5.x.x/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

任何帮助都会真正有用

1 个答案:

答案 0 :(得分:0)

在收到通知时,使用以下库启动您的应用程序: 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");
}