【问题标题】:IOS Firebase Push notification not working in background modeIOS Firebase 推送通知在后台模式下不起作用
【发布时间】:2021-05-02 03:40:18
【问题描述】:

我实现了 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"}

【问题讨论】:

  • 您是否在功能中启用了后台模式下的远程通知
  • 是的@Anbu.Karthik
  • 您的通知负载是否有标题和正文?还是仅数据推送?
  • @DimaRostopira {"to":"mytoken","data":{"aps":{"alert":{"title":"Portugal vs. Denmark","body":"很棒的比赛!"},"徽章":1}},"priority":"high"}
  • 您说“我通过控制台发送通知它工作正常,但是当它通过我的后端服务器发送时它不工作”。也许后端代码中的错误?你能添加一个sn-p吗?

标签: ios swift firebase push-notification


【解决方案1】:

添加 "content-available" 值为 1

例子:

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

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

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

p.s 我对 cme​​ts 名气不大,所以我会尽力帮助解答

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2017-10-29
    • 2019-06-22
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多