2

我正在为 Growl 1.3.1 SDK 开发一个小型包装器。更具体地说,我想将 Growl 打包到我的应用程序中,这样即使用户没有 Growl,他们仍然能够收到通知。我之前安装了 Growl,我的代码会触发通知。我已经卸载了 Growl 并且只使用了框架;雾,我相信它被称为。但是,当我现在启动代码(Growl 已卸载)时,不会触发任何通知!以下是我目前正在使用的代码:

#import "growlwrapper.h"

void showGrowlMessage(std::string title, std::string desc) {
    std::cout << "[Growl] showGrowlMessage() called." << std::endl;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [GrowlApplicationBridge setGrowlDelegate: @""];
    [GrowlApplicationBridge
        notifyWithTitle: [NSString stringWithUTF8String:title.c_str()]
        description: [NSString stringWithUTF8String:desc.c_str()]
        notificationName: @"Upload"
        iconData: nil
        priority: 0
        isSticky: NO
        clickContext: nil
    ];
    [pool drain];
}

int main() {
    showGrowlMessage("Hello World!", "This is a test of the growl system");
    return 0;
}

我也有适当的 Growl Registration 字典,并且正在编译:

g++ growlwrapper.mm -framework Growl -framework Foundation -o growltest

这段代码有什么问题吗?任何想法为什么它不会被解雇?


编辑:似乎上面的代码工作得很好。只需要在运行循环中使用适当的 Growl 字典即可。

4

1 回答 1

2

我不是 Growl 的权威,但我有一个很好的预感:当安装 Growl 应用程序时,像这样的一次性通知需要祈祷,因为正在运行的应用程序有一个运行循环并且可以驱动 UI它。在您在这里的示例中,没有运行循环,因此这个一次性应用程序无法绘制任何通知——它甚至在它有机会之前就已经死了。我猜如果你制作了一个样板的 Cocoa 应用程序,然后调用showGrowlMessagefrom applicationDidFinishLaunching:,但在你终止/退出应用程序之前,我敢打赌它会工作。至少你应该试一试。

编辑:如果您创建一个新的 Cocoa 非文档应用程序,并将以下方法添加到 appDelegate 类,它将使用 Mist(即应用程序内)Growl 成功显示通知。

@implementation SOAppDelegate

@synthesize window = _window;

- (void)showGrowlMessageTitled: (NSString*)title description:(NSString*) desc
{
    NSLog(@"[Growl] showGrowlMessage() called.");
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [GrowlApplicationBridge notifyWithTitle: title
                                description: desc
                           notificationName: @"Upload"
                                   iconData: nil
                                   priority: 0
                                   isSticky: NO
                               clickContext: nil];
    [pool drain];
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [GrowlApplicationBridge setGrowlDelegate: (NSObject<GrowlApplicationBridgeDelegate>*)self];
    [self showGrowlMessageTitled: @"Foo" description: @"Bar"];    
}

- (NSDictionary *) registrationDictionaryForGrowl
{
    return [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSArray arrayWithObject: @"Upload"], GROWL_NOTIFICATIONS_ALL,
            [NSArray arrayWithObject: @"Upload"], GROWL_NOTIFICATIONS_DEFAULT,
            nil];
}

@end

因此,简而言之,原始代码的问题不仅是 runLoop 问题,而且它没有将真正的委托(即,根据需要实现标头中描述的委托方法的对象)传递给 GrowlApplicationBridge(它传递了一个空的细绳)。你肯定仍然需要一个 runLoop,但这还不是全部——还有额外的、非可选的设置来使用这个框架。

于 2012-01-16T03:59:13.833 回答