簡體   English   中英

如何使用NSUserNotification在單擊一次按鈕時兩次顯示用戶通知?

[英]How to show User notification twice on single button click using NSUserNotification?

讓我首先描述場景,然后描述問題:

我創建了一個使用NSUserNotification顯示用戶通知的函數

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

    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = @"TechHeal";
    notification.informativeText = message;
    //notification.soundName = NSUserNotificationDefaultSoundName;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];    
}

我有一個從服務器獲取詳細信息的按鈕。 在開始和按鈕的結尾處,單擊“我已致電通知”,如下所示:

-(IBAction)get2000Rows:(id)sender{

    [self notify:@"Please wait..."];

    //some code that takes a while to run. like 10 minues :P

    [self notify:@"Thanks for waiting..."];

}

現在,問題是按鈕單擊上沒有顯示第一個通知“ Please wait ...”,但是最后一個通知顯示得很好。

我也嘗試在單獨的線程中調用Notify函數,但效果不佳。 (如下所示)

dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

    dispatch_async(backgroundQueue, ^{

        [self notify:@"Please wait..."];


        dispatch_async(dispatch_get_main_queue(), ^{
        });    
    });

非常感謝您的幫助。 先感謝您。

問題是您在與代碼的UI部分相同的線程上運行10 minutes的代碼。 因此,您應該使用以下命令將它們分開:

-(IBAction)get2000Rows:(id)sender{

    [self notify:@"Please wait..."];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //some code that takes a while to run. like 10 minues :P

        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self notify:@"Thanks for waiting..."];
        });
    });
}

您可以通過NSTimer啟動計時器以進行消息傳遞

@interface MyClass ()
{
     NSTimer *timer;
}

-(IBAction)get2000Rows:(id)sender{

       [self notify:@"Please wait..."];


       timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
                                                target:self
                                            selector:@selector(timerClick:)
                                            userInfo:nil
                                             repeats:YES];
}

 - (void)timerClick:(NSTimer *)timer {

        [self notify:@"Thanks for waiting..."];
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM