火力基地 |远程通知不显示,swift 3.0

Posted

技术标签:

【中文标题】火力基地 |远程通知不显示,swift 3.0【英文标题】:Firebase | Remote Notification does not show up, swift 3.0 【发布时间】:2016-11-02 19:20:23 【问题描述】:

我遇到了一个问题,即 Firebase 远程通知不显示。我的通知在 Target -> 功能中启用,并且还安装了 Firebase。在 Firebase 网站上,当我尝试发送通知时,它会立即关闭。收到 0 个设备。

这是我的代码:

    import UIKit
    import UserNotifications

    import Siren

    import Firebase
    import FirebaseDatabase
    import FirebaseInstanceID
    import FirebaseMessaging

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate 

        var window: UIWindow?

        override init() 
            super.init()
            FIRApp.configure()
            FIRDatabase.database().persistenceEnabled = true
        

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

    //
    let notificationSettings = UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil)

    UIApplication.shared.registerUserNotificationSettings(notificationSettings)

    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(notificationSettings)


    return true

这就是它在 Firebase 中显示的内容:

如果您需要任何其他信息,请尽管询问。

感谢您的帮助。

更新(我添加了一些代码):

appDelegate:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // Print message ID.
        print("Message ID: \(userInfo["gcm.message_id"]!)")
        // Print full message.
        print("%@", userInfo)
    
    // [END receive_message]
    // [START refresh_token]
    func tokenRefreshNotification(_ notification: Notification) 
        if let refreshedToken = FIRInstanceID.instanceID().token() 
            print("InstanceID token: \(refreshedToken)")
        
        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    

didFinishLaunchingWithOptions:

// Add observer for InstanceID token refresh callback.
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)

其他代码:

// [START connect_to_fcm]
    func connectToFcm() 
        FIRMessaging.messaging().connect  (error) in
            if (error != nil) 
                print("Unable to connect with FCM. \(error)")
             else 
                print("Connected to FCM.")
            
        
    
    // [END connect_to_fcm]
    func applicationDidBecomeActive(_ application: UIApplication) 
        connectToFcm()
    
// [START disconnect_from_fcm]
func applicationDidEnterBackground(_ application: UIApplication) 
    FIRMessaging.messaging().disconnect()

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,就我而言,解决方案是从info.plist 文件中删除FirebaseAppDelegateProxyEnabled 行。但是我也没有看到您正在使用didReceiveRemoteNotification 和观察者。

将此也添加到您的appDelegate

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        // Print message ID.
        print("Message ID: \(userInfo["gcm.message_id"]!)")
        // Print full message.
        print("%@", userInfo)
    
    // [END receive_message]
    // [START refresh_token]
    func tokenRefreshNotification(_ notification: Notification) 
        if let refreshedToken = FIRInstanceID.instanceID().token() 
            print("InstanceID token: \(refreshedToken)")
        
        // Connect to FCM since connection may have failed when attempted before having a token.
        connectToFcm()
    

而且我也没有在didFinishLaunchingWithOptions看到观察者:

// Add observer for InstanceID token refresh callback.
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)

您是否也可以对此进行测试,以便查看用户是否连接到云消息传递:

// [START connect_to_fcm]
    func connectToFcm() 
        FIRMessaging.messaging().connect  (error) in
            if (error != nil) 
                print("Unable to connect with FCM. \(error)")
             else 
                print("Connected to FCM.")
            
        
    
    // [END connect_to_fcm]
    func applicationDidBecomeActive(_ application: UIApplication) 
        connectToFcm()
    
// [START disconnect_from_fcm]
func applicationDidEnterBackground(_ application: UIApplication) 
    FIRMessaging.messaging().disconnect()

另外,您能否在发送通知后说出 Firebase 控制台中的状态。

【讨论】:

添加了所有内容。它显示每个打开的“已连接到 FMC”。但是,如果我尝试发送通知,它仍然没有出现,并且 Firebase 没有响应(即时完成)你能帮我吗?谢谢大佬! 您是否像我说的那样从 info.plist 中删除了该行?您确定在设置中允许通知吗? FirebaseAppDelegateProxyEnabled 已删除,如您所说。通知在 Targets -> 'AppName' -> Capabilities -> Push Notifications 设置为 ON!

以上是关于火力基地 |远程通知不显示,swift 3.0的主要内容,如果未能解决你的问题,请参考以下文章

为啥从 firebase 控制台发送的通知能够绕过 android 后台任务限制? - 反应原生火力基地 -

Swift 3 Firebase 远程通知未在 TestFlight 上显示

我的 Array 的 IndexPath 有问题吗?迅速 4 火力基地

如何解决“此广告系列针对 0 个应用用户(0% 的潜在用户)”。 (火力基地)(安卓)

远程通知中的自定义声音 iOS 10,swift 3

Swift:如何从服务器接收远程通知?