【问题标题】:How to get local notification when app is running in Objective-C应用程序在 Objective-C 中运行时如何获取本地通知
【发布时间】:2017-09-27 09:30:39
【问题描述】:

我是 iOS 新手,在应用程序运行时显示本地通知时遇到问题。 我的代码是这样的

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
localNotification.alertBody = @"Message hear";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我在单击按钮时编写了此代码,但它没有向我显示通知。 提前致谢!

【问题讨论】:

  • 如果应用程序正在运行,为什么会显示通知?通知是让用户启动应用程序的一种方式。如果应用程序已经在运行,为什么要这样做?

标签: ios objective-c uilocalnotification


【解决方案1】:

您必须添加此代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

在 didFinishLaunchingWithOptions 中

如果你设置了这个,那么你必须改变这一行

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.

当您点击按钮设置通知但您无法显示时,因为您的应用程序处于活动状态并替换 0 到 60 时间间隔并放置您的应用程序背景并检查

【讨论】:

    【解决方案2】:

    不,如果您使用上面的代码,它不会显示。首先我想问一些问题。

    Does your above code work if you use iOS 10?

    Where do you call above lines of code in your class or in which method do you apply?

    iOS 10 有一个名为 UNUserNotification 的新框架。我们必须导入 UNUserNotificationCenterDelegate 以在应用运行时显示 UILocalNotification。

    UNUserNotificationCenterDelegate 协议定义了用于 响应可操作通知和接收通知 当您的应用处于前台时。您分配您的委托对象 到共享 UNUserNotificationCenter 的委托属性 目的。用户通知中心对象调用您的方法 在适当的时候委派传递信息。

    然后当通知到达时它会调用willPresentNotification

    如果您的应用在通知到达时处于前台,则 通知中心调用以下方法

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center 
       willPresentNotification:(UNNotification *)notification 
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
    

    上述方法传递通知 直接到您的应用程序。如果你实现这个方法,你可以采取 处理通知和更新所需的任何操作 你的应用程序。完成后,执行 completionHandler 块并 指定您希望系统如何提醒用户(如果有的话)。如果你的 委托未实现此方法,系统将警报静音为 如果您已将 UNNotificationPresentationOptionNone 选项传递给 完成处理程序块。如果您根本不提供委托 对于 UNUserNotificationCenter 对象,系统使用 通知的原始选项来提醒用户。

    How can you show the notification?

    为了实现这一点,我们有 3 个选项

    Sound
    Badge
    Alert 
    

    UNNotificationPresentationOptions help us to implement this

    上面是理解和实践的好方法。

    我也喜欢一些答案。他们是

    Show iOS notification when app is running

    Displaying a stock iOS notification banner when your app is open and in the foreground?

    Getting local notifications to show while app is in foreground

    Notification while app is in foreground

    Notification in iOS 10

    Local Notification

    didReceiveRemoteNotification not called , iOS 10

    【讨论】:

      【解决方案3】:

      这样试试

      NSTimeInterval interval;
      interval = 60* 60* 12;//12 hours from now
      UILocalNotification *localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//Enter the time here in seconds.
      localNotification.alertBody= @"This is message Users will see";
      localNotification.timeZone = [NSTimeZone defaultTimeZone];
      localNotification.repeatInterval= NSCalendarUnitMinute;//NSCalendarUnitMinute; //Repeating instructions here.
      localNotification.soundName= UILocalNotificationDefaultSoundName;
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
      

      【讨论】:

      • 感谢您的帖子。您能否提供更多解释以使您的回复更有用。
      • 但是如果你不想这样的话,通知会重复,用这个代码替换行 localNotification.fireDate= [[NSDate date] addTimeInterval:5];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2012-02-29
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多