我有一个 Flutter 应用程序,该应用程序已经发布几年了,并且一直在使用 Firebase Cloud Messaging,没有出现任何问题。 Google 正在从 Cloud Messaging API(旧版)转向 Firebase Cloud Messaging API (V1),我一直在尝试按照迁移文档进行设置。
我已成功为 Android 配置它,但在 iOS 上,我在设备上测试时收到以下消息::
[ERROR:flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.mm(42)] Using the Impeller rendering backend.
10.22.0 - [FirebaseInstallations][I-FIS002003] Firebase Installation registration failed for app with name: __FIRAPP_DEFAULT, error:
The server responded with an error:
- URL: https://firebaseinstallations.googleapis.com/v1/projects/*****-*****/installations/
- HTTP status code: 403
- Response body: {
"error": {
"code": 403,
"message": "Requests to this API firebaseinstallations.googleapis.com method google.firebase.installations.v1.FirebaseInstallationsService.CreateInstallation are blocked.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "API_KEY_SERVICE_BLOCKED",
"domain": "googleapis.com",
"metadata": {
"service": "firebaseinstallations.googleapis.com",
"consumer": "projects/************"
}
}
]
}
}
Please make sure you use valid GoogleService-Info.plist
FLTFirebaseMessaging: An error occurred while calling method Messaging#getToken, errorOrNil => {
NSLocalizedFailureReason = "Too many server requests.";
}
(我已经替换了上面的项目名称和项目 ID,但为了充分披露,正确的 URL 格式为“appname-1234”,元数据使用者的格式为“projects/1234567890”。)
flutter doctor -v
给出以下输出:
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.3.1 23D60 darwin-x64, locale en-GB)
• Flutter version 3.19.5 on channel stable at /Volumes/Hathaway/Programming/Cross platform dev/Flutter/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 300451adae (7 days ago), 2024-03-27 21:54:07 -0500
• Engine revision e76c956498
• Dart version 3.3.3
• DevTools version 2.31.1
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Volumes/Hathaway/Programming/Android/sdk
• Platform android-33, build-tools 30.0.3
• ANDROID_HOME = /Volumes/Hathaway/Programming/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.15.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
[✓] VS Code (version 1.87.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.86.0
[✓] Network resources
• All expected network resources are available.
• No issues found!
pubspec.yaml
的相关部分:
environment:
sdk: '>=3.3.3 <4.0.0'
dependencies:
flutter:
sdk: flutter
transparent_image: ^2.0.1
url_launcher: ^6.2.5
flutter_native_splash: ^2.4.0
shared_preferences: ^2.2.2
path_provider: ^2.1.2
#images_picker: ^1.2.4
image_picker: ^1.0.7
sqflite: ^2.3.2
http: ^1.2.1
fluttertoast: ^8.2.4
dio: ^5.4.2+1
flutter_app_badger: ^1.5.0
badges: ^3.1.2
firebase_core: ^2.28.0
firebase_messaging: ^14.8.0
overlay_support: ^2.1.0
flutter_image_compress: ^2.2.0
flutter_launcher_icons: ^0.13.1
geocoding: ^3.0.0
geolocator: ^11.0.0
google_maps_flutter: ^2.6.0
gestures: ^1.0.0
flutter_markdown: ^0.6.22
advance_pdf_viewer_fork: any
easy_image_viewer: ^1.4.1
flutter_animate: ^4.5.0
main.dart
的相关部分:
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'firebase_options.dart';
import 'package:overlay_support/overlay_support.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// I've tried with and without the following line. This line results in the 'Too many server requests' error shown above and throws an unknown error in the debugger
String? token = await FirebaseMessaging.instance.getToken();
runApp(const MyApp());
}
我已运行
flutterfire configure
并在出现提示时选择我的项目以及 Android 和 iOS。它会生成 firebase_options.dart
没有任何文档提到在 Firebase Console 中对权限执行任何操作,但是在 Stack Overflow 上的其他地方我看到了添加权限的参考,如下所示:
我注意到在 API 和服务/凭证中添加了一个新的 Android 密钥,大概是在我运行
flutterfire configure
时,但是我的旧 iOS 密钥仍然存在(并且使用 AppDelegate.swift
中的正确密钥正确引用了该密钥)但并没有添加新的key。也许这就是问题所在?
如果我将
String? token = await FirebaseMessaging.instance.getToken();
添加到 main.dart
,此时我会收到未知错误,但是由于 Firebase 尚未初始化,我并不完全感到惊讶,我会在链的下游出现错误。在 Android 上运行此程序,我会得到一个令牌。
我尝试过以下方法:
GoogleService-Info.plist
(根本没变,不过以防万一我重新复制了)flutter create
从头开始设置项目,将文件复制到 lib 并按照 https://firebase.google.com/docs/flutter/setup 从头开始既然 Android 可以运行,那么 iOS 中也一定可以运行。我在 XCode 中设置了必要的功能(推送通知、后台和远程均已勾选)。
我错过了什么?!
https://stackoverflow.com/a/78257462/22646405
查看这个答案,它包含ios必要的代码,包括在ios中请求权限。
您还需要执行一些 ios 配置步骤,例如在 firebase 设置中添加 APNs 身份验证密钥。 (不确定你有没有)