SwiftUI在没有按钮的情况下询问通知权限

Posted

技术标签:

【中文标题】SwiftUI在没有按钮的情况下询问通知权限【英文标题】:SwiftUI ask notification permission on start without button 【发布时间】:2021-06-30 09:21:52 【问题描述】:

我想在启动时向用户请求通知权限,而无需用户按下特定按钮。我如何在 Xcode 12.5 中做到这一点?

这是我的 App 结构:

import SwiftUI
import UserNotifications

@main
struct KiwiApp: App 
    let persistenceController = PersistenceController.shared
    let center = UNUserNotificationCenter.current()

    var body: some Scene 
        WindowGroup 
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        
    

现在我应该在哪个地方添加此代码以请求许可?

center.requestAuthorization(options: [.alert, .sound, .badge])  granted, error in
    
    if let error = error 
        // Handle the error here.
    
    // Enable or disable features based on the authorization.

(来自https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications)

我知道我可以将它添加为按钮的函数调用,但我想请求访问而无需用户按下任何东西,所以基本上是在初始化视图时。还是我需要在ContenView.swift 的某个地方这样做?

编辑:

简短回答:将请求代码放在 App Struct 的 init() 函数中。

struct KiwiApp: App 
    let persistenceController = PersistenceController.shared
    let center = UNUserNotificationCenter.current()
    
    init() 
        center.requestAuthorization(options: [.sound , .alert , .badge ], completionHandler:  (granted, error) in
            if let error = error 
                // Handle the error here.
            
            // Enable or disable features based on the authorization.
        )
    

    var body: some Scene 
        WindowGroup 
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        
    
```

【问题讨论】:

【参考方案1】:

使用UIApplicationDelegateAdaptor

@main
struct KiwiApp: App 
    let persistenceController = PersistenceController.shared
    let center = UNUserNotificationCenter.current()
    
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene 
        WindowGroup 
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        
    


class AppDelegate: NSObject, UIApplicationDelegate 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool 
        
        registerForNotification()
        return true
    
    
    
    func registerForNotification() 
        //For device token and push notifications.
        UIApplication.shared.registerForRemoteNotifications()
        
        let center : UNUserNotificationCenter = UNUserNotificationCenter.current()
        //        center.delegate = self
        
        center.requestAuthorization(options: [.sound , .alert , .badge ], completionHandler:  (granted, error) in
            if ((error != nil))  UIApplication.shared.registerForRemoteNotifications() 
            else 
                
            
        )
    


或使用init

@main
struct KiwiApp: App 
    let persistenceController = PersistenceController.shared
    let center = UNUserNotificationCenter.current()
    
    init() 
        registerForNotification()
    
    
    func registerForNotification() 
        //For device token and push notifications.
        UIApplication.shared.registerForRemoteNotifications()
        
        let center : UNUserNotificationCenter = UNUserNotificationCenter.current()
        //        center.delegate = self
        
        center.requestAuthorization(options: [.sound , .alert , .badge ], completionHandler:  (granted, error) in
            if ((error != nil))  UIApplication.shared.registerForRemoteNotifications() 
            else 
                
            
        )
    
    var body: some Scene 
        WindowGroup 
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        
    

【讨论】:

以上是关于SwiftUI在没有按钮的情况下询问通知权限的主要内容,如果未能解决你的问题,请参考以下文章

Firebase iOS 11 在按钮按下后要求用户通知

检测在 SwiftUI 中点击的系统警报按钮

如何在不使用swiftUI附带的幻灯片删除的情况下创建自定义删除按钮我没有使用列表,只是使用foreach循环

如何在没有权限通知的情况下自动发送短信?

我可以在没有通知权限的情况下使用 FCM 接收消息吗?

如何在没有 GUI 通知的情况下保存 excel 文件?