如果应用程序终止,则不会收到 iOS FCM 推送通知

标签 ios swift firebase apple-push-notifications firebase-cloud-messaging

我正在开发一个 Swift 项目并将 FCM 集成到其中。我能够在应用程序运行时以及应用程序处于后台状态时接收推送通知。但有时当我终止(强制关闭)应用程序,然后从控制台发送通知时,没有显示任何通知。

我在 iOS 10 上工作并在 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") 
            }
      }
}

我还实现了 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()
}

只有当应用程序从最近使用的应用程序抽屉中强制关闭时才会发生。请调查一下。任何帮助将不胜感激。

最佳答案

解决了!!!

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

关于如果应用程序终止,则不会收到 iOS FCM 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43068408/

相关文章:

ios - 如何在 Alamofire 请求 swift 3 中传递 JSON 对象

ios - 发布到服务器,将 ASIFormDataRequest 替换为 AFnetworking

iOS:MaterialComponents 阴影

ios - 如何从 Today Widget 打开特定的 View Controller?

swift - 通过 Enum、Struct 或其他方式处理本地化字符串?

java - 在 Firebase 上上传多个图像 - Android Studio

java - 使用 Firebase 实时数据库的 SearchView 需要很长时间才能提供结果,而且价格昂贵

ios - 如何使用FBSDKLoginManager进行FB登录?

node.js - 通过条件将 Firebase 云消息传递 (FCM) 发送到多个主题的正确语法是什么

ios - 重写商店中已有的 IOS 应用程序