【问题标题】:Schedule notification in background task在后台任务中安排通知
【发布时间】:2014-11-21 16:43:15
【问题描述】:

我正在为 iOS 开发一个日历/闹钟应用程序,它与网络服务器同步。在服务器上添加活动时,会发送推送通知,以便 iOS 客户端可以获取新数据,并在需要时更新和安排下一次警报的时间(本地通知)。

但这仅在应用程序在客户端打开时才有效。我希望客户端接收推送通知,如果需要,在后台重新安排下一次警报的时间。

这在 iOS 上不可能吗?

【问题讨论】:

    标签: ios ios7


    【解决方案1】:

    您可以为此使用 Background Fetch,操作系统会定期“唤醒”您的应用以在后台执行数据提取。

    首先,为您的应用启用后台提取功能。在 XCode 6 中,查看您的项目,然后转到 Capabilities 选项卡,打开 Background Modes,然后检查 Background Fetch。

    然后你必须在 App Delegate 中实现一些代码:

    在application:didFinishLaunchingWithOptions:,添加:

    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    

    上面设置了您希望系统多久“唤醒”您的应用程序以进行理想的后台进程。请注意,最终频率由 iOS 中的算法确定,因此可能并不总是如此频繁。

    -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    //fetch code here
    completionHandler(UIBackgroundFetchResultNewData);
    

    }

    以上是在这段后台进程期间调用的实际覆盖函数。记得拨打completionHandler - 不这样做可能会降低您的应用下次在后台运行的机会(或者说文档)。您可以传递给completionHandler 的枚举是UIBackgroundFetchResultNewData、UIBackgroundFetchResultNoData、UIBackgroundFetchResultFailed。根据获取的结果使用其中之一。

    【讨论】:

      【解决方案2】:
      // use this methods in Appdeleagte
      - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
      
          [self showAlarm:notification.alertBody];
          application.applicationIconBadgeNumber = 1;
          application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
      }
      
      //  call this  in appdelagete
      -(void)makeNotificationRequest:(UILocalNotification *)notification1
      {
          [self showAlarm:notification1.alertBody];
      }
      
      // call this mathods in appdelagte
      - (void)showAlarm:(NSString *)text {
      
      **strong text**
      // set notification and call this notification methods your another view ..... 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"uniqueNotificationName" object:self]; //leak
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-30
        • 2019-01-02
        • 1970-01-01
        • 2014-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多