Pitpoints encountered when flutter integrates firebase push

Background, when I was doing this, I searched for flutter firebase integrated search results, and the configuration was completed in a few sentences. When I did it, I found that there were a lot of pitfalls. Anyone in the formal process can write, but the important pitfalls must be Everyone cares. So I wrote this article, I hope this article can help friends who have encountered many pitfalls like me.

One: Android firebase configuration

1: Go to the firebase console to apply for an account and create an application, download the google-services.json file and put it in the app/ directory of the android project

2: Configure the gradle file. One of the two gradle files is the gradle file of the project, and the other is the gradle file of the app. I will not introduce the role of gradle here. If you are interested, you can go to the official website of gradle.

Project-level build.gradle (<project>/build.gradle):
// 配置第三方插件相关
buildscript {
  // 指明下面的插件去哪里下载,这里是到google仓库和maven仓库去下载,它是按照顺序的,如果google下载到了就不会到maven仓库里面下载了
  repositories {
    // Make sure that you have the following two repositories
    google()  // Google's Maven repository

    mavenCentral()  // Maven Central repository

  }
  // 插件的依赖
  dependencies {
    ...
    // Add the dependency for the Google services Gradle plugin
    classpath 'com.google.gms:google-services:4.3.13'

  }
}

allprojects {
  ...
  repositories {
    // Make sure that you have the following two repositories
    google()  // Google's Maven repository

    mavenCentral()  // Maven Central repository

  }
}

It is worth noting that app/build.gradle, the solution given by the official website is not easy to use, the following is the official firebase given: the location of the placement is very vague, and various errors are reported in the personal test.

plugins {
  id 'com.android.application'

  // Add the Google services Gradle plugin
  id 'com.google.gms.google-services'

  ...
}

dependencies {
  // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:30.3.2')


  // TODO: Add the dependencies for Firebase products you want to use
  // When using the BoM, don't specify versions in Firebase dependencies
  implementation 'com.google.firebase:firebase-analytics'


  // Add the dependencies for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries
}

In the end, this website firebase_flutter  saved me and helped me go ashore successfully. Paste the correct configuration below

android/app/build.gradle
apply plugin: 'com.google.gms.google-services'

Just this short sentence is awesome, and then the introduction of flutter project, firebase_core and firebase_messaging

 3: Initialize firebase in the code

Firebase.initializeApp().then((value) async {
    print("firebase init over");
    String? token = await FirebaseMessaging.instance.getToken();
    print("token is $token");
    if(token != null && token.isNotEmpty) {
        // 将token上传到后端,让后端控制发送推送通知
    }
})

At this point, the integration of firebase on android is completed.

Two: Firebase configuration on the IOS side, this step is very strenuous for me, it is really one step at a time!

1: Still download the Google-Services.plist file

2: configure firebase

Let's take a look at the firebase official website first.

It was over in a few sentences, so I thought it was very simple and followed it, but the xcode report did not find it. I checked whether it was a network problem, but it was not, so I cut to the ios file and added pod install, but the error was reported as FirebaseCore Relying on GoogleUitls Yunyun, so I found a real solution to the problem on the Internet

Error:

[!] The following Swift pods cannot yet be integrated as static libraries:

The Swift pod FirebaseCoreInternal-library depends upon GoogleUtilities-library, which does not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set use_modular_headers! globally in your Podfile, or specify :modular_headers => true for particular dependencies.

Solution: Modify Podfile

 platform :ios, '12.4'
  ...
  ...
  
  pod 'Firebase', :modular_headers => true
  pod 'FirebaseCoreInternal', :modular_headers => true
  pod 'GoogleUtilities', :modular_headers => true
  #....add any library need headers

 Connection Address: Workaround

Looking at  the method of the official website on the official website-garbage

 Not to mention the worst  

:modular_headers => true

 3: IOS wants to push the certificate that needs to be configured for notification, and then download the .p8 file and upload it to the firebase configuration background to succeed, otherwise it will report apns- string not recognized, and you need to add push settings in the project location

Guess you like

Origin blog.csdn.net/lck8989/article/details/126376104