Local Notification on MacOS is not working

I am trying to display banner notification on macOS swift app and the banner does not appear when running debug build in Xcode neither any new notification related my app is visible in the notification center whereas other app notification are being displayed regularly. What have I missed? I have granted permission when requested

My Appdelegate file

class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification)
        {
         NSUserNotificationCenter.default.delegate = self
        }

    func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool
        {
        return true
        }

On app startup I try to request premission from the user

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge, .provisional])
    { allowed, error in
   if allowed{
       print ("Notifications allowed");
       }else{
       print ("Notifications denied")
       }
 }

And this the code for triggering the notification

let notification = NSUserNotification()
notification.title = "test"
notification.subtitle = "Test notification"
notification.informativeText = "this the test notification"
NSUserNotificationCenter.default.deliver(notification)

What am i missing here.

I am trying to push local notification in macOS swift app.

2

There are a few things you might be missing:
You are using NSUserNotification which is deprecated in macOS 10.14 and later. You should use NSUserNotificationCenter instead.

You are trying to use NSUserNotificationCenter with NSUserNotification which is not correct.

NSUserNotificationCenter is used to display notifications, and NSUserNotification is used to create the content of the notification.

Here is the correct way to do it:

let notification = NSUserNotification()
notification.title = "test"
notification.subtitle = "Test notification"
notification.informativeText = "this the test notification"
NSUserNotificationCenter.default.deliver(notification)

This will not work because NSUserNotificationCenter does not support delivering notifications directly. You need to schedule the notification to be delivered at a later time.

Here is how you can do it:

let notification = NSUserNotification()
notification.title = "test"
notification.subtitle = "Test notification"
notification.informativeText = "this the test notification"

let notificationCenter = NSUserNotificationCenter.default
notificationCenter.deliveryAlert = true
notificationCenter.schedule(notification)

This will schedule the notification to be delivered. However, this will not work if the user has not granted permission to your app to display notifications. You need to request permission first:

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { allowed, error in
    if allowed {
        print("Notifications allowed")
    } else {
        print("Notifications denied")
    }
}

This will request permission to display notifications. If the user grants permission, you can schedule the notification to be delivered.

Remember to handle the case where the user denies permission. In this case, you should not schedule the notification.

Also, remember to handle the case where the user has granted permission but has not allowed notifications to be displayed in the notification center. In this case, you should not schedule the notification.

For macOS version 10.14 and above please use UNUserNotificationCenter instead of NSUserNotification

For delegate on macOS 10.14 and above

@available(macOS 10.14, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.sound, .alert])
}

For delegate on macOS lower than 10.14

public func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
    return true
}

For triggering the notification on macOS 10.14 and above

let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.title = "test"
content.body = "Test notification"
let request = UNNotificationRequest(identifier: "Test Identifier", content: content, trigger: nil)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

For triggering the notification on macOS lower than 10.14

let notification = NSUserNotification()
notification.title = "test"
notification.informativeText = "Test notification"
notification.soundName = NSUserNotificationDefaultSoundName
delegate = self
deliver(notification)

Your permission code is correct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật