iOS Background Fetch Not Working Even Though Correct Background Mode Configured

余生颓废 提交于 2019-11-29 02:51:42

Here's the only way I've found to test background fetch.

  1. Edit your scheme
  2. Select Run
  3. Select Options
  4. Check the "Launch due to background fetch event" option
  5. Plug-In your iOS Device and run the application on it. It does not work in the iOS Simulator.

Take a look on the XCode release note: https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

"The Xcode menu command Simulator Background Fetch does not work.

Use the menu command in iOS Simulator instead. (20145602)"

Vitaliy Gozhenko

You can create your own background fetch push notification to your real device via Houston

  1. install Houston: Open Terminal app and use sudo gem install houston
  2. create PEM certificate for your app Generate .pem file Used to setup Apple PUSH Notification
  3. Get your device token from

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

  1. run Terminal and use

for usual push notification: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -m "Hello from the command line!"

for background fetch push notification: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -n

More options for apn app:

  c.option '-m', '--alert ALERT', 'Body of the alert to send in the push notification'
  c.option '-b', '--badge NUMBER', 'Badge number to set with the push notification'
  c.option '-s', '--sound SOUND', 'Sound to play with the notification'
  c.option '-y', '--category CATEGORY', 'Category of notification'
  c.option '-n', '--[no]-newsstand', 'Indicates content available for Newsstand'
  c.option '-d', '--data KEY=VALUE', Array, 'Passes custom data to payload (as comma-delimited "key=value" declarations)'
  c.option '-P', '--payload PAYLOAD', 'JSON payload for notifications'
  c.option '-e', '--environment ENV', [:production, :development], 'Environment to send push notification (production or development (default))'
  c.option '-c', '--certificate CERTIFICATE', 'Path to certificate (.pem) file'
  c.option '-p', '--[no]-passphrase', 'Prompt for a certificate passphrase'

This method is called.

 func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

}

you forgot to write @escaping after completionHandler

In Xcode 8.3.2 this problem doesn't exist. Debug->Simulate Background Fetch does its job. It even works on a simulator.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!