繁体   English   中英

flutter 应用程序的 FCM 通知未显示在 iOS 系统托盘中

[英]FCM Notification for flutter app does not show in iOS system tray

我有一个 flutter 应用程序,我在其中使用 Firebase 云消息传递通知。

我通过 firebase 控制台发送测试通知。

通知 function 如预期在 android 上。

在 ios 上,当应用程序处于前台时,通知按预期到达。 当应用程序在后台最小化时,通知不会显示在系统托盘中。 当我通过控制台发送时,即使在 firebase 通知中设置了徽章,也没有显示徽章。

基本上,在再次打开应用程序之前,根本没有任何通知已经到达的迹象。

我已按照此页面上的说明进行操作,并确认我遵循了 ios 集成步骤到 T。

我正在模拟器上进行测试。

根据Firebase 网站上关于苹果集成的文档:

对于 iOS; 您必须拥有物理 iOS 设备才能接收消息。 Firebase 云消息与 Apple 推送通知服务 (APN) 集成,但 APN 仅适用于真实设备。

当我没有在我的应用程序中请求通知的权限时,我遇到了一些问题。 因此,您应该检查您在执行应用程序时是否已请求通知的权限,并且它已被接受,因为如果您不接受权限,您可能会在接收通知时遇到问题。

您可以在 iOS 项目的 appDelegate 中添加此代码,以在应用执行时请求权限:

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        
        // Enable or disable features based on the authorization.
    }

或者您也可以在 pub.dev 中使用 package 来检查和请求您的应用程序中的权限。

您也可以在手机上查看是否为您的应用启用了通知。

按着这些次序:

  1. 按照 Firebase 文档中的本指南生成 Apple 接收推送通知所需的证书。 您可以跳过标题为“创建配置文件”的部分。

  2. Using the Firebase Console add an iOS app to your project: Follow the assistant, download the generated GoogleService-Info.plist file, open ios/Runner.xcworkspace with Xcode, and within Xcode place the file inside ios/Runner. 请勿按照 Firebase 助手中的“添加 Firebase SDK”和“添加初始化代码”的步骤进行操作。

  3. 在项目导航器中的 Xcode、select Runner 中。 在功能选项卡中打开推送通知和后台模式,并在后台模式下启用后台获取和远程通知。

  4. 按照 Firebase 文档的“上传您的 APNs 证书”部分中的步骤操作。

5.如果您需要禁用 FCM iOS SDK 完成的方法转换(例如,以便您可以将此插件与其他通知插件一起使用),然后将以下内容添加到您的应用程序的 Info.plist 文件中。

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

其次, Apple 不支持模拟器中的 FCM 推送通知,您必须设置物理 iOS 设备来运行您的应用程序。

我最近遇到了这个问题......尝试了很多像这些人写的东西,但唯一对我有用的是将 firebase_message 版本更改为firebase_messaging: ^8.0.0-dev.14 您应该尝试一下。与上一个稳定版本相比,有些东西有所变化,但过渡并不难。 你可以在这里查看教程。 https://firebase.flutter.dev/docs/messaging/usage/

我现在看到他们发布了最新版本(9.0.0).. 但对我来说 8.0.0 工作

*仅适用于真实设备

import UIKit
import Flutter
import UserNotifications

//import FacebookCore

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
       if #available(iOS 10.0, *) {
           UNUserNotificationCenter.current().delegate = self
       } else {
           // Fallback on earlier versions
       }

        //SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    // This method will be called when app received push notifications in foreground
    
    @available(iOS 10.0, *)
    override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler([.alert, .badge, .sound])
    }

}

在您的info.plist文件中进行此替换。

<false/>替换为<string>0</string>

前:

在此处输入图像描述

现在:

在此处输入图像描述

信息:如果您的info.plist中没有此代码并且您也没有收到通知,请确保添加它。

别忘了,这是新代码:

<key>FirebaseAppDelegateProxyEnabled</key>
<string>0</string>

Info: IOS不支持模拟器中的 FCM 推送通知,即使您在XCode中使用模拟器也是如此。 最好的解决方案是使用物理设备

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM