简体   繁体   English

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

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

I implement firebase notification and when my device is in foreground notification works fine through firebase console and my backend server too.我实现了 firebase 通知,当我的设备处于前台时,通知通过 firebase 控制台和我的后端服务器也可以正常工作。 But in case of background mode my when I send notification through console its working fine but when its sending through my backend server it's not working also try with online tools like push try but not working.但是在后台模式的情况下,当我通过控制台发送通知时它工作正常,但是当它通过我的后端服务器发送时它不起作用也尝试使用推送尝试等在线工具但不起作用。

I set auth key in firebase console.我在 firebase 控制台中设置了身份验证密钥。 and in my app delegate file code is following..在我的应用程序委托文件中,代码如下..

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
   }

another method I added我添加的另一种方法

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

my capabilities section in xcode我在 xcode 中的功能部分

在此处输入图像描述

Notification payload:通知负载:

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

Add "content-available" with value 1添加值为1的“内容可用”

Example:例子:

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

For more information about background notifications, see the Apple documentation:有关后台通知的更多信息,请参阅 Apple 文档:

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

ps I don't have much reputation for comments, so I'll try to help with the answers ps 我在评论方面没有太多声誉,所以我会尽力帮助回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM