首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSUserNotificationCenter未显示通知

NSUserNotificationCenter未显示通知
EN

Stack Overflow用户
提问于 2013-04-09 15:55:21
回答 4查看 3.6K关注 0票数 1

我不确定我是否弄错了,但从示例中我可以找到。我写了那个小helloworld。

代码语言:javascript
复制
#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>


int main (int argc, const char * argv[])
{
    NSUserNotification *userNotification = [[NSUserNotification alloc] init];
    userNotification.title = @"Some title";
    userNotification.informativeText = @"Some text";

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

    return 0;
}

我用以下命令编译它:

代码语言:javascript
复制
cc -framework Foundation -o app main.m

它编译了,我可以执行它,但它不会显示任何东西。由于某些原因,没有提供任何内容。我读到,如果应用程序是主要参与者,通知可能不会显示。如何使其显示通知?

EN

回答 4

Stack Overflow用户

发布于 2013-05-06 20:07:00

设置委托和重写

代码语言:javascript
复制
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                               shouldPresentNotification:(NSUserNotification *)notification {
  return YES;
}
票数 7
EN

Stack Overflow用户

发布于 2018-05-11 10:14:05

有关Objective-C和Swift中的完整示例:

我们需要提供一个NSUserNotificationCenterDelegate,在最简单的情况下,我们可以使用AppDelegate实现这一点

Objective-C

我们需要修改提供NSUserNotificationCenterDelegate方法的AppDelegate。从干净的AppDelegate (看起来像这样...)

代码语言:javascript
复制
@interface AppDelegate ()

我们将其修改为:

代码语言:javascript
复制
@interface AppDelegate () <NSUserNotificationCenterDelegate>

现在,我们可以在@implementation AppDelegate中提供所需的委托方法

代码语言:javascript
复制
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                               shouldPresentNotification:(NSUserNotification *)notification {
  return YES;
}

Swift 4

您可以使用AppDelegateextension来提供NSUserNotificationCenterDelegate方法:

代码语言:javascript
复制
extension AppDelegate: NSUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
        return true
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-11-24 08:23:17

请确保为任何新通知分配新的标识符。一个通知只会显示一次,除非使用了新的标识符或用户清除了其通知历史记录。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15896348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档