1
votes

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

  • Apple Developer Account Configuration

  • Generating a CSR file

  • Uploading CSF file

  • Preparing the APNs Certificate

  • Configuring Firebase for Push Notifications

  • Building the Firebase Notification in my app

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)")
}
}
2
Have you uploaded P12 certificate of Push Notification certificate without key on firebase console ? - Krunal Darji
I uploaded P12 certificate, but I don't understand what you mean by "without key" - Thomas.O
when you export certificate from Keychain you can see one drop down arrow left to your Apple Push name expand it you will see Key icon - Krunal Darji
Ok, I have uploaded my P12 certificate with keychain for production and development notification, but I use the same CSR to generate them - Thomas.O

2 Answers

1
votes

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
        }
    

    }

0
votes

I think first configure for firebase

FirebaseApp.configure()

after that you should call registerForRemoteNotifications()