Swift3 - Background Fetch Crash on Debug - ios

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

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.

Silent APN Notification not working for Firebase IOS Auth

I'm currently trying to implementation Auth for iOS using Firebase. I've gotten it to work using the recaptcha method specified here: https://firebase.google.com/docs/auth/ios/phone-auth but still running into issues with the silent APN. I uploaded my APN key to the firebase project and added push notification to my swift project capabilities. I also added the following piece of code to my AppNameApp.swift file:
class Appdelegate : NSObject,UIApplicationDelegate{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
UIApplication.shared.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
}
}
I'm still unable to get it to work and I've been stuck for a few days now. Any help will be greatly appreciated!

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?

Background fetch never fires without Xcode

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)
}
}

Catch push notification text on lock screen

How can I get the push notification text when the app does not run or in lock screen?
I did try:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
println(userInfo)
// or just
println("something")
}
But it doesn't print me anything. Is it wrong function???
didReceiveRemoteNotification function only called this condition.
while app will running and app in foreground.(alert not showing)
if app in background and click notification on home screen
if app not run in device didReceiveRemoteNotification function not called,
in this case user click notification in home screen, we identify app launched by notification or not in didFinishLaunchingWithOptions
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
you get userinfo data in localNotif.userinfo
Try in Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var userinfo : NSDictionary =launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
print(launchOptions);
return true
}

Resources