skip to Main Content

I have a Flutter app that has been out for a couple of years and has been using Firebase Cloud Messaging without any problems. Google are moving away from Cloud Messaging API (Legacy) to Firebase Cloud Messaging API (V1) and I’ve been trying to set it up following the documentation to migrate.

I’ve successfully configured it for Android, but on iOS I get the following message when testing on a device::

[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.";
}

(I’ve replaced my project name and project ID from the above, but for full disclosure that is correct with the URL in format "appname-1234" and the metadata consumer in the format "projects/1234567890".)

flutter doctor -v gives the following output:

[✓] 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!

Relevant parts of 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

The relevant part of 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());
}

I’ve run flutterfire configure and chosen my project and Android and iOS when prompted. It generates firebase_options.dart

None of the documentation mentions doing anything in Firebase Console to permissions, however elsewhere on Stack Overflow I’ve seen references to adding permissions as follows:

  • In Google Cloud Console go to APIs & Services
  • Click Credentials
  • Add key permissions for Firebase Installations API, Firebase Cloud Messaging API and FCM Registration API

I noticed in APIs and Services/Credentials that a new Android key has been added, presumably when I ran flutterfire configure, however my old iOS key is still there from before (and this is correctly referenced with the right key in AppDelegate.swift but it hasn’t added a new key. Perhaps this is where there’s a problem?

If I add String? token = await FirebaseMessaging.instance.getToken(); to main.dart I get an unknown error at that point, however as Firebase hasn’t initialised I’m not entirely surprised I get errors further down the chain. On Android this runs and I get a token back.

I’ve tried the following:

  • flutter clean
  • Recopied GoogleService-Info.plist (it hadn’t changed at all, but I re-copied just in case)
  • Remove all restrictions from the iOS Google Cloud Key in APIs & Services (now reinstated)
  • Set up project from scratch with flutter create, copy files into lib and follow the Firebase set up instructions at https://firebase.google.com/docs/flutter/setup from scratch

As Android is working it must be something in iOS. I have the necessary capabilities set in XCode (Push Notifications, Background and remote both ticked).

What am I missing?!

2

Answers


  1. Chosen as BEST ANSWER

    I've managed to resolve this. Thanks to @Ultranmus for their help. The linked answer has a useful class that others who read this may like to view.

    For me, I was kind of on the right track. I suspected that the problem was with the keys in APIs & Services in Google Cloud Console. Running flutterfire configure had indeed generated a new key for Android as I thought. I couldn't see a new key for iOS, but what had happened is that FlutterFire had found an old key (even older than the one I'd used in my previous code for the legacy FCM) and used that in firebase_options.dart.

    When I hunted for the key used in firebase_options.dart in APIs & Services and located it, then added the 3 required permissions (Firebase Cloud Messaging API, Firebase Installations API and FCM Registration API) everything worked - APNS token came back followed by the device token. Happy days!

    So the fix is to make sure you have the permissions set up for your keys (as described above) checking that the keys you're updating are the ones referenced in firebase_options.dart. Then make use of the class mentioned in the answer above.


  2. https://stackoverflow.com/a/78257462/22646405

    Check out this answer, it contains necessary code for ios, including requesting permission in ios.

    You need to also do few ios config steps like adding your APNs auth key in firebase settings. (Not sure if you did)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search