简体   繁体   English

如何在Mac OS X上设置本地通知?

[英]How to set local notification on Mac OS X?

I'm writing an app for mac os x, and I have to display a notification every day, to remind users about something. 我正在为mac os x编写一个应用程序,我每天都要显示一个通知,以提醒用户一些事情。 The notification should appear even if the app isn't running, and I can't use push notification. 即使应用程序未运行,也会显示通知,并且我无法使用推送通知。

I guess the best solution would be local notification, just like on iOS. 我想最好的解决方案是本地通知,就像在iOS上一样。 Also I know UILocalNotification is available on iOS only. 我也知道UILocalNotification仅适用于iOS。 But I think there should be an alternative solution. 但我认为应该有另一种解决方案。

Can anybody please point me in the right direction? 任何人都可以指出我正确的方向吗? Is it even possible? 它甚至可能吗? If yes, what is the best practice to do this? 如果是,那么最佳做法是什么?

Local notifications support, very similar to iOS local notifications, has been added in MacOS 10.8 (Mountain Lion). MacOS 10.8(Mountain Lion)中添加了与iOS本地通知非常相似的本地通知支持。 See MacOSX10.8/Frameworks/Foundation.Framework/NSUserNotification.h for more information. 有关更多信息,请参阅MacOSX10.8/Frameworks/Foundation.Framework/NSUserNotification.h
Quick usage example could be found here . 快速使用示例可以在这里找到。

The notification should appear even if the app isn't running 即使应用未运行,也会显示通知

Well, something will still have to be running to send the actual notification. 好吧,仍然需要运行一些东西才能发送实际通知。 Mostly helper programs are used for this (separate executable binaries that are added as launch agents or launch daemons). 大多数辅助程序用于此(单独的可执行二进制文件,作为启动代理程序或启动守护程序添加)。 So I'm suggesting you create a daemon-like helper program that would be added as launch daemon on a per-user basis. 所以我建议你创建一个类似守护进程的帮助程序,它将作为每个用户的启动守护进程添加。 You can also look at shared file list api to see how can it be added to user's login items. 您还可以查看共享文件列表api,了解如何将其添加到用户的登录项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM