Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS background fetch clears previous notifications

It looks like no matter I return UIBackgroundFetchResultNewData or UIBackgroundFetchResultNoData from performFetchWithCompletionHandler, it clears all previous notifications (local or push) for my app. This is not acceptable for me.

Anyone knows how do I do fetch and not make previous notifications cleared (disappear)?

like image 918
Ethan Long Avatar asked Jan 20 '15 10:01

Ethan Long


People also ask

Does background fetch work when app is terminated?

The Background Fetching will NOT happen in your app after the user has killed it in the multitasking UI. This is by design.

What is background fetch in iOS?

The Background Fetch API provides a method for managing downloads that may take a significant amount of time such as movies, audio files, and software.

What is notification service extension?

A notification service app extension doesn't present any UI of its own. Instead, it's launched on demand when the system delivers a notification of the appropriate type to the user's device. You use this extension to modify the notification's content or download content related to the extension.

How do I keep apps from running in the background in Swift?

This can be done with the fetch capability mentioned in the iOS Background Execution guide. You need to include the 'Background fetch' option in your app's capabilities, and then implement the application(_:performFetchWithCompletionHandler:) method in your application delegate.


2 Answers

My guess here is that you had a background fetch that was setting your badge number to 0, if that ever happens all your notifications are cleared.

like image 198
Sam Saffron Avatar answered Sep 21 '22 15:09

Sam Saffron


Source: Apple's AppDelegate API Reference

application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

Tells the app that a remote notification arrived that indicates there is data to be fetched. Use this method to process incoming remote notifications for your app. Unlike the

application(_:didReceiveRemoteNotification:)

method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again. If the user opens your app from the system-displayed alert, the system may call this method again when your app is about to enter the foreground so that you can update your user interface and display information pertaining to the notification.

Note: You need to make sure that the app is checking for the state before settings the application's badge number to 0. Test the below cases:

  • Application running, receives a notification

  • Application terminated, receives a notification

  • Launch app directly from app icon

  • Launch app from notification received

Knowing the difference between the 3 approached below is essential in your use case.

  1. Checking the notification object received in application(_:didReceiveRemoteNotification:)

  2. Checking the notification object received in application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

  3. Checking the didFinishLaunchingWithOptions for UIApplicationLaunchOptionsRemoteNotificationKey

like image 22
TechSeeko Avatar answered Sep 20 '22 15:09

TechSeeko