UILocalNotification 根本不起作用
我在 UILocalNotification
方面遇到了一些非常恼人的问题。
在完成一个即将完成的应用程序时,我注意到无论我如何尝试,我都无法让本地通知正常工作。
因此,我没有浪费时间,而是决定回到基础知识,看看是否能让它们发挥作用。
我创建了一个新的基于 XCode 视图的应用程序,并将 -viewDidLoad
替换为:
- (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 和模拟器上都进行了测试。
我在这里错过了一些真正重要的东西吗? (我搜索了 Apple 文档,但找不到任何关于为什么会发生这种情况的信息)
谢谢
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.
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];
}
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.
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)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当应用未运行(或在后台运行)时,
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 anUIAlertView
yourself in the delegate method.只是我个人愚蠢冒险的评论......
我有同样的问题,但我的问题是我忘记了为alertBody分配一个值。如果不设置alertBody,则不会显示通知。
Just a comment from my personal adventures in stupidity...
I had the same issue, but my problem was that I had forgotten to assign a value to alertBody. If you don't set alertBody, the notification won't display.
还有一点,不要忘记显示查询是否允许推送,将以下代码添加到AppDelegate:
one more thing, do not forget to show query whether to allow push, add below code to AppDelegate: