【问题标题】:Local Notification in foreground in iPhone SDKiPhone SDK中前台的本地通知
【发布时间】:2011-10-14 23:12:00
【问题描述】:

当应用程序处于前台并且当前在 iPhone SDK 中运行时,是否会显示本地通知?

【问题讨论】:

    标签: iphone local uilocalnotification


    【解决方案1】:

    不,您将在 appdelegate 中收到通知。

    - (void) application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification {
        //Place your code to handle the notification here.
    }
    

    【讨论】:

    • 非常感谢。现在,我在应用程序的 didReceiveLocalNotification 方法中保留了一个 UIAlertView,以便在应用程序已经运行时可以使用它来代替通知。但是当应用程序在后台并且通知被触发并且当应用程序进入前台时,这个方法被调用并且那个 alertView 出现了。你能告诉我如何避免这种情况吗?
    • 好的,我们有 applicationWillEnterForeground: 方法。对不起愚蠢的问题!非常感谢。
    • 不是一个愚蠢的问题 - 你刚刚给了我我正在寻找的答案:-)
    【解决方案2】:

    我制作了一个库来制作与本地通知几乎相同的动画。

    检查这个: https://github.com/OpenFibers/OTNotification

    演示:

    当您在

    中收到消息时,您可以向此库发布新消息
    - (void) application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification
    {
        OTNotificationManager *notificationManager = [OTNotificationManager defaultManager];
        OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init];
        notificationMessage.title = [self notificationTitle];
        notificationMessage.message = @"A notification. Touch me to hide me.";
        [notificationManager postNotificationMessage:notificationMessage];
    }
    

    【讨论】:

      【解决方案3】:

      接受的分析器是正确的,但不足以接收所有通知并向用户显示来自

      - (void) application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification {
      

      您必须检查是否是当前通知。 有时会触发另一个通知(例如,当您取消它们时)。所以,你必须检查,那是你除了:

      - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
          if (fabs([[NSDate date] timeIntervalSinceDate:[notification fireDate]]) <= 0.5f)
          {
              [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notification alert", @"")
                                          message:notification.alertBody
                                         delegate:self
                                cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];    
          }
      }
      

      【讨论】:

        【解决方案4】:

        如果您的应用当前处于前台,则将在您的委托中调用以下函数:

        - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)Notifikation
        

        然后您可以决定显示警报视图,但标准视图不会自行显示

        【讨论】:

        • 无论应用程序是否已经运行,都会调用它。
        【解决方案5】:

        斯威夫特 2.2:

        func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
            var state = application.applicationState
            if state == .Active {
                // handle the notification, e.g. show an alert 
            } 
        }
        

        斯威夫特 3.0:

        func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
            var state: UIApplicationState = application.applicationState
            if state == .active {
                // handle the notification, e.g. show an alert
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多