简体   繁体   中英

xcode 13 - trying to trigger a local Notification every 30 sec using a public function

Im trying to trigger a local Notification every 30 sec but I'm getting an error "Type '()' cannot conform to 'View'" I just opened a new project on Xcode 13 where it says hello world and wanted to trigger a local Notification using the below A() functions after the "hello world" ContentView() function

what am i doing wrong? is there a better approach?

thank you +++++++++++++++++

import SwiftUI
import UserNotifications

@main
struct ozApp: App {
    var body: some Scene {
        WindowGroup {    /// error /// Type '()' cannot conform to 'View'            
            ContentView()
            A()
        }
    }
}

public func A(){
    // Swift
    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Body"
    content.sound = UNNotificationSound.default
                   
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    print("INSIDE NOTIFICATION")
    
   // let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)
    //print("INSIDE NOTIFICATION")
                    
    UNUserNotificationCenter.current().add(request, withCompletionHandler: {(error) in
        if let error = error {
            print("SOMETHING WENT WRONG")
        }
    })
    

}

try this:

@main
struct ozApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
              .onAppear { A() }
        }
    }
}

and the time interval must be at least 60 if repeating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM