UILocalNotification根本不起作用 [英] UILocalNotification isn't working at all
问题描述
我在使用UILocalNotification
时遇到了一些令人烦恼的问题.
I'm having some really irritating problems with UILocalNotification
.
在完成我即将完成的应用程序的过程中,我注意到无论尝试如何,都无法获得本地通知.
While finishing up an app that I've nearly completed, I noticed that I couldn't get local notifications to work, no matter what I tried.
因此,我决定不浪费时间,而是回到基础知识,看看是否可以使它们完全正常工作.
So instead of wasting time, I decided to go back to basics and see if I could get them working at all.
我创建了一个新的基于XCode视图的应用程序,并用以下内容替换了-viewDidLoad
:
I created a new XCode view-based application, and replaced -viewDidLoad
with this:
- (void)viewDidLoad
{
UILocalNotification * theNotification = [[UILocalNotification alloc] init];
theNotification.alertBody = @"Alert text";
theNotification.alertAction = @"Ok";
theNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
[[UIApplication sharedApplication] scheduleLocalNotification:theNotification];
}
但是,这也什么都不做.
我希望在启动应用程序10秒钟后看到通知,但什么也没出现.
另外,我在iPhone和模拟器上都对此进行了测试.
However, that also doesn't do anything at all.
I expected to see a notification 10 seconds after launching the app, but nothing appears.
Also, I tested this on both my iPhone and the simulator.
我在这里错过了一些非常关键的东西吗? (我已经搜索了Apple文档,却找不到发生这种情况的任何原因)
Am I missing something really crucial here? (I've searched through the Apple documentation and couldn't find anything as to why this is happening)
谢谢
推荐答案
UILocalNotification
仅在应用程序未运行(或在后台运行)时自动显示.如果应用程序正在运行并且触发了本地通知,则会调用UIApplicationDelegate
的- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
方法,并且系统不会显示任何内容(也不会播放声音).如果要显示通知,请在委托方法中自己创建一个UIAlertView
.
UILocalNotification
s are only displayed automatically if the app is not running (or running in background). If the app is running and a local notification fires, UIApplicationDelegate
’s - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
method gets called and the system doesn’t display anything (nor does it play a sound). If you want to display the notification, create an UIAlertView
yourself in the delegate method.
这篇关于UILocalNotification根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!