首页 > 解决方案 > Flutter IOS 通知的 FCM 在应用程序处于后台或终止时不显示

问题描述

我使用这个为 Flutter 实现了 Firebase Cloud Messaging 。Android 中的所有东西都运行良好,但 iOS 推送通知仅在应用程序处于前台时显示。

请帮助我在后台显示通知应用程序并终止。

这些是我到目前为止所做的步骤

  1. 创建了 firbase 项目。
  2. 将 GoogleService-Info.plist 添加到 xcode。
  3. 将 firebase_messaging: ^6.0.13 & flutter_local_notifications: ^1.3.0 添加到 pubspec.yaml 中。
  4. 然后我启用推送通知和后台模式

  5. 扑通扑通

    FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    new FlutterLocalNotificationsPlugin();
    
    
    
    @override
    void initState() {
    super.initState();
    var android = new 
    AndroidInitializationSettings('mipmap/ic_launcher');
    var ios = new IOSInitializationSettings();
    var platform = new InitializationSettings(android, ios);
    flutterLocalNotificationsPlugin.initialize(platform);
    
    firebaseMessaging.configure(
    onLaunch: (Map<String, dynamic> msg) async{
    print(" onLaunch called ${(msg)}");
    },
    
    onResume: (Map<String, dynamic> msg) async{
    print(" onResume called ${(msg)}");
    },
    onMessage: (Map<String, dynamic> msg) async{
    showNotification(msg);
    print(" onMessage called ${(msg)}");
    },
    );
    firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, alert: true, badge: 
    true));
    firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings setting) {
    print('IOS Setting Registed');
    });
    firebaseMessaging.getToken().then((token) {
    update(token);
    });
    getCashedInfo();
    }
    
    
    
    showNotification(Map<String, dynamic> msg) async {
    var android = new AndroidNotificationDetails(
    'sdffds dsffds',
    "CHANNLE NAME",
    "channelDescription",
    );
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
     0, "This is title", "this is demo", platform);
    }
    
    update(String token) {
    print(token);
    
    }
    
  6. 在 Xcode 我添加

    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? 
    UNUserNotificationCenterDelegate
    }
    

标签: flutterfirebase-cloud-messaging

解决方案


发布此问题后,iOS 的后台消息处理功能以前不受支持。但是这篇GitHub 帖子中提到了一些解决方法。

使用通知服务扩展是一种方法,是的。

Flutter 是一个优秀的框架,它允许以惊人的速度构建和发布漂亮的 UI。然而,这是否意味着它提供了我们想要的一切(从我们的角度来看)?不。这是否意味着我们放弃了 Flutter 并使用 SwiftUI(错误)或故事板?不。

我们决定将 Flutter 用于 UI,我们正在处理几乎所有的业务逻辑,除了推送通知和其他一些次要功能 - 这是你最好自己滚动的地方。

实现通知服务扩展并使其与 Flutter 一起工作只需几个小时,考虑到有多少失败,这有点有趣,但考虑到入门级,这并不令人震惊。

几个月来,我们确实使用了带有通知内容扩展的通知服务扩展来获得更高级的通知,但最后我们放弃了它并转移到 PushKit(显然带有 CallKit)并推出了我们自己的通知呈现器逻辑,这导致我们放弃了通知服务和内容扩展.

我对那些需要为 Flutter 添加推送通知支持的人的建议 - 在原生端解决它,稍后你会感谢自己的。

将 NotificationServiceExtension(本机代码)与 firebase_messaging 一起使用如何?

在同一个 GitHub 帖子上,特别是在2020 年 8 月 4 日的这个帖子中。有人提到现在支持这个功能。

大家好,当前的开发版本现在支持这一点,同时也支持 macOS 平台支持登陆(请参阅迁移指南文档以获取完整的变更日志以及如何升级)。

有关试用此开发版本的讨论/反馈,请参阅 #4023 - 希望得到反馈。


推荐阅读