objective-c – NSUserNotification – 点击时打开应用程序

前端之家收集整理的这篇文章主要介绍了objective-c – NSUserNotification – 点击时打开应用程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用NSUserNotification来显示通知.这工作正常问题是当您点击通知时:

>应用通知不会从通知中心删除.
>应用程序(最小化时)不打开.

任何熟悉NSUserNotification的人都可以提供一些指针?

notice.m

#import "Notice.h"

@implementation Notice

- (void) notify:(NSDictionary *)message {

    NSLog(@"Notification - Show it");

    NSUserNotification *notification = [[NSUserNotification alloc] init];
    [notification setTitle:[message valueForKey:@"title"]];
    [notification setInformativeText:[message valueForKey:@"content"]];
    [notification setDeliveryDate:[NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]]];
    [notification setSoundName:NSUserNotificationDefaultSoundName];
    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification:notification];
}

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{

    NSLog(@"Notification - Clicked");

    notification=nil;
    [center removeDeliveredNotification: notification];
}







#pragma mark WebScripting Protocol

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector
{
    if (selector == @selector(notify:))
        return NO;

    return YES;
}

+ (NSString*) webScriptNameForSelector:(SEL)selector
{
    id  result = nil;

    if (selector == @selector(notify:)) {
        result = @"notify";
    }

    return result;
}

// right now exclude all properties (eg keys)
+ (BOOL) isKeyExcludedFromWebScript:(const char*)name
{
    return YES;
}

@end

谢谢

解决方法

只需实现NSUserNotificationCenterDelegate并定义此方法
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification

例:

这是我在“通知程序”应用程序中所做的.

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
    NSRunAlertPanel([notification title],[notification informativeText],@"Ok",nil,nil);
}

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
    notifications=nil;
    [tableView reloadData];
    [center removeDeliveredNotification: notification];
}

通知被激活(用户点击)时,我只是通知用户一个面板(我可以使用一个哈特窗口).在这种情况下,我立即删除已发送的通知,但这不是通常发生的情况.通知可以留下有一些时间,并在1/2小时后取消(这取决于您正在开发的应用程序).

原文链接:https://www.f2er.com/c/115791.html

猜你在找的C&C++相关文章