【问题标题】:NSUserNotification makes sound but does not showNSUserNotification 发出声音但不显示
【发布时间】:2015-01-21 21:21:20
【问题描述】:

我的NSUserNotification 正在传送,正在播放给定的声音,但如果我的应用不是最顶层的应用,则不会在屏幕上直观显示。

如果我的应用程序在后台,则不会显示通知。如果我的应用程序处于活动状态,它会显示。在这两种情况下,声音都会播放。

这是我发送通知的方式:

NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle: @"hi"];
[notification setSoundName: NSUserNotificationDefaultSoundName];
[notification setDeliveryDate: [NSDate dateWithTimeIntervalSinceNow: 3]];
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification: notification];

我将覆盖 shouldPresentNotification 以始终呈现它。

[NSUserNotificationCenter defaultUserNotificationCenter].delegate = self;

...

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
    return YES;
}

OSX 10.10.1 优胜美地

如何让这个通知总是显示,就像它总是播放声音一样?

【问题讨论】:

    标签: objective-c macos nsusernotification


    【解决方案1】:

    我找到了两种让通知始终显示的方法:

    使用

    删除所有以前的通知
    [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications];
    

    或者通过改变标识符

    notification.identifier = @"com.yourcompany.yourapp.notificationidentifier";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 2016-03-31
      • 1970-01-01
      相关资源
      最近更新 更多