スケジュールされた通知を表示するために NSUserNotificationCenter を使用しています。右側の通知パネルにアプリの通知があるため、通知をクリックするとアプリが起動しますが、パネルから通知が削除されません。
- アプリが実行されていないときに通知をクリックすると、applicationDidFinishLaunching が呼び出され、didActivateNotification デリゲートが呼び出されないため、通知は削除されません。
- アプリケーションが既に実行されていて、通知をクリックすると、appShouldHandleReopen が呼び出されます。そして、デリゲートを呼び出しません。
-(void)userNotificationCenter:(id)center didActivateNotification:(id)通知
ユーザーが通知をクリックすると、通知が削除されるすべての標準アプリの動作を確認しました。
次のように実装しました。
/* will create notification and set the delegate*/
@interface NotificationViewController : NSViewController
{
id delegate;
}
-(void)showNotification;
@end
@implementation NotificationViewController
- (void)createNotification:(NSString*)str
{
    Class UserNotificationClass=NSClassFromString(@"NSUserNotification");
    id notification = [[UserNotificationClass alloc] init];
    //Set the title of the notification
    [notification setTitle:@"My Notification"];
    [notification setSubtitle:@"Test"];
    [notification setHasActionButton:YES];
    [notification setActionButtonTitle:@"OK"];
    [notification setOtherButtonTitle:@"CANCEL"];
    //Set the text of the notification
    [notification setInformativeText:str];
    [notification setDeliveryDate:[NSDate dateWithTimeInterval:0.1 sinceDate:[NSDate date]]];
    [notification setSoundName:@"NSUserNotificationDefaultSoundName"];
    [notification setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:str,@"string",nil]];
    Class UserNotificationCenterClass=NSClassFromString(@"NSUserNotificationCenter");
    id center = [UserNotificationCenterClass defaultUserNotificationCenter];
    [center setDelegate:self.delegate];
    //Scheldule our NSUserNotification
    [center scheduleNotification:notification];
    [notification release];
}
@end
/* Class which implements NSUserNotificationDelegate*/
@interface NotificationHandler : NSObject <NSUserNotificationCenterDelegate>
{
}
- (void)createNotification:(NSString*)str;
@end
@implementation NotificationHandler
- (void)createNotification:(NSString*)str
{
NotificationHandler *notificationHandler = [[NotificationHandler alloc] initWithNibName:@"NotificationHandler" bundle:[NSBundle mainBundle]];
        notificationHandler.delegate = self;
[notificationHandler showNotification];
}
- (void)userNotificationCenter:(id)center didActivateNotification:(id)notification
{
    [center removeDeliveredNotification:notification];
}
- (BOOL)userNotificationCenter:(id)center shouldPresentNotification:(id)notification
{
    return YES;
}
@end
ただし、これは私の場合は機能しません。そのポイントを教えてください...
私が観察した他のこと:
- NSUserNotificationDelegate を AppDelegate に実装すると、2 番目のケース (アプリケーションが既に実行されていて、通知をクリックしたとき) で機能します。この場合、通知は削除されます。
- そして、最初のケースの通知を削除するために、つまり(アプリが実行されていないときに通知をクリックすると、appDidFinishLaunching が呼び出されます)、appDidFinishLaunching に次のコードを記述しました。 - NSUserNotification *userNoti = [[通知 userInfo] valueForKey:@"NSApplicationLaunchUserNotificationKey"]; [[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification:userNoti]; - なぜこれが AppDelegate では機能するのに、上記の実装では機能しないのか、または最初の実装で何か間違ったことをしているのですか?
- 私の AppDelegate 実装は私の両方のシナリオを処理するのに問題ありませんか?
 
前もって感謝します。