2012-07-26 41 views
39

我使用這個代碼:NSUserNotification不顯示操作按鈕

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    // Insert code here to initialize your application 
    NSUserNotification *notification = [[NSUserNotification alloc] init]; 
    [notification setTitle: @"Title"]; 
    [notification setSubtitle: @"Subtitle"]; 
    [notification setInformativeText: @"Informative Text"]; 

    [notification setHasActionButton: YES]; 
    [notification setActionButtonTitle: @"Action Button"]; 
    [notification setOtherButtonTitle: @"Other Button"]; 

    [notification setSoundName: NSUserNotificationDefaultSoundName]; 

    [notification setDeliveryDate: [NSDate dateWithTimeIntervalSinceNow: 10]]; 
    [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification: notification]; 
} 

而且我得到,沒有失敗,

enter image description here

無操作按鈕或其他按鈕。

回答

26

這裏就是答案。再次

由於#macdev freenode上。

enter image description here

的選擇需要 「警報」 有按鈕。

+1

你是怎麼到這個畫面? – Dorian 2015-11-07 01:00:45

48

正如在前面的回答已經說過的,通知類型需要設置提醒的要顯示的操作按鈕。如果你想設置你的應用程序的默認的通知樣式提醒,您需要與價值警報定義密鑰NSUserNotificationAlertStyle在info.plist中。

見蘋果的info.plist keys reference瞭解更多詳情:

NSUserNotificationAlertStyle指定通知樣式是否應該橫幅警報,或沒有。默認值是橫幅,這是推薦的樣式。

+1

你知道這樣的事情是否適用於iOS嗎? – 2012-08-21 18:22:20

+0

我不相信這可以在iOS中使用當前API配置UILocalNotifications。 – 2012-08-24 22:09:58

+5

而且,應用程序需要進行簽名以允許按鈕。 – RichS 2013-01-09 20:26:21

17

作爲其他答案的一個反例,我們可以使用iTunes - 它仍然會顯示「跳過」按鈕,即使我們設置警報樣式的橫幅。所以我繼續搜索並找到了this github repo其中Indragie Karunaratne在NSUserNotification私有頭文件中提供了一些有用的附加屬性。您可以檢查在NSUserNotification_Private.h文件的屬性的完整列表,但實際用於顯示橫幅通知式的按鍵是

@property BOOL _showsButtons; // @dynamic _showsButtons; 

,所以你可以只添加這行到你的代碼

[notification setValue:@YES forKey:@"_showsButtons"]; 

和您的通知操作按鈕將變爲獨立的警報風格。

+0

不幸的是,這在El Capitan中不起作用。 ( – 2015-10-26 03:53:35

+2

作品對我來說在埃爾卡皮坦.. – Dorian 2015-11-07 00:59:45

+0

如何爲按鈕通知加選擇? – 2016-08-12 08:56:22

1

基於黨派的回覆魔法的命令是:

notification.set_showsButtons_(True) 

茶清:)

+0

在PyObjC完美工作 – kgutwin 2017-08-02 12:44:25