Register For Remote Notifications With Firebase Outside AppDelegate

Here’s how you can ask a user to register for remote notification when a certain action has been taken, i.e. created a new account.

import Firebase
import UserNotifications
 
DispatchQueue.main.async() {
 
    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_,_ in })
 
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as! AppDelegate
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = UIApplication.shared.delegate as! AppDelegate
 
    } else {
        let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound]
        let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
        UIApplication.shared.registerUserNotificationSettings(pushNotificationSettings)
    }
 
    UIApplication.shared.registerForRemoteNotifications()
 
}
 

How To Save An Image Using DKIMage

Here’s a quick snippet to save the original image once it has been selected usingĀ DKImage.

 

self.assets![0].fetchOriginalImageWithCompleteBlock({ (image, info) in
    let phAsset = self.assets![0].originalAsset
    self.savePHAsset(asset: phAsset!){(file) -> Void in
        print(file)
    }
})
func savePHAsset(asset: PHAsset, completionHandler: @escaping ((URL) -> Void)) {
let manager = PHImageManager.default()
let options = PHImageRequestOptions()
options.version = .original
options.isSynchronous = true
 
manager.requestImageData(for: asset, options: options) { data, _, _, _ in
 
if let data = data {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
 
let image = UIImage(data: data)
self.phaAsset = UIImage(data: data)
self.imageData = UIImageJPEGRepresentation(image!,0.3) as NSData!
self.fileNombre = "\(self.currentTag!)-\(randomStringWithLength(len: 5)).jpg"
self.filename[self.currentTag!] = FileName(name: self.fileNombre)
 
do {
let nsU = URL(fileURLWithPath:"\(documentsPath)/\(self.fileNombre)")
try self.imageData.write(to: nsU, options: [])
completionHandler(nsU)
} catch {
print("Error with \(documentsPath)/\(self.fileNombre)")
}
 
}
}
}