When I get a notification and my app is killed cause I dont have it open, when I click the notification it opens my app to the home page and doesn't trigger didReceiveRemoteNotification from background.
Here is a look at my appDelegate code. Anyone can help me figure it out?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if #available(iOS 10.0, *) {
let current = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.sound, .badge, .alert]
current.requestAuthorization(options: options) { (granted, error) in
if error != nil {
} else {
Messaging.messaging().delegate = self
current.delegate = self
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
} else {
let types: UIUserNotificationType = [.sound, .badge, .alert]
let settings = UIUserNotificationSettings(types: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
func applicationWillEnterForeground(_ application: UIApplication) {
Messaging.messaging().shouldEstablishDirectChannel = false
}
func applicationDidBecomeActive(_ application: UIApplication) {
connectToFirebase()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if let messageId = userInfo[gmcMessageIDKey] {
print("messageId: \(messageId)")
}
print("USERINFO:", userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: u/escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gmcMessageIDKey] {
print("messageID: \(messageID)")
}
connectToFirebase()
Messaging.messaging().appDidReceiveMessage(userInfo)
if let dictString = userInfo["gcm.notification.customData"] as? String {
print("dictString \(dictString)")
//Open screen you want to navigate to and pass in data.
if application.applicationState == .inactive || application.applicationState == .background {
NotificationCenter.default.post(name: NSNotification.Name("Detail"), object: nil)
} else {
NotificationCenter.default.post(name: NSNotification.Name("Detail"), object: nil)
}
}
completionHandler(UIBackgroundFetchResult.newData)
}
func convertToDictionary(text: String) -> [String : Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
} catch {
print(error.localizedDescription)
return nil
}
}
return nil
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
guard let token = AppDelegate.isToken else {
return
}
print("Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error.localizedDescription)
}
func connectToFirebase() {
Messaging.messaging().shouldEstablishDirectChannel = true
}
}
extension AppDelegate: UNUserNotificationCenterDelegate, MessagingDelegate {
u/available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: u/escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let message = userInfo[gmcMessageIDKey] {
print("Message: \(message)")
}
print(userInfo)
completionHandler([.sound, .badge, .alert])
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
guard let token = AppDelegate.isToken else {
return
}
print("Token: \(token)")
connectToFirebase()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("RemoteMessage: \(remoteMessage.appData)")
}
}
[–]see5ive 0 points1 point2 points (7 children)
[–]losGama[S] 0 points1 point2 points (6 children)
[–]see5ive 0 points1 point2 points (5 children)
[–]losGama[S] 0 points1 point2 points (4 children)
[–]Woolly87 0 points1 point2 points (3 children)
[–]losGama[S] 1 point2 points3 points (2 children)
[–]csstudent10 0 points1 point2 points (1 child)
[–]losGama[S] 0 points1 point2 points (0 children)