簡體   English   中英

我無法在 Ios 上的 Flutter 中實現 Firebase 通知

[英]I cannot implement Firebase Notifications into Flutter on Ios

我在谷歌的文檔中做了完全相同的事情,但它不起作用。 我遇到了“沒有這樣的模塊'Flutter' ”下面是代碼示例:

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)
  }
}

而且,我發現了一些東西。 你可以從下面看到。 這是一種工作,但不能正常工作。 當它打開時,它需要我的許可才能發出通知。 但它給了我一個黑屏。 我什么都看不到。 以下是代碼部分:

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 {}

根據一個想法:這是我的AppDelegate.swift文件。 我正在嘗試應用您的回復。

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()
 }
}

這就是我之前提到的output: 黑屏好像

安裝一個 pod:- pod 'FirebaseMessaging'

-> 轉到 AppDelegate:

import UserNotifications
import Firebase
import FirebaseMessaging

-> 在 didFinishLaunchingWithOptions 上

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

-> 創建函數以接受通知權限

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()
 }
}

-> 獲取設備令牌

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

這是我的 AppDelegate.swift 文件。 我正在嘗試應用您的回復。

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()
 }
}

這就是我之前提到的output: 黑屏好像

AppDelegate.swift 文件沒有問題。 我需要更改 main.dart 文件。 無需再更改任何內容。

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)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM