Ios10, Swift 3, and Fcm Delegate Error

Firebase Issues iOS 10.3 on Xcode 8.3 Swift 3 FCM notifications not working

You need to add Push notification certificate to FCM Console in Cloud Messaging. Only then It'll be able to send notifications to your App. Also, make sure you've enabled Push notifications in Capabilities in your iOS App.

Update:

extension AppDelegate: MessagingDelegate {
// Registering for Firebase notifications
func configureFirebase(application: UIApplication) {

FirebaseApp.configure()

Messaging.messaging().delegate = self

// 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 {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

print("-----firebase token: \(String(describing: Messaging.messaging().fcmToken)) ----")

}

//MARK: FCM Token Refreshed
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
// FCM token updated, update it on Backend Server
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("remoteMessage: \(remoteMessage)")
}

//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}

//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("User Info = \(response.notification.request.content.userInfo)")

completionHandler()
}
}

Call configureFirebase(application:) inside didFinishLaunchingWithOptions of your AppDelegate.

ios10, Swift 3 and Firebase Push Notifications (FCM)

Inside method didRegisterForRemoteNotificationsWithDeviceToken, add the following code:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""

for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}

FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)

print("tokenString: \(tokenString)")
}

And do not forget to enable Push Notifications inside Capabilities.

FCM + Swift 3 - Notifications not appearing

Figured it out.
p.s. im using Swift 3 syntax, you are missing the completionhandler in your willPresent method

completionHandler
The block to execute with the presentation option for the notification. Always execute this block at some point during your implementation of this method.

https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo as! [String: Any]

completionHandler([.alert, .badge, .sound])

}

Firebase Cloud Messaging AppDelegate Error

The example in Firebase docs is outdated. Here is the code for most recent Xcode 9 and Swift 3:

import Firebase
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate {

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

FIRApp.configure()

if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_,_ in })

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self
}

application.registerForRemoteNotifications()

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
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")

// Print full message.
print("%@", userInfo)

}

}

extension AppDelegate : MessagingDelegate {
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
print("%@", remoteMessage.appData)
}
}

ios10, Swift 3 and Firebase Push Notifications setup

The issue is that the sample code you're looking at is in Swift 2.3, and your project is in Swift 3.0.

There's a few ways to address this:

  1. Go ahead and use swift 2.3. You can do this by going to your Project, select Build Settings and change Use Legacy Swift Language Version to Yes.

  2. Ask Xcode to automatically update it for you. You can do this by going to Edit > Convert > To Current Swift Syntax... and it should hopefully address the issues you're seeing.

  3. Update the code manually. As you can see, Xcode generally knows what the updated code is "supposed" to be, but it looks like the method that's giving you trouble can be updated to something that looks a little like this:

    func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if #available(iOS 10.0, *) {
    let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_,_ in })

    // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.current().delegate = self
    // For iOS 10 data message (sent via FCM)
    FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
    let settings: UIUserNotificationSettings =
    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FIRApp.configure()

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

    return true
    }
  4. Bug the Firebase Sample Code people and tell them, "Hey! You forgot to update this sample to Swift 3.0!" Luckily, I'm in a position to do that, so will tell them right now. :)



Related Topics



Leave a reply



Submit