簡體   English   中英

IOS Firebase 推送通知在后台模式下不起作用

[英]IOS Firebase Push notification not working in background mode

我實現了 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"}

添加值為1的“內容可用”

例子:

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

有關后台通知的更多信息,請參閱 Apple 文檔:

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

ps 我在評論方面沒有太多聲譽,所以我會盡力幫助回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM