Wednesday, June 26, 2024
HomeiOS Developmentios - Native Notification would not work when App is launched from...

ios – Native Notification would not work when App is launched from Xcode, however works when cellphone is restarted and App is launched domestically


I wrote a easy app which has the purpose of triggering a neighborhood notification (banner fashion, like a push notification) when the consumer clicks a button.

import SwiftUI
import UserNotifications

struct ContentView: View {
    var physique: some View {
        VStack {
            Button(motion: {
                sendNotification()
            }) {
                Textual content("Ship Notification")
                    .padding()
                    .background(Coloration.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
            }
        }
        .onAppear {
            requestNotificationPermission()
        }
    }
}

func requestNotificationPermission() {
    let middle = UNUserNotificationCenter.present()
    middle.requestAuthorization(choices: [.alert, .sound]) { granted, error in
        if let error = error {
            print("Notification permission error: (error.localizedDescription)")
        }
    }
}

func sendNotification() {
    let content material = UNMutableNotificationContent()
    content material.title = "Good day!"
    content material.physique = "It is a notification out of your app."
    content material.sound = UNNotificationSound.default

    let set off = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
    let request = UNNotificationRequest(identifier: UUID().uuidString, content material: content material, set off: set off)

    UNUserNotificationCenter.present().add(request) { error in
        if let error = error {
            print("Notification error: (error.localizedDescription)")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

After I construct and run my app from Xcode and the press the button, nothing occurs. Even when I shut the app and restart it domestically, nothing occurs once more… HOWEVER, if I restart the cellphone utterly after which launch the app, the button works as meant and the native notification seems!

That is extraordinarily aggravating to my growth course of, does anybody know why the app is not working after I launch it from Xcode? FWIW it would not work AT ALL on the simulator, even when I restart the simulator and launch it “domestically” like I do on my actual machine.

My actual machine is an iPhone XS operating iOS 18 DB1, the simulator is an iPhone 15 operating iOS 17.5



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments