Thomas.O
Thomas.O

Reputation: 45

iOS Push Notifications doesn't work with Firebase

I use Firebase to send Notification with my iOS app, I have followed all step in documentation :

When I try to send a Notification there isn't no problem found in Firebase, for one particular user or for all iOS device. But none of my device (real device obviously) receive notification.

I use the good Bundle, I enable notification in my application and there is the code in my AppDelegate :

import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window: UIWindow?

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


    UINavigationBar.appearance().barTintColor = Design.blue_es
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    UINavigationBar.appearance().tintColor = UIColor.white


    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().remoteMessageDelegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()

    return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
    print(remoteMessage.appData)
}

// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Print the error to console (you should alert the user that registration failed)
    print("APNs registration failed: \(error)")
}

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}
}

Upvotes: 1

Views: 2106

Answers (2)

Jaydeep Paghdar
Jaydeep Paghdar

Reputation: 31

IN Swift 3

  1. Configuring Your Apple Developer Account

  2. Generating a CSR file

  3. Uploading Your CSR File

  4. Preparing the APNs Certificate

  5. Configuring Firebase for Push Notifications

  6. Building the Firebase Notification App

  7. Installing the Firebase SDK Using CocoaPods

  8. Adding GoogleService-Info.plist

  9. Enabling Push Notifications

  10. Initializing Push Notifications

  11. AppDelegate of your Project

    import UIKit
    import SVProgressHUD
    import UserNotifications
    import Firebase
    import FirebaseInstanceID
    import FirebaseMessaging
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate, MessagingDelegate {
    
        var window: UIWindow?
    
    
        static var appDelegate:AppDelegate!
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    
            if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self
                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
                // For iOS 10 data message (sent via FCM
                Messaging.messaging().remoteMessageDelegate = self
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }
    
            application.registerForRemoteNotifications()
    
            FirebaseApp.configure()
    
            return true
    
        }
    
        func application(received remoteMessage: MessagingRemoteMessage) {
            print(remoteMessage.appData)
        }
    
        func application(application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
            Messaging.messaging().apnsToken = deviceToken as Data
        }
    

    }

Upvotes: 1

Bhavi Lad
Bhavi Lad

Reputation: 247

I think first configure for firebase

FirebaseApp.configure()

after that you should call registerForRemoteNotifications()

Upvotes: 0

Related Questions