How to receive remote notifications with Firebase

0

I'm trying to get notifications with firebase. I'm sending them with Cloud messaging from Firebase. Send the message but it does not appear in the notification tray.

Doing tests and implementing the following method in the AppDelegate if you receive the message but not as background notification which is what I want to do.

public func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage){
    print(remoteMessage.appData)
    //let title = remoteMessage.appData[("data"): {"title"}]
    let title = "New Request"

    for value in remoteMessage.appData{
        if let value = value as?  [AnyHashable:Any]{
            print(value)
        }
    }

    let message = "okay"
    print(message)
    print(title, message)

}

What am I doing wrong? Why do not the notifications appear in the notification tray?

    
asked by Popularfan 23.05.2018 в 17:03
source

1 answer

1

I do not know well the steps that you have followed to activate the notifications, I mention them quickly the sequence to follow, to locate if any of them has been missing:

Create the authentication key

  • In the Apple developer account, go to Certificates, Identifiers & Profiles, and in Keys select All.

  • Click on the add (+) button in the upper right corner.

  • Enter a description for the key.

  • In Key Services, select the APNs checkbox and click Continue.

  • Click Confirm and then Download. Keep the key in a safe place.

  • Install the authentication key in firebase

  • In the firebase project, select the gear icon and click Settings.
  • Go to Cloud Messaging
  • In the iOS setup in the APN Authentication Key, select Upload.
  • Configure the AppID

  • Go to your developer account - > App IDs and select the corresponding AppID, click Edit
  • In the services select the Push Notifications checkbox.
  • Click Done.
  • Add notifications to your project

  • In XCode enable push notifications in App> Capabilities. If you mark an error in the provisioning profile, download it again because it was updated with the notification features.
  • Install the SDK

  • Add the pods to install in the PodFile as follows: pod 'Firebase / Core'pod' Firebase / Messaging ' Run pod install Close and open the xcode project again.
  • In the AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Use Firebase library to configure APIs
        FirebaseApp.configure()
    
        //TODO Confirm if this is the right point to initialize Push Notifications
        // [START set_messaging_delegate]
        Messaging.messaging().delegate = self
        // [END set_messaging_delegate]
    
        // Register for remote notifications. This shows a permission dialog on first run, to
        // show the dialog at a more appropriate time move this registration accordingly.
        // [START register_for_notifications]
        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})
        } else {
            // iOS 9 or before
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
    
        application.registerForRemoteNotifications()
        // [END register_for_notifications]
    
    
        return true
    }
    
    @available(iOS 10, *)
    extension AppDelegate : UNUserNotificationCenterDelegate {
    
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
    
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID iOS10: \(messageID)")
        }
        // Change this to your preferred presentation option
        completionHandler(.alert)
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        completionHandler()
    }
    
    extension AppDelegate : MessagingDelegate {
        func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
            print("NOTIFICATION: Registration token  =>  \(fcmToken)")
    }
    
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("NOTIFICATION: Received data message => \(remoteMessage.appData)")
    }
    
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)")
    }
    

    }

        
    answered by 07.06.2018 / 01:33
    source