【问题标题】:application:didReceiveRemoteNotification:fetchCompletionHandler error应用程序:didReceiveRemoteNotification:fetchCompletionHandler 错误
【发布时间】:2015-08-09 19:08:54
【问题描述】:

这是我完整的“AppDelegate.swift”文件:

import UIKit

import Parse
import Bolts

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        Parse.setApplicationId("NOT SHOWING MY KEY",
            clientKey: "NOT SHOWING MY KEY2")

        // Register for Push Notitications
        if application.applicationState != UIApplicationState.Background {
            // Track an app open here if we launch with a push, unless
            // "content_available" was used to trigger a background push (introduced in iOS 7).
            // In that case, we skip tracking here to avoid double counting the app-open.

            let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
            let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
            var pushPayload = false
            if let options = launchOptions {
                pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
            }
            if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
                //PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
                PFAnalytics.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
            }
        }
        if application.respondsToSelector("registerUserNotificationSettings:") {
            let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
            let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        } else {
            let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound
            application.registerForRemoteNotificationTypes(types)
        }

        return true
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let installation = PFInstallation.currentInstallation()
        installation.setDeviceTokenFromData(deviceToken)
        //installation.saveInBackground()
        installation.saveInBackgroundWithTarget(nil, selector: nil)
    }

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        if error.code == 3010 {
            println("Push notifications are not supported in the iOS Simulator.")
        } else {
            println("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
        }
    }

    func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == .Inactive {
            PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
        }
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

我已经从这里设置了一切: https://www.parse.com/apps/quickstart#parse_push/ios/swift/existing

当我打开 xCode 并使用连接到 iphone 的电缆测试我的应用程序时,当我从站点按“尝试”时出现错误:

警告:应用程序委托收到调用 -application:didReceiveRemoteNotification:fetchCompletionHandler: 但从未调用过完成处理程序。

【问题讨论】:

    标签: xcode swift parse-platform push-notification


    【解决方案1】:

    请按照错误消息的提示执行操作:调用完成处理程序

    来自文档

    完成通知处理后,您必须致电 在 handler 参数中阻止,否则您的应用程序将被终止。您的 应用程序有长达 30 秒的挂钟时间来处理 通知并调用指定的完成处理程序块。在 练习,你应该在完成后立即调用处理程序块 处理通知。系统跟踪经过时间、电量 应用程序后台下载的使用量和数据成本。应用程序 处理远程通知时使用大量电力 可能并不总是被提早唤醒以处理未来的通知。

    所以,例如调用

    completionHandler(.NewData)
    

    【讨论】:

      【解决方案2】:

      我稍微清理了你的AppDelegate.swift。这应该是您注册和使用推送通知所需的全部内容。我还在最后添加了一个功能,用于在用户打开应用时清除徽章计数器:

      import UIKit
      import Parse
      import Bolts
      
      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
      
          var window: UIWindow?
      
          func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
              // Override point for customization after application launch.
      
              Parse.setApplicationId("NOT SHOWING MY KEY",
                  clientKey: "NOT SHOWING MY KEY2")
      
              // Register for Push Notitications
              let userNotificationTypes = (UIUserNotificationType.Alert |
                  UIUserNotificationType.Badge |
                  UIUserNotificationType.Sound);
      
              let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
              application.registerUserNotificationSettings(settings)
              application.registerForRemoteNotifications()
      
      
              return true
          }
      
          func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
              let installation = PFInstallation.currentInstallation()
              installation.setDeviceTokenFromData(deviceToken)
              installation.saveInBackground()
          }
      
          func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
              if error.code == 3010 {
                  println("Push notifications are not supported in the iOS Simulator.")
              } else {
                  println("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
              }
          }
      
          func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
              if application.applicationState == .Inactive {
                  PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
              }
          }
      
          // ADDED FUNCTION:
          func applicationDidBecomeActive(application: UIApplication) {
      
              //Reset badge counter to zero
              var currentInstallation = PFInstallation.currentInstallation()
              if(currentInstallation.badge != 0){
                  currentInstallation.badge = 0
              }
          }
      
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 2017-06-28
        • 2013-08-25
        • 1970-01-01
        相关资源
        最近更新 更多