如果app被终止,iOS FCM推送通知不会收到。

时间:2021-07-07 16:23:44

I am working on a Swift project and integrating FCM into it. I am able to receive push notification when app is running as well as when app is in background state. But sometimes when I terminate (force close) the app, then on sending notification from console, no notification is shown.

我正在做一个快速项目,并将FCM集成到其中。我可以在app运行时以及app处于后台状态时收到推送通知。但有时当我终止(强制关闭)应用程序时,在从控制台发送通知时,不会显示通知。

I am working on iOS 10 and implemented below code in didFinishLaunchingWithOptions:

我正在开发ios10,并在didFinishLaunchingWithOptions中实现如下代码:

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
     if error == nil {
            UIApplication.shared.registerForRemoteNotifications()

            if granted {
                print("Notification access true!")
            } else {
                print("Notification access false") 
            }
      }
}

I have also implemented UNUserNotificationCenterDelegate and it's methods.

我还实现了UNUserNotificationCenterDelegate及其方法。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert,.badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo

        print(userInfo)

        self.handleOnNotifClick()

        completionHandler()
}

It only happens when the app is force closed from recently used apps drawer. Please look into it. Any help would be appreciated.

只有当应用被强制关闭时,它才会发生。请看它。如有任何帮助,我们将不胜感激。

1 个解决方案

#1


1  

Solved!!!

解决了! ! !

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let state: UIApplicationState = UIApplication.shared.applicationState

    if state == .active{
        if let notification = userInfo["aps"] as? [AnyHashable: Any],
            let alert = notification["alert"] as? String {
            print(alert)
            let localNotification = UILocalNotification()
            localNotification.alertBody = alert
            localNotification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.shared.scheduleLocalNotification(localNotification)
        }
    }else if state == .inactive{

        if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String {
            print(alert)
            let localNotification = UILocalNotification()
            localNotification.alertBody = alert
            localNotification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.shared.scheduleLocalNotification(localNotification)

        }
    }else if state == .background{
        UIApplication.shared.applicationIconBadgeNumber = 0

        if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String,let sound = notification["sound"] as? String{
            print(alert)
            var localNotification = UILocalNotification()
            localNotification.alertBody = alert
            localNotification.soundName = sound
            UIApplication.shared.scheduleLocalNotification(localNotification)
        }
    }
}

#1


1  

Solved!!!

解决了! ! !

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let state: UIApplicationState = UIApplication.shared.applicationState

    if state == .active{
        if let notification = userInfo["aps"] as? [AnyHashable: Any],
            let alert = notification["alert"] as? String {
            print(alert)
            let localNotification = UILocalNotification()
            localNotification.alertBody = alert
            localNotification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.shared.scheduleLocalNotification(localNotification)
        }
    }else if state == .inactive{

        if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String {
            print(alert)
            let localNotification = UILocalNotification()
            localNotification.alertBody = alert
            localNotification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.shared.scheduleLocalNotification(localNotification)

        }
    }else if state == .background{
        UIApplication.shared.applicationIconBadgeNumber = 0

        if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String,let sound = notification["sound"] as? String{
            print(alert)
            var localNotification = UILocalNotification()
            localNotification.alertBody = alert
            localNotification.soundName = sound
            UIApplication.shared.scheduleLocalNotification(localNotification)
        }
    }
}