-1

my below code only sends local notifications when foreground. how can i show local notification of both "foreground and background" mode? can anyone provide a code ? thank you o.k

@main
struct ozApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate


    var body: some Scene {
        WindowGroup {
            ContentView().onAppear {   self.send()  }
            
        }
    }

func send(){
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
            if success {
                print("All set!")
            } else if let error = error {
                print(error.localizedDescription)
            }
        }
         
        
        var content = UNMutableNotificationContent()
        content.title = "Message1"
        content.subtitle = "subtitle"
        content.body = "body"
        content.sound = UNNotificationSound.default
        var request = UNNotificationRequest(identifier: UUID().uuidString , content: content, trigger: nil)
        UNUserNotificationCenter.current().add(request)

and i also have this section


class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        // Show local notification in foreground
        UNUserNotificationCenter.current().delegate = self
        
        return true
    }
}
// Conform to UNUserNotificationCenterDelegate to show local notification in foreground
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .badge, .sound])
    }
}

ozk
  • 1
  • 2

1 Answers1

1

I think you miss the trigger, the below code works fine

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
var content = UNMutableNotificationContent()
 content.title = "Message1"
         content.subtitle = "subtitle"
         content.body = "body"
         content.sound = UNNotificationSound.default
         var request = UNNotificationRequest(identifier: UUID().uuidString , content: content, trigger: trigger)
         UNUserNotificationCenter.current().add(request)

enter image description here

Mahmoud Eissa
  • 167
  • 1
  • 6
  • it only triggers when the app is foreground..... you can add this and during the 10 seconds put the app in the background and see it docent trigger --- code::::: let secondsToDelay = 10.0 DispatchQueue.main.asyncAfter(deadline: .now() + secondsToDelay) request = UNNotificationRequest(identifier: UUID().uuidString , content: content, trigger: nil) UNUserNotificationCenter.current().add(request) – ozk May 05 '22 at 20:56
  • I tried it on background too, it work fine but without async after just set the trigger time interval . – Mahmoud Eissa May 05 '22 at 21:46
  • just set the trigger time interval .... do i have to set it for it to work ? when its nil it fires imm. – ozk May 05 '22 at 22:14
  • ??? didn't get an answers from you – ozk May 18 '22 at 21:03
  • DispatchQueue.main.asyncAfter(deadline: .now() + gSecondsToDelay) { self.executeRepeatedly() } – ozk May 18 '22 at 21:17