I am trying to create a local notification for my app using Apple's UNUserNotificationCenter.
Here is my code:
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = task.name
content.body = task.notes
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: task.UUID, content: content, trigger: trigger)
center.add(request) { (error:Error?) in
if let theError = error {
print("Notification scheduling error: \(error)")
}else{
print("Notification was scheduled successfully")
}
}
Access Request:
let center = UNUserNotificationCenter.current()
//request notification access
let options: UNAuthorizationOptions = [.alert, .sound]
center.requestAuthorization(options: options) { (granted, error) in
if !granted {
print("Notification access was denied")
}
}
After 5 seconds the app makes the Default sound but does not show the alert content. I am trying both with the app in the foreground and in the background.
Step 1: In your SwiftUI app, create a new class called LocalNotificationManager that adopts the @ObservableObject. Adopting this protocol isn’t a must but can be useful if you want your SwiftUI view to react when a local notifications gets fired.
The Swift code for sending a local notification after 5 seconds is as follows: Basically, the above code simply instantiates a UNMutableNotificationContent, which is the content of your notification message, so we need to supply it with the title, subtitle and the sound.
The local notification is a handy way for you to send notifications right away from the app to the users without having to do it from the server side. To send local notifications in your app, all you need to do is to make use of the UserNotifications framework. First of all, you’ll need to import the UserNotifications as below:
There are two types of notifications in iOS app, the local and remote notifications. The local notification is a handy way for you to send notifications right away from the app to the users without having to do it from the server side. To send local notifications in your app, all you need to do is to make use of the UserNotifications framework.
I had the same problem.
If your content body is blank ie(content.body == "") then you won't get a notification even if you do have a title.
So the solution is to make sure that your content body is not an empty string.
Add this Protocoal UNUserNotificationCenterDelegate
.
and add this delegate into your controller. and dont forget to set delegate.
UNUserNotificationCenter.current().delegate = self
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification being triggered")
//You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
//to distinguish between notifications
if notification.request.identifier == "yourrequestid"{
completionHandler( [.alert,.sound,.badge])
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With