我已将Firebase Cloud Messaging配置为带有flutter的通知正在前台运行。但在后台运行或应用被杀死时无法正常工作。

完成以下步骤。

  • 生成的应用程序ID并按入
    https://developer.apple.com/account/resources/certificates/
  • 将info.plist添加到ios/Runner文件夹
  • 启用了推功能
  • 将APNS证书上载到Firebase控制台
  • 在AppDelegate.swift中添加了行

  • 还尝试按照Flutter Firebase Cloud Messaging - Notification when app in background上的建议删除以下行,但仍无法正常工作。

    如果(@available(iOS 10.0,*)){
    [UNUserNotificationCenter currentNotificationCenter] .delegate =(id)自我;
    }
    Flutter Doctor
    
    [✓] Flutter (Channel beta, v1.12.13+hotfix.6, on Mac OS X 10.14.5 18F132, locale en-IN)
    
    [✗] Android toolchain - develop for Android devices
    
        ✗ Unable to locate Android SDK.
    
          Install Android Studio from: https://developer.android.com/studio/index.html
    
          On first launch it will assist you in installing the Android SDK components.
    
          (or visit https://flutter.dev/setup/#android-setup for detailed instructions).
    
          If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.
    
          You may also want to add it to your PATH environment variable.
    
    
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.3)
    
    [✓] Chrome - develop for the web
    
    [!] Android Studio (not installed)
    
    [✓] Connected device (3 available)
    

    我注意到的一件事是,在首次安装应用程序时,并没有要求我检查是否允许该应用程序发送推送通知。

    我的代码在登录页面之后的页面上有以下几行。
    _firebaseMessaging.requestNotificationPermissions(
            const IosNotificationSettings(sound: true, badge: true, alert: true));
        _firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings settings) {
          print("Settings registered: $settings");
        });
    

    并交叉检查是否已勾选所有以下设置。

    ios - 当应用程序在后台运行或关闭时,Flutter Firebase Messaging无法在IOS上运行-LMLPHP

    最佳答案

    我的解决方案如下,它的工作原理。

    publspec.yaml
    firebase_messaging:^ 6.0.9

    从ios/runner/AppDelegate.m或ios/runner/AppDelegate.swift中删除以下行。

    如果(@available(iOS 10.0,*)){
    [UNUserNotificationCenter currentNotificationCenter] .delegate =(id)自我;
    }

    在Dart代码中使用临时== false,应弹出以在通知时进行确认。

    myFirebaseMessagingService.requestNotificationPermissions(
    const IosNotificationSettings(
    声音:true,徽章:true,警报:true,临时:false));

    在Xcode之后的Push通知以及后台Fetch和远程通知中。

    ios - 当应用程序在后台运行或关闭时,Flutter Firebase Messaging无法在IOS上运行-LMLPHP

    10-05 20:48
    查看更多