Home > database >  I cannot implement Firebase Notifications into Flutter on Ios
I cannot implement Firebase Notifications into Flutter on Ios

Time:12-01

I did exactly the same things in google's documentation but it does not work. I faced with "No such module 'Flutter'" Here is the code example:

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

And also, I find something. You can see from below. It is kind of working but not properly working. While it was opening, it wanted my permission for notifications. But it gives me a black screen. I cannot see anything. Here are the code parts:

import UIKit
import FirebaseCore
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions:
  [UIApplication.LaunchOptionsKey: Any]?
 ) -> Bool {
  // Firebase
  FirebaseApp.configure()
  // Push Notification
  UNUserNotificationCenter.current().delegate = self
   let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
   UNUserNotificationCenter.current().requestAuthorization(
    options: authOptions,
    completionHandler: { _, _ in }
   )
   application.registerForRemoteNotifications()
  return true
 }
}
extension AppDelegate: UNUserNotificationCenterDelegate {}

According to an idea: This is my AppDelegate.swift file. I'm trying to apply your response.

import UserNotifications
import Firebase
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
 _ application: UIApplication,
 didFinishLaunchingWithOptions launchOptions:
 [UIApplication.LaunchOptionsKey: Any]?
 ) -> Bool {
 // Firebase
 FirebaseApp.configure()
 // Push Notification
 UNUserNotificationCenter.current().delegate = self
 registerForPushNotifications() // Function created below
 return true
 }
  func registerForPushNotifications() {
    UNUserNotificationCenter.current()
      .requestAuthorization(
        options: [.alert, .sound, .badge]) { [weak self] granted, _ in
        print("Permission granted: \(granted)")
        guard granted else { return }
        self?.getNotificationSettings()
      }
  }
  // MARK:- Get Notification
  func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { settings in
      print("Notification settings: \(settings)")
      guard settings.authorizationStatus == .authorized else { return }
      DispatchQueue.main.async {
        UIApplication.shared.registerForRemoteNotifications()
      }
     }
    }
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
  completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfo = response.notification.request.content.userInfo
  print(userInfo)
  completionHandler()
 }
}

This is the output that I mentioned before: The black screen seems

CodePudding user response:

Install a pod :- pod 'FirebaseMessaging'

-> goto AppDelegate:

import UserNotifications
import Firebase
import FirebaseMessaging

-> on didFinishLaunchingWithOptions

UNUserNotificationCenter.current().delegate = self
registerForPushNotifications() // Function created below

-> Create functions to accept Notification permission

func registerForPushNotifications() {
    
    UNUserNotificationCenter.current()
        .requestAuthorization(
            options: [.alert, .sound, .badge]) { [weak self] granted, _ in
            print("Permission granted: \(granted)")
            guard granted else { return }
            self?.getNotificationSettings()
        }
}

// MARK:- Get Notification
func getNotificationSettings() {
    
    UNUserNotificationCenter.current().getNotificationSettings { settings in
        print("Notification settings: \(settings)")
        
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
        
     }
   }

-> delegate methods

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
    let userInfo = response.notification.request.content.userInfo
    print(userInfo)
    
    completionHandler()
 }
}

-> To get Device Token

 func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
    let tokenParts = deviceToken.map { data in String(format: ".2hhx", data) }
    let token = tokenParts.joined()
    Logger.log(logType: .info, object: token)
}

CodePudding user response:

There is no problem with AppDelegate.swift file. I need to change main.dart file. No need to change anything more.

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
  • Related