简体   繁体   English

应用程序被杀死时 FCM 通知不会触发

[英]FCM Notification not firing when app is killed

FCM notifications work for me when the app is in background or in foreground but not when the app is killed.当应用程序处于后台或前台时,FCM 通知对我有用,但在应用程序被终止时不起作用。

Here's my FCM configuration code:这是我的 FCM 配置代码:

 Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print("Handling a background message: ${message.messageId}");
}
class CategoriesScreen extends StatefulWidget {
  static const routeName = '/view-cateogries';
  _CategoriesScreenState createState() => _CategoriesScreenState();
}

class _CategoriesScreenState extends State<CategoriesScreen> {
  Future _screenFuture;
  void initState() {
    _saveDeviceToken(FirebaseMessaging.instance);
     FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
    super.initState();
  }

I have read numeruos articles online and here on stackoverflow.我已经在网上和这里阅读了很多关于 stackoverflow 的文章。 For instance, one of the suggestions was to disable battery saver.例如,其中一项建议是禁用电池保护程序。 I have tried that but no luck.我试过了,但没有运气。 Any ideas what I am missing?有什么想法我想念的吗?

I am using firebase-messaging version ^10.0.2我正在使用 firebase-messaging 版本^10.0.2

Looks like you're calling the FirebaseMessaging.onBackgroundMessage() method inside initState() of a stateful widget which is not allowed as it is stated in the documentation:看起来您正在调用有状态小部件的initState()内的FirebaseMessaging.onBackgroundMessage()方法,如文档中所述,这是不允许的:

Since the handler runs in its own isolate outside your applications context, it is not possible to update application state or execute any UI impacting logic .由于处理程序在您的应用程序上下文之外在其自己的隔离中运行,因此无法更新应用程序 state 或执行任何影响 UI 的逻辑 You can however perform logic such as HTTP requests, IO operations (updating local storage), communicate with other plugins etc.但是,您可以执行诸如 HTTP 请求、IO 操作(更新本地存储)、与其他插件通信等逻辑。

You should set the background messaging handler before runApp() :您应该在runApp()之前设置后台消息处理程序:

/// Define a top-level named handler which background/terminated messages will
/// call.
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  await Firebase.initializeApp();
  print("Handling a background message: ${message.messageId}");
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize Firebase App
  await Firebase.initializeApp();

  // Set the background messaging handler early on, as a named top-level function
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  // Run your app
  runApp(HomeScreen());
}

Check this for details.检查以获取详细信息。

When the App is killed the only way to "do something" when FCM wants to trigger some behaviour is by using a BroadcastReceiver: https://chloe-thhsu.medium.com/a-simple-way-to-combine-fcm-notification-with-tts-b48a777ec84f (you need only one piece of example code at this link)当应用程序被杀死时,当 FCM 想要触发某些行为时“做某事”的唯一方法是使用 BroadcastReceiver: https://chloe-thhsu.medium.com/a-simple-way-to-combine-fcm- notification-with-tts-b48a777ec84f (您只需要此链接上的一段示例代码)

FCM should trigger some Intent and only registered App will receive it and do something. FCM 应该触发一些 Intent 并且只有注册的 App 才会接收它并做一些事情。

BroadcastReceivers works even when the App is closed/killed, but them allow only basic "actions" when them are triggered. BroadcastReceivers 即使在应用程序被关闭/杀死时也能工作,但是当它们被触发时它们只允许基本的“动作”。 For you it could be usable: " start and Activity using an Intent by passing XYZ argument " where your App/Activity will be launched and you can do "all what you want" from there.对您来说,它可能是可用的:“通过传递 XYZ 参数使用 Intent 启动和活动”将在其中启动您的应用程序/活动,您可以从那里执行“所有您想要的”。

You can trigger the notification onMessage function is by simply getting all the data in the data key instead of notification key.您可以通过简单地获取数据键而不是通知键中的所有数据来触发 onMessage function 通知。 The data that is being send, please update the key from notification to data.正在发送的数据,请将密钥从通知更新为数据。

Handling FCM notifications in the background even when app is terminated has been there for quite some time now.即使应用程序终止,在后台处理 FCM 通知也已经存在了很长一段时间。 The steps步骤

  void _setupFCM() async {
    await Firebase.initializeApp();
    _messaging = FirebaseMessaging.instance;
    // The function passed here will be fired when app is in background and when app is terminated
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
    FirebaseMessaging.onMessage.listen((event) {
    //...
    //...
    // This is fired when app is in foreground
    });
  }

Only thing to keep in mind is that the function passed for firing upon background has to be an anonymous function , that is it should be outside any class.唯一要记住的是,通过后台触发的 function必须是匿名的 function ,也就是说它应该在任何 class 之外。 The function will be executed in a separate isolate, so you can perform any kind of non tasks like network call, show a notification, update database etc (anything that does not involve UI). function 将在单独的隔离中执行,因此您可以执行任何类型的非任务,如网络调用、显示通知、更新数据库等(任何不涉及 UI 的事情)。 You can find more documentation here您可以在此处找到更多文档

EDIT- I may have misread the question a bit.编辑-我可能有点误读了这个问题。 The answer above is for times when you want to handle something when a notification is received.上面的答案适用于您想要在收到通知时处理某些事情的时候。 As for the notification, unless it is a data notification, a notification should pop up on its own.至于通知,除非是数据通知,否则应该会自行弹出通知。 You can generate your own notification using something like flutter_local_notifications in the callback itself as well!您也可以在回调本身中使用类似flutter_local_notifications的东西生成自己的通知!

I recommend you to use OneSignal for push notifications.我建议您使用 OneSignal 进行推送通知。 It is more stable and always receive notifications.它更稳定并且总是收到通知。 (Even when the app is killed). (即使应用程序被杀死)。

If you getting notification while in foreground and background state and not in terminated state it's may not only because of your code.如果您在处于前台和后台状态而不是处于终止状态时收到通知,这可能不仅仅是因为您的代码。 If you are running in debug mode then try totally terminate the application and launch without through IDE, launch from mobile/simulator.如果您在调试模式下运行,请尝试完全终止应用程序并在不通过 IDE 的情况下启动,从移动设备/模拟器启动。 Android OS will restrict background activity while debugging this may cause to avoid the notification from firebase. Android 操作系统将在调试时限制后台活动,这可能会导致避免来自 firebase 的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序被终止时,设备未收到推送通知 - push notification not received to devices when app is killed Flutter FCM 推送通知在用户点击通知时显示空白屏幕 state - Flutter FCM push notification show the blank screen when user tap on Notification in killed state Flutter iOS 应用程序在前台时的 FCM 推送通知 - Flutter FCM push notification for iOS when app is in foreground 本机反应-应用程序被杀死/未启动时未收到通知 - react native - not receiving notification when app is killed/not started FCM iOS 推送通知在应用程序终止时捕获自定义数据? - FCM iOS push notification capture custom data when app is terminated? 我如何仅在应用程序终止时接收 FCM 通知,如果应用程序在后台运行则不接收它 - How I can receive FCM notification only when app is terminated and doesn't receive it if app is working in background 如何从 web 向应用程序发送 FCM 通知 - How to send FCM notification to app from web 在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递 - Sending FCM Push Notification in Kotlin Client App - Firebase Cloud Messaging 设备休眠时 FCM 推送通知重新唤醒应用程序无效 - FCM push notification to reawaken app not effective while device slept 后台通知 OnClick With FCM Flutter app open an empty activity - Background Notification OnClick With FCM Flutter app open up an empty activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM