UILocalNotification 根本不起作用

发布于 2024-10-16 23:10:42 字数 769 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情丝乱 2024-10-23 23:10:42

仅当应用运行(或在后台运行)时,UILocalNotification才会自动显示。如果应用正在运行并且触发本地通知,则会调用 UIApplicationDelegate- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 方法,并且系统不显示任何内容(也不播放声音)。如果您想显示通知,请在委托方法中自行创建一个UIAlertView

UILocalNotifications 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.

北陌 2024-10-23 23:10:42

只是我个人愚蠢冒险的评论......

我有同样的问题,但我的问题是我忘记了为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.

他夏了夏天 2024-10-23 23:10:42
  1. fireDate 必须是未来时间。
  2. 应用程序必须在后台运行,或者已关闭。
  3. 还有一点,不要忘记显示查询是否允许推送,将以下代码添加到AppDelegate:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
      if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
           UIUserNotificationSettings *设置 = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | 用户界面通知类型警报UIUserNotificationTypeSound 类别:nil]; 
           [应用程序注册用户通知设置:设置]; 
      }
    }
    
  1. the fireDate must be future time.
  2. app must be running in backdrop, or is closed.
  3. one more thing, do not forget to show query whether to allow push, add below code to AppDelegate:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
      if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
           UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil]; 
           [application registerUserNotificationSettings:settings]; 
      }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文