2

I'm not sure if I got something wrong but from the examples I could find. I wrote that little helloworld.

#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>


int main (int argc, const char * argv[])
{
    NSUserNotification *userNotification = [[NSUserNotification alloc] init];
    userNotification.title = @"Some title";
    userNotification.informativeText = @"Some text";

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

    return 0;
}

I compile it with:

cc -framework Foundation -o app main.m

It compiles and I can execute it but it won't show anything. For some reasons, nothing gets presented. I read that Notifications may not get displayed if the application is the main actor. How can I make it show Notification?

4

4 回答 4

7

设置委托并覆盖

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                               shouldPresentNotification:(NSUserNotification *)notification {
  return YES;
}
于 2013-05-06T12:06:59.873 回答
1

请务必为任何新通知分配一个新标识符。通知只会显示一次,除非使用了新的标识符或用户清除了他们的通知历史记录。

于 2018-11-24T00:23:16.690 回答
1

有关 Objective-C 和 Swift 的完整示例:

我们需要提供一个NSUserNotificationCenterDelegate, 在最简单的情况下我们可以使用AppDelegate

Objective-C

我们需要修改AppDelegate提供NSUserNotificationCenterDelegate方法。从一个干净的AppDelegate(看起来像这样......)

@interface AppDelegate ()

我们将其修改为:

@interface AppDelegate () <NSUserNotificationCenterDelegate>

现在我们可以在里面提供所需的委托方法@implementation AppDelegate

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                               shouldPresentNotification:(NSUserNotification *)notification {
  return YES;
}

斯威夫特 4

您可以使用extensionofAppDelegate来提供NSUserNotificationCenterDelegate方法:

extension AppDelegate: NSUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
        return true
    }
}
于 2018-05-11T02:14:04.820 回答
-2

我知道这是一个老问题 - 但由于可能有人在寻找如何在 Python 和 PyObjC 中执行此操作的答案,因此我将在此处发布正确的语法(因为它是谷歌搜索结果中的佼佼者之一)。由于我无法评论小鬼的评论,我将把它作为另一个答案发布。

在 Python 中使用 pyobjc 也可以这样做:

def userNotificationCenter_shouldPresentNotification_(self, center, notification):
    return True

完整的课程看起来像这样:

from Cocoa import NSObject
import objc

class MountainLionNotification(NSObject):
    """ MacOS Notifications """
    # Based on http://stackoverflow.com/questions/12202983/working-with-mountain-lions-notification-center-using-pyobjc

    def userNotificationCenter_shouldPresentNotification_(self, center, notification):
        return True

    def init(self):

        self = super(MountainLionNotification, self).init()
        if self is None: return None

        # Get objc references to the classes we need.
        self.NSUserNotification = objc.lookUpClass('NSUserNotification')
        self.NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')

        return self

    def clearNotifications(self):
        """Clear any displayed alerts we have posted. Requires Mavericks."""

        NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
        NSUserNotificationCenter.defaultUserNotificationCenter().removeAllDeliveredNotifications()

    def notify(self, title="", subtitle="", text="", url=""):
        """Create a user notification and display it."""

        notification = self.NSUserNotification.alloc().init()
        notification.setTitle_(str(title))
        notification.setSubtitle_(str(subtitle))
        notification.setInformativeText_(str(text))
        # notification.setSoundName_("NSUserNotificationDefaultSoundName")
        notification.setHasActionButton_(False)
        notification.setActionButtonTitle_("View")
        # notification.setUserInfo_({"action":"open_url", "value": url})

        self.NSUserNotificationCenter.defaultUserNotificationCenter().setDelegate_(self)
        self.NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)

        # Note that the notification center saves a *copy* of our object.
        return notification
于 2015-05-28T15:07:17.060 回答