xcode UILocalNotification 根本不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4974512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:13:27  来源:igfitidea点击:

UILocalNotification isn't working at all

iosobjective-cxcodeuilocalnotification

提问by JPEG_

I'm having some really irritating problems with UILocalNotification.

我有一些非常恼人的问题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 -viewDidLoadwith this:

我创建了一个新的基于 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];
}

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.

但是,这也根本没有任何作用。
我希望在启动应用程序 10 秒后看到通知,但什么也没出现。
另外,我在我的 iPhone 和模拟器上测试了这个。

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)

我在这里错过了一些非常重要的东西吗?(我已经搜索了 Apple 文档,但找不到任何关于为什么会发生这种情况的信息

Thanks

谢谢

回答by mplappert

UILocalNotifications are only displayed automatically if the app is notrunning (or running in background). If the app is running and a local notification fires, UIApplicationDelegate's - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notificationmethod gets called and the system doesn't display anything (nor does it play a sound). If you want to display the notification, create an UIAlertViewyourself in the delegate method.

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

回答by ghostatron

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.

我有同样的问题,但我的问题是我忘记为 alertBody 赋值。如果您不设置 alertBody,则不会显示通知。

回答by ibamboo

  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]; 
      }
    }
    
  1. fireDate 必须是未来时间。
  2. 应用程序必须在后台运行,或已关闭。
  3. 还有一件事,不要忘记显示查询是否允许推送,将以下代码添加到 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]; 
      }
    }