【问题标题】:iOS FCM push notification not received if app is terminated如果应用程序终止,则未收到 iOS FCM 推送通知
【发布时间】:2017-03-28 11:30:54
【问题描述】:

我正在开发一个 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()
}

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

【问题讨论】:

  • 当您的应用终止时,通知不会发送给您的应用委托。如果通知包含警报负载,则会显示。
  • 谢谢@Paulw11。但我已经在警报中发送数据。仍然没有显示通知。
  • 是前台接收吗?
  • 是的@GaneshKumar。在前台模式下,我可以收到通知。
  • 向我们展示完整的appdelegate 文件

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


【解决方案1】:

解决了!!!

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

【讨论】:

  • 这不能解决应用程序终止的情况
  • 您是否尝试过 FCM 控制台发送消息?
  • 如果是接收消息,那么您必须更改服务器端以在后台接收通知
  • OP 指定应用状态为terminated 而不是background
  • 用控制台检查。如果可行,则在通知服务器文件的数据中添加 content_available: true, mutable_content: true
猜你喜欢
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 2021-02-11
  • 2021-01-22
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多