It is Doable utilizing Silent Push notifications ,you may this reply.
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var dataManager = DataManager()
var reloadSign = false;
let gcmMessageIDKey = "gcm.message_id"
func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Material.with([Crashlytics.self])
// Override level for personalisation after software launch.
IQKeyboardManager.shared.allow = true
DropDown.startListeningToKeyboard()
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.present().delegate = self as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
// //Solicit permission from person to obtain notifications
UNUserNotificationCenter.present().requestAuthorization(choices: authOptions) { (_, error) in
guard error == nil else{
print(error!.localizedDescription)
return
}
}
//
// //get software occasion ID
InstanceID.instanceID().instanceID { (outcome, error) in
if let error = error {
print("Error fetching distant occasion ID: (error)")
} else if let outcome = outcome {
print("Distant occasion ID token: (outcome.token)")
}
}
software.registerForRemoteNotifications()
return true
}
func software(_ software: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
let proj = Challenge()
proj.checkData()
}
// Print full message.
print(userInfo)
}
func software(_ software: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for distant notifications: (error.localizedDescription)")
}
func applicationWillResignActive(_ software: UIApplication) {
// Despatched when the applying is about to maneuver from lively to inactive state. This could happen for sure forms of non permanent interruptions (reminiscent of an incoming cellphone name or SMS message) or when the person quits the applying and it begins the transition to the background state.
// Use this methodology to pause ongoing duties, disable timers, and invalidate graphics rendering callbacks. Video games ought to use this methodology to pause the sport.
}
func applicationDidEnterBackground(_ software: UIApplication) {
// Use this methodology to launch shared assets, save person information, invalidate timers, and retailer sufficient software state info to revive your software to its present state in case it's terminated later.
// In case your software helps background execution, this methodology is named as an alternative of applicationWillTerminate: when the person quits.
}
func applicationWillEnterForeground(_ software: UIApplication) {
// Referred to as as a part of the transition from the background to the lively state; right here you may undo most of the adjustments made on getting into the background.
}
func applicationDidBecomeActive(_ software: UIApplication) {
// Restart any duties that have been paused (or not but began) whereas the applying was inactive. If the applying was beforehand within the background, optionally refresh the person interface.
}
func applicationWillTerminate(_ software: UIApplication) {
// Referred to as when the applying is about to terminate. Save information if applicable. See additionally applicationDidEnterBackground:.
let ud = UserDefaults.customary
ud.set( true, forKey: "isTerminated");
ud.synchronize()
}
func crashlyticsDidDetectReport(forLastExecution report: CLSReport, completionHandler: @escaping (Bool) -> Void) {
completionHandler(true)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate{
func userNotificationCenter(_ middle: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content material.userInfo
// With swizzling disabled it's essential to let Messaging know in regards to the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
// Print full message.
print(userInfo)
// Change this to your most well-liked presentation choice
completionHandler([])
}
func userNotificationCenter(_ middle: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content material.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
// Print full message.
print(userInfo)
completionHandler()
}
}
extension AppDelegate: MessagingDelegate{
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: (fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.put up(title: Notification.Identify("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If crucial ship token to software server.
// Notice: This callback is fired at every app startup and at any time when a brand new token is generated.
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Acquired information message: (remoteMessage.appData)")
}
}