What's The Difference Between Nsusernotification and Unusernotification in Macos

What's the Difference Between NSUserNotification and UNUserNotification in macOS?

NSUserNotification is now deprecated.

A UNNotification object contains the initial notification request, which contains the notification's payload, and the date on which the notification was delivered.

Don't create notification objects directly. When handling notifications, the system delivers notification objects to your UNUserNotificationCenterDelegate object. The UNUserNotificationCenter object also maintains the list of notifications that were previously delivered, and you use the getDeliveredNotifications(completionHandler:) method to retrieve those objects.

Use UNUserNotifaction instead

Display banner for NSUserNotification while app is frontmost

Your notification centre delegate and delegate method should be implemented in AppDelegate then it works. If you implement in any other class, it won't present as banner though it silently comes in notification panel.

I tried as below and its working:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {

func applicationDidFinishLaunching(aNotification: NSNotification) {
let notification: MyNotificationDelegate = MyNotificationDelegate()
NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;
notification.setNotification("Hi", message: "How are you?")
}

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

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}

}

class MyNotificationDelegate: NSObject {

func setNotification(title: String, message: String)
{
let notification: NSUserNotification = NSUserNotification()
notification.title = title
notification.informativeText = message
NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
}
}

MacOS App Local Notification Not Showing when testing with XCode

I believe that my problem here was asking permission to use UNUserNotification and then using NSUserNotification to create the notification itself, which of course I had not requested permission to use. Requesting permission is now mandatory in Catalina (and perhaps it was in earlier versions of macOS as well.)

So I replaced the generateNotification function with the following and it all works correctly.

let notificationCenter = UNUserNotificationCenter.current();
notificationCenter.getNotificationSettings
{ (settings) in
if settings.authorizationStatus == .authorized
{
//print ("Notifications Still Allowed");
// build the banner
let content = UNMutableNotificationContent();
content.title = summary ;
content.body = title ;
if sound == "YES" {content.sound = UNNotificationSound.default};
// could add .badge
// could add .userInfo

// define when banner will appear - this is set to 1 second - note you cannot set this to zero
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false);

// Create the request
let uuidString = UUID().uuidString ;
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger);

// Schedule the request with the system.
notificationCenter.add(request, withCompletionHandler:
{ (error) in
if error != nil
{
// Something went wrong
}
})
//print ("Notification Generated");
}


Related Topics



Leave a reply



Submit