Showing Notification Banner on MAC with Swift

Showing notification banner on Mac with Swift

You won't get a banner if your app is in the foreground.

Try using…

notification.deliveryDate = Date(timeIntervalSinceNow: 5)
NSUserNotificationCenter.default.scheduleNotification(notification)

and then switch to another app

macOS Remote Push Notifications Not Showing Alert Banner

In iOS the following code works fine:

UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }

DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}

For macOS I changed that code to be:

UNUserNotificationCenter.current().requestAuthorization(options: options) { granted, _ in
guard granted else { return }

DispatchQueue.main.async {
NSApplication.shared.registerForRemoteNotifications()
}
}

Turns out the line NSApplication.shared.registerForRemoteNotifications() is incorrect. On macOS you have to pass in the same options you provided into this call.

Changing that line to the following worked.

NSApplication.shared.registerForRemoteNotifications(matching: [.alert, .sound, .badge])

What I find strange is that in Apple's documentation it says that method is deprecated, and that we should use registerForRemoteNotifications() instead. Which makes me think there is some type of bug with registerForRemoteNotifications() where the notifications are not appearing correctly.

One other thing to mention. It did take a little bit of time (couple minutes), and a few notifications sent, for them to actually appear after making that change. Not sure if it was just due to slow internet connection or what. But now they are appearing very quickly after being sent.


Edit

Apple has informed me that this is fixed in macOS 10.14.4. I have not been able to upgrade to the best and test it yet tho. So I can not confirm at this point. I will update this when I get a chance to test on macOS 10.14.4.

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");
}

Swift & NSUserNotification - No banner or alert, but is silently added to the notification list

Some notifications are not shown if the application is the foreground/active application at the time the notification is delivered. See NSUserNotification's presented property for more info.

For example, when you are in iTunes and a new song starts playing, you already see which song is playing and there's no need for a banner to be shown.

Note that you can still get notified when a notification is delivered (and check presented) by providing a delegate to NSUserNotificationCenter, including overriding the presentation behavior by implementing userNotificationCenter:shouldPresentNotification:.

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)
}
}

Creating interactive notifications for OS X

Banners, Alerts, and Badges are all types of NSUserNotification instances. The chat reply image is an example of a the alert style.

To change the user NSUserNotification display style for your application, set the value for NSUserNotificationAlertStyle to alert in your application's Info.plist file. I should note that there are known issues and open radars with this in the Cocoa SDK. The default value for this is banner.

You can then use the Display Information and Displayed Notification Buttons to customize the alert.

See the NSUserNotification API reference for information on how you can customize the buttons, placeholder text, etc on the alert.

Here's how I did it (Swift 2.2, if you are using Swift 2.3 or 3 your syntax may vary). The key is to set hasReplyButton to true.

let notification = NSUserNotification()
notification.hasActionButton = true
notification.actionButtonTitle = "Agree"
notification.title = "Title"
notification.informativeText = "Text."
notification.responsePlaceholder = "Placeholder"
notification.hasReplyButton = true

NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)

Alert

Alert with Reply



Related Topics



Leave a reply



Submit