0

I have a problem that Firebase Remote notifications doesn't show up. My notifications are enabled in Target -> capabilities and Firebase is also installed. On the Firebase website, when I try to send a notification, it closes instantly. Received 0 Devices.

That is my code:

    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
}

And that is what it shows in Firebase:

enter image description here

If you need any other information, just ask.

Thanks for helping.

UPDATE (I added some code):

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)

Other Code:

// [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()
user6083214
  • 63
  • 1
  • 8

1 Answers1

0

I had the same problem and in my case the solution was to remove the FirebaseAppDelegateProxyEnabled line from the info.plist file. However I also do not see that you are using didReceiveRemoteNotification and observers.

Add this also into your 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()
    }

And I also do not see the observer in didFinishLaunchingWithOptions:

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

Can you also test this so you can see if user is connected to cloud messaging or not:

// [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()

Also, can you say what the status is in Firebase console after sending notification.

Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
  • Added everything. It says every open "Connected to FMC." But if I try to send a notification, it still not appear and no response from Firebase (Insant finish) Can you help me? Thanks man! – user6083214 Nov 03 '16 at 19:10
  • Did you remove the line from info.plist like I said? Are you sure you allowed notifications in settings? – Tarvo Mäesepp Nov 03 '16 at 19:17
  • FirebaseAppDelegateProxyEnabled is removed, like you said. Notification is in Targets -> 'AppName' -> Capabilities -> Push Notifications is set to ON! – user6083214 Nov 03 '16 at 19:23