관리 메뉴

꿀맛코딩

Flutter module local notification foreground push in ios not working 본문

공부방/Flutter

Flutter module local notification foreground push in ios not working

soycrab 2023. 8. 29. 17:32

Flutter module을 가지고 이것 저것 테스트 하는데 이상한 현상이 있어서 기록해요. 

Flutter module에서 Local notification 기능을 넣고, 그 상태로 iOS에서 로컬 푸시를 보내면 Foreground background 아주 잘 오는데 이상하게 이 module을 다시 native iOS에 넣어서 테스트를 하면 foreground 에서는 푸시가 오지 않는 현상이 있습니다. 

이것을 해결하기위해 아래 처럼 코드를 수정을 하면 정상 동작을 확인할수 있습니다. 

 

 

//
//  moduletestApp.swift
//  moduletest


import SwiftUI
import Flutter
 // The following library connects plugins with iOS platform code to this app.
import FlutterPluginRegistrant

 class FlutterDependencies: ObservableObject {
   let flutterEngine = FlutterEngine(name: "my flutter engine")
   init(){
     // Runs the default Dart entrypoint with a default Flutter route.
     flutterEngine.run()
     // Connects plugins with iOS platform code to this app.
     GeneratedPluginRegistrant.register(with: self.flutterEngine);
       
   }
     
}



@main
struct moduletestApp: App {
    
//    init() {
//       if #available(iOS 10.0, *) {
//        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
//       }
//     }
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    @StateObject var flutterDependencies = FlutterDependencies()
         var body: some Scene {
           WindowGroup {
             ContentView().environmentObject(flutterDependencies)
           }
         }
    
}



class AppDelegate: NSObject, UIApplicationDelegate {
    
    // 앱이 켜졌을때
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        
        // 원격 알림 등록
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: { _, _ in }
            )
        } else {
            let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        
        return true
    }
    
}


extension AppDelegate : UNUserNotificationCenterDelegate {
    
    // 푸시메세지가 앱이 켜져 있을때 나올때
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        let userInfo = notification.request.content.userInfo
        
        print("willPresent: userInfo: ", userInfo)
        
        completionHandler([.banner, .sound, .badge])
    }
    
    // 푸시메세지를 받았을 때
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        print("didReceive: userInfo: ", userInfo)
        completionHandler()
    }
    
}
반응형
Comments