This Project serves the purpose of guiding, how to use firebase notification in flutter for [android] and [ios].
To begin add dependencies in [pubspec.yaml], reference available in project yaml.
- Check minSdkVersion supported by [firebase core] version you are using.
- Firebase core supports minSdkVersion 19.
- Replace example in package name with [company/firm] name as example will cause problem later.
- Update/Change [applicationId] to provide unique name to your project.
Go to firebase console create a [project] on console to begin with firebase notification. Once the project is ready in firebase console let's being.
- Select android and register the app.
- Download the [google-service.json] and move to next step, add the downloaded file in [android/app] folder.
- Copy the dependencies and paste it in Root-level (project-level) Gradle file (/build.gradle)/ [android/build.gradle].
- Next Step is to add some dependencies in Module (app-level) Gradle file (//build.gradle): [android/app/build.gradle].
- finish the console setup.
with above points we have successfully completed the [android] part.
- Open Podfile located in [ios] folder and uncomment [platform :ios, '11.0'] make sure the version is set to 11.
- Go to Info.plist which is located in [ios/Runner],
- Search for [CFBundleIdentifier].
- Copy android package name and paste it in between [] tag.
Make sure to use same bundle as it'll be difficult to manage 1 project with different package name. Right click on [ios] folder and select open in finder -> open ios folder and then [Runner.xcworkspace] in [xcode]
- Select ios and register app.
- Download the [google-service.json] and move to next step, add [google-service.json] inside [Runner] folder.
- Open [Runner] switch tab to [Signing & Capabilities], inside Signing make sure the [Bundle Identifier] is same as Android Package.
- Disable Automatically manage signing.
- Now, Switch back to general and check [Bundle Identifier] and check if it is similar to the previous [Bundle Identifier].
- Come back to [your code Editor] eg [Android studio/vscode].
- Open [Terminal] and navigate to [IOS] folder. Type [cd ios] and hit Enter.
- As we did changes in [IOS] we have to update our Podfile, Type [pod update] in terminal and hit Enter.
- Go to [main.dart]
- Inside void main() function we have to bind our widget first before initializing anything. we can bind our widget by calling [WidgetsFlutterBinding.ensureInitialized()].
- Next is we will initialize our firebase by calling an async function [Firebase.initializeApp()].
- We will separate our notification code by using [NotificationService] class.
- Follow the [NotificationService] class for detail guidance.
- Created instance of [NotificationService] class in Homepage.dart.
- Add Meta in Android [Manifest] file.
- We have to Initialize [flutter_local_notification] plugin, this plugin will take care of user interface of notification.
- Create [inAppNotificationInit] method which will read the notification and update UI. (This is a Stream).
- To add support for background notification, simply copy and paste entry point code available in main.dart.
- copy and paste [GoogleService-Info.plist] which we'll get from [Firebase].
- open [AppDelegate.swift] and import [FirebaseCore].
- Initialise config inside [Bool{}] by calling [Firebase.configure()] and done.
- Now open [IOS] app setup in console scroll below in [cloud messaging] tab, notice it's asking of APNs keys and certificates, we have to create these certificate.
- Open [keychain] in mac -> click on [Keychain Access] in toolbar -> [Certificate Assistant] -> [Request a certificate From a Certificate Authority].
- Enter details [email must match with the app console email id] common name [name of your app] and select [saved to disk] and continue.
- Open [developer.apple.com/account/resources/certificates/list] after login into your dev account.
- click on plus icon near certificate text.
- select [Apple Push Notification service SSL (sandbox & production)], scroll up and click on continue.
- select [App ID:] from the dropdown, to find specific app remember your app bundle name, and click continue.
- On this screen add the keychain certificate which we created. once selected click continue.
- Download the certificate, open in finder and double click on it.
- Open Keychain and go to certificate tab, open dropdown and select certificate and private key. [see the file type in kind tab].
- right click or double click on it and select [Export 2 items...], this will generate [.p12] file and save it. [Always Remember password as it'll be ask at the time of uploading certificate]
- Open firebase console -> cloud messaging -> scroll all the way to ios app inside [Apn certificate].
- Click on [upload] where hint text say [No development APNs certificate] and upload [.p12] file here.
- open ios project in xcode -> select runner -> click on [+ Capability] search and add [Push Notifications].