【问题标题】:Problem implementing notification using firebase in iOS在 iOS 中使用 firebase 实现通知的问题
【发布时间】:2019-10-05 01:02:20
【问题描述】:

我正在尝试使用 firebase 在我的应用上实现推送通知。我正在从 firebase 消息传递委托中获取数据,但它没有显示在通知中心。

我已经配置了从 firebase 到消息传递委托的所有内容。

   func registerForPushNotifications(application: UIApplication) {
        if #available(iOS 10.0, *) {
            let center  = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.alert,.sound]) { (granted, error) in
                if error == nil{
                    DispatchQueue.main.async(execute: {
                        application.registerForRemoteNotifications()
                    })
                }
            }
        }
        else {

            let settings = UIUserNotificationSettings(types: [.alert,.sound], categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        }



    }

【问题讨论】:

    标签: ios swift iphone firebase


    【解决方案1】:

    如果您使用较新的 UNUserNotifications,则需要正确实现此method,并为 UserNotifications 框架提供委托。

    如果您未在前台实现此委托并且您的应用程序 ia,则默认行为是静默通知。这意味着您不会收到任何视觉通知,但您会在该方法中获得通知数据。如果您在后台,无论您是否实现了该方法,您的通知都会正确显示。

    【讨论】:

    • 我使用了以下两种方法:``` func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification .request.content.userInfo NSLog("[UserNotificationCenter] applicationState: willPresentNotification: (userInfo)") //TODO: 处理前台通知completionHandler([.alert]) } ``
    • func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo NSLog("[UserNotificationCenter ] applicationState: didReceiveResponse: (userInfo)") //TODO: 处理后台通知completionHandler() }
    • 当从 firebase 接收到任何数据时,这些方法不会被调用。
    • 我在 func 消息中获取数据(_ 消息:消息,didReceive remoteMessage:MessagingRemoteMessage){ print("收到的数据消息:(remoteMessage.appData)") }
    • 你在使用 firebase swizzling appdelegate 吗?
    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多