react-native - 在后台收听通知并启动应用程序
问题描述
如 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
任何帮助都会非常有帮助
解决方案
当您收到通知时,使用以下库启动您的应用程序: 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");
}
推荐阅读
- java - Java:instanceof vs 类名切换性能
- java - Java/Jackson - TypeReference() 是如何工作的?
- google-apps-script - 如何知道点击了哪个菜单项?
- javascript - CMS 的 Google Analytics API 授权(单一授权)
- angular - 在另一个框架内的特定 DOM 元素上引导 Angular 2+ 应用程序
- docker - docker里面的docker gitlab-ci.yml
- php - 无法从一个页面重定向到另一个页面
- sql - sql表中新增行的主键,vb.net
- python - 为什么我的位置查找功能出现值错误?
- spring - 事务不使用弹簧回滚