Background fetch never fires without Xcode - ios

Background fetch works when i use Xcode's background fetch simulation. But when device is not connected to Xcode, background fetch not calls for about many hours of testing.
I enabled capabilities, my app is not terminated. Any suggestions?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let request = WeatherRequest(url: weatherUrl(for: "city"))
request.perform{result in
// show data in view controller
completionHandler(.newData)
}
}

Related

How to configure Pusher Beams SDK with Firebase in App Delegate?

I have the Firebase SDK in my project for the backend and I am using the Beams SDK from Pusher to configure push notifications.
The issue is that, it doesn't seem that my app delegate is configuring the settings I need to send notification with the Beams SDK if I have Firebase. Here is what I mean.
According to the Beams SDK I need to place this code in the didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.pushNotifications.start(instanceId: "MyInstanceID")
self.pushNotifications.registerForRemoteNotifications()
try? self.pushNotifications.addDeviceInterest(interest: "debug-hello")
// FirebaseApp.configure() . However it ONLY works properly when this is commented out
return true
}
As I showed above, it only configures my notifications properly if I comment out the Firebase configuration. Here is other relevant code in my delegate:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("RECEIVED NOTIFICATION")
self.pushNotifications.handleNotification(userInfo: userInfo)
if Auth.auth().canHandleNotification(userInfo){
completionHandler(.noData)
return
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Did register for remote notifications")
self.pushNotifications.registerDeviceToken(deviceToken)
Auth.auth().setAPNSToken(deviceToken, type: .prod)
}
How do I properly configure both in my project?
The issue is that Firebase.configure() will break the didRegisterForRemoteNotificationsWithDeviceToken method.
You need to add GoogleUtilitiesAppDelegateProxyEnabled to the info.plist and set it to NO.

3D-Touch Home Screen Quick Action do not work in cold start

(iOS 13)
It is worked when app from suspend to active in hot start.
ShortcutItem will be caught in following method:
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void)
But when I force close and restart the app, then 3D-Touch Home Screen Quick Action does not work.
I can not catch the shortcutItem user tapped in following method:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool
When I try to log the shortcutItem in launchOptions, it's nil.
Please tell me where is wrong?

Appdelegate print() not working

I want to open my iOS app from and URL but when I put NSURL into fund application print() doesn't working.
please refer here
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var openUrl:NSURL? //This is used when to save state when App is not running before the url trigered
func application(_ application: UIApplication, openURL url: NSURL, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//print("Host: \(url.host!)")
//let url = url.standardized
//NotificationCenter.default.post(name: NSNotification.Name(rawValue: "HANDLEOPENURL"), object:url!)
print("bbb")
return true
}
}
There is no AppDelegate method with that signature, so its not getting called
func application(_ application: UIApplication, openURL url: NSURL, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
The correct method signature is:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool`
You are confusing it with this method
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
EDIT
To respond to links from the web to iOS, use
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {

Swift3 - Background Fetch Crash on Debug

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
print(UIApplicationBackgroundFetchIntervalMinimum)
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(.newData)
}
This is my code and when I Debug in XCode > Simulate background fetch my app crash without reason. I activated in Capabilities>Background modes>Background fetch... any ideas?
It crash only on iPhone in simulator I don't have problems.
As per your above description, I tried it in my project but it works fine and performFetchWithCompletionHandler is working in background also.
First I try without adding this property, but performFetchWithCompletionHandler not called. So,Try to add this in info.plist file
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
It will solve my problem

Swift iOS: How do I handle remote notification when app is not running

How do I handle a push remote notification when the app is not running. Currently, when remote notification comes in, and my app is in foreground or background, my code is correctly handling the remote notification. But when the app is not running, and remote notification come in, I see a remote banner on top of screen, and when I tapped on it, my app get launch and go away. It doesn't seem to handle the remote notification correctly. Is this how iOS behave, or is there a way to handle it. Thanks. This is currently my code in AppDelegate.swift.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
...
println("..... application didFinishLaunchingWithOptions()")
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
// there is a notification...do stuff...
println("dinFInishLaunchingWithOption().. calling didREceiveRemoteNotification")
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
println("didRegisterForRemoteNotificationsWithDeviceToken")
}
fun application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)
{
println(error)
println(error.localizedDescription)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
println("..... application didReceiveRemoteNotification()")
}

Resources