简体   繁体   English

应用程序终止时的 FCM 推送通知 FLUTTER

[英]FCM push notifications FLUTTER when app is terminated

I am trying to receive push notifications on ANDROID from the Firebase console when the app is terminated but I get nothing (I have onResume, onMessage and onLaunch callback listeners but I don't even want to handle them in the app yet) I'd just like to get them to show In the tray or lock screen.当应用程序终止时,我试图从 Firebase 控制台接收有关 ANDROID 的推送通知,但我什么也没得到(我有 onResume、onMessage 和 onLaunch 回调侦听器,但我什至不想在应用程序中处理它们)就像让它们显示在托盘或锁定屏幕中。 Is there some Android configuration required?是否需要一些 Android 配置?

first answer, I hope will be good.第一个答案,我希望会很好。

As you can seehere notifications are handled with onBackgroundMessage even if the app is terminated.正如您在此处看到的,即使应用程序终止,通知也会使用 onBackgroundMessage 处理。 Maybe your problem is that you are not sending a Notification but a "Data only" message.也许您的问题是您没有发送通知,而是发送“仅数据”消息。 In that case you have to set the field "priority": "high"在这种情况下,您必须设置字段"priority": "high"

Also, with the latest release of Flutter and firebase_messaging the methods onResume, onMessage and onLaunch are deprecated.此外,在最新版本的 Flutter 和firebase_messaging中,方法 onResume、onMessage 和 onLaunch 已弃用。 You should substitute them with onMessage, onMessageOpened, onBackground, but you can find definitely more info with the official documentation您应该将它们替换为 onMessage、onMessageOpened、onBackground,但您可以通过官方文档找到更多信息

Just an addition to @samUser1 answer.只是对@samUser1 答案的补充。 When app is terminated this code will handle events.当应用程序终止时,此代码将处理事件。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

And outside main function parse it.在主 function 之外解析它。 The example below uses AwesomeNotifications package.下面的示例使用 AwesomeNotifications package。 https://github.com/rafaelsetragni/awesome_notifications/blob/master/example/lib/main.dart https://github.com/rafaelsetragni/awesome_notifications/blob/master/example/lib/main.dart

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  final res = await AwesomeNotifications().createNotification(
    content: NotificationContent(
      id: Random().nextInt(1000000),
      channelKey: 'channel_name',
      title: 'Hello!',
      body: 'World!',
      notificationLayout: NotificationLayout.Default,
    ),
  );
} 

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

相关问题 当App位于前台时,FCM推送通知将被替换 - FCM Push Notifications are replacing when App is in Foreground Firebase 应用程序终止时推送通知回调不起作用 - Firebase push notifications callback doesn't work when app is terminated 当应用程序处于后台状态时,FCM多个推送通知无法正常工作 - FCM Multiple push notifications not working properly when app in background state 应用程序被杀死时是否可以接收 FCM 推送通知? - Is it possible to receive FCM push notifications when app is killed? FCM 推送通知仅在应用程序处于后台时显示 - FCM Push Notifications are only displayed when app is in background 当应用程序有后台处理程序时,在 Flutter 中未收到 FCM 通知 - FCM notifications not received in Flutter when there is a background handler for the app 使用Flutter和FCM终止应用时,Android设备未收到通知消息 - Android device not receiving notification message when the app is terminated using Flutter and FCM 应用被杀死时,应用未收到FCM通知 - app not receiving FCM notifications when the app is killed Flutter – 当应用程序被杀死时,推送通知在后台不起作用 - Flutter – push notifications not working in the background when app is killed 使用FCM的Ionic 2推送通知 - Ionic 2 Push Notifications with FCM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM