【问题标题】:Is it possible to get Local Notification when application's state is inactive当应用程序的状态不活动时是否可以获得本地通知
【发布时间】:2012-02-03 08:31:07
【问题描述】:

我正在开发一个 iOS 应用程序,我必须获得本地通知,但如果应用程序的状态处于非活动状态。当应用程序处于后台状态时,我成功收到通知。 那么,当应用程序处于非活动状态时,是否可以获得本地通知? 或者也许这只能通过使用推送通知来实现?

问候, 阿门

【问题讨论】:

    标签: ios uilocalnotification localnotification


    【解决方案1】:

    您需要在应用委托中的两个位置响应本地通知:

    - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    
    - (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    

    第一个是当您的应用未运行时 - 使用 launchOptions 参数检查您的应用是否由于本地通知而启动。

    第二个是您的应用当前正在运行的时间(活动或非活动)。您可以通过在application:didReceiveLocalNotification: 方法中检查NSApplication 的applicationState 属性来检查应用是否处于非活动状态。

    【讨论】:

      【解决方案2】:
      - (void)sendNotification
      {
          UILocalNotification *localNotification = [[UILocalNotification alloc] init];
      //    notification.repeatInterval = NSDayCalendarUnit;
          localNotification.fireDate = vwPicker.date;
          localNotification.alertBody = txtAlarmTitle.text;
          localNotification.timeZone = [NSTimeZone defaultTimeZone];
          localNotification.userInfo = @{@"Title": txtAlarmTitle.text};
          localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
      
          [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
      }
      
      
      - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
      {
          [self handleNotification:notification application:application];
      }
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
          if (localNotification)
              [self handleNotification:localNotification application:application];
      
          return YES;
      }
      
      -(void)handleNotification: (UILocalNotification *)notification application:(UIApplication *)application
      {
          NSString *title = [notification.userInfo objectForKey:@"Title"];
      
          [[[UIAlertView alloc]initWithTitle:@"Smart Alarm" message:title delegate:self cancelButtonTitle:@"Answer the Teaser" otherButtonTitles: nil] show];
      
          application.applicationIconBadgeNumber = 0;
      }
      

      【讨论】:

        【解决方案3】:

        当然,只需使用列出的willResignActiveNotification 通知侦听器 here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-21
          • 1970-01-01
          相关资源
          最近更新 更多