【问题标题】:Listening to notifications on background and start the app在后台收听通知并启动应用程序
【发布时间】:2019-11-22 15:27:01
【问题描述】:

如 react-native-firebase 文档 (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

任何帮助都会非常有帮助

【问题讨论】:

  • 用户需要点击通知图标才能启动应用程序AFAIK
  • @aytek 太糟糕了,这是一个 voip 应用程序,如果有来电,我们应该启动应用程序,在 ios 上我们使用推送套件来解决它,不确定是否有类似的选项 android

标签: react-native react-native-firebase


【解决方案1】:

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

【讨论】:

  • 这确实启动了应用程序但不是在前台,它直接打开应用程序,这可能在传入的android版本中是不可能的,也不是我想要的,我想在前台启动它并保留在那里
  • 即使屏幕被锁定,它也会将应用程序带到前台并显示它
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
相关资源
最近更新 更多