首页 > 解决方案 > IOS Firebase 推送通知在后台模式下不起作用

问题描述

我实现了firebase通知,当我的设备处于前台时,通知也可以通过firebase控制台和我的后端服务器正常工作。但是在后台模式的情况下,当我通过控制台发送通知时它工作正常,但是当它通过我的后端服务器发送时它不起作用也尝试使用推送尝试等在线工具但不起作用。

我在 Firebase 控制台中设置了身份验证密钥。在我的应用程序委托文件中,代码如下..

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      
        FirebaseApp.configure()
        ....
        if #available(iOS 10.0, *) {
           // For iOS 10 display notification (sent via APNS)
           UNUserNotificationCenter.current().delegate = self
       //    Messaging.messaging().delegate = self
           
           let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
           UNUserNotificationCenter.current().requestAuthorization(
               options: authOptions,
               completionHandler: {_, _ in })
       } else {
           let settings: UIUserNotificationSettings =
               UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
           application.registerUserNotificationSettings(settings)
       }
    Messaging.messaging().delegate = self
    application.registerForRemoteNotifications()
return true
   }

我添加的另一种方法

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("receive new data from didReceiveRemoteNotification")
        print(userInfo)
        completionHandler(.newData)
    }
    

//MARK:Messaging delegate
extension AppDelegate : MessagingDelegate {
  
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(String(describing: fcmToken))")
        user.fcmToken = fcmToken
        user.firebaseToken = fcmToken
        print("user token >>>>>  \(user.fcmToken)")
    }

extension AppDelegate : UNUserNotificationCenterDelegate {
    
  
    // Receive displayed notifications for iOS 10 devices.
      func userNotificationCenter(_ center: UNUserNotificationCenter,
                                  willPresent notification: UNNotification,
                                  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          let userInfo = notification.request.content.userInfo
          
          // Print full message.
          print("user_info---",userInfo)
          
          // Change this to your preferred presentation option
        completionHandler([.alert,.sound])
      }
    
  
    //--- one
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
            let userInfo = response.notification.request.content.userInfo
            
            print("user_info---",userInfo)
            print("tap on on forground app",userInfo)
            completionHandler()
        }
    
}

我在 xcode 中的功能部分

在此处输入图像描述

通知负载:

{"to":"mytoken","data":{"aps":{"alert":{"title":"Portugal vs. Denmark","body":"great match!"},"badge":1}},"priority":"high"}

标签: iosswiftfirebasepush-notification

解决方案


添加值为1的“内容可用”

例子:

{
    "aps" : {
        "content-available" : 1
    },
    "acme1" : "bar",
    "acme2" : 42
}

有关后台通知的更多信息,请参阅 Apple 文档:

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

ps 我在评论方面没有太多声誉,所以我会尽力帮助回答


推荐阅读