FCM push notification error in swift 2.0 - ios

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 //ERROR THIS LINE
}
else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
Getting error when set the delegate to self.
"FIRMessing has no member remoteMessageDelegate"

It looks like the Google documentation is out of date.
Please run the following commands in your terminal:
pod repo update
Then go to your project folder and run
pod update
(please mark this as the solution if this helped you)

Try this:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate{
}
//MARK: FIRMessaging Delegate
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage){
}

Related

How to access AppDelegate object and call method from other SwiftUI views

I am working with the SwiftUI app and I configured my Push Notification setup in AppDelegate and it's working fine like below:
Messaging.messaging().delegate = self
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()
Now I have to move this code from app launch to TabBarView screen where I can request for permission once login success.
So for that, I have created func in AppDelegate file like this:
func enablePushNotification() {
let application = UIApplication.shared
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()
}
and calling it from TabBarView() like this:
init() {
print("TabBar Init called")
configurePushNotification()
}
func configurePushNotification() {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.enablePushNotification()
}
}
But I am not able to get the AppDelegate object here and It's unable to call appDelegate method from TabBarView().
Any suggestion here why I am not able to get the AppDelegate object or is there any other way to get it?

How to deal with Could not cast Appdelegate to FIRMessagingDelegate

if #available(iOS 10.0, *)
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM
Messaging.messaging().delegate = (self as! MessagingDelegate)
Couldn't cast value from Appdelegate to FIRMessagingDelegate
You need conform to the protocol in class line
class AppDelegate: UIResponder, UIApplicationDelegate,FIRMessagingDelegate {
as this cast (self as! MessagingDelegate) will crash , then replace it with
Messaging.messaging().delegate = self

how to place Notification Authorization Request for FCM outside AppDelegate?

when we want to implement Firebase Cloud Messaging, we first have to ask permission for notification to the user. from Firebase Documentation they recommend this lines of codes to be implemented in Appdelegate on didFinishLaunchingWithOptions like below :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
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()
return true
}
but if place those codes in appDelegate, the pop up alert to ask permission will show up immediately after the user open up my app.but I want to show the alert in the certain View Controller.
if I move those code to a VC, then the application is not available, since the application comes from didFinishLaunchingWithOptions. and then the messagingDelegate ... I am not sure. could you please help me to rewrite those codes but if it is not in AppDelegate?
Add a Class
import Foundation
import FirebaseMessaging
import FirebaseInstanceID
import UserNotifications
class Notifications {
private init() { }
static let shared = Notifications()
func requestNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isAuthorized, error) in
if error != nil {
print(error as Any)
return
} else {
print("set up successfully")
// Completion Code Here:
}
}
}
}
Add to your prefered VC
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Notifications.shared.requestNotifications()
}
I was testing on a iOS 10.3.3 device and it didn't register until I set the delegate of the UNUserNotificationCenter to UIApplication.shared.delegate as? UNUserNotificationCenterDelegate implementing delegate in the AppDelegate as an empty delegate. But it didn't work implementing an empty delegate in the current view controller.
AppDelegate.swift:
extension AppDelegate: UNUserNotificationCenterDelegate { }
So here's the method in my view controller:
private func registerPushNotifications() {
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as? UNUserNotificationCenterDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {granted, _ in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
})
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}
Of course you have to leave FirebaseApp.configure() in didFinishLaunchingWithOptions method.
Later edit: You might get a warning about calling registerForRemoteNotifications() from the main thread, so I adjusted the code to do that.

Use of undeclared type UNAuthorizationOptions

I am trying to use Firebase to handle push notifications. I have installed Firebase pod ('Firebase/Core' and 'FirebaseMessaging' pods).
And after I imported Firebase to the project
import Firebase
I have configured the Firebase app like this( code is copied from official docs ):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {FIRApp.configure() }
After that I've tried to use this code ( code is copied from official docs ):
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(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
But I got the error from the title which says:
Use of undeclared type UNAuthorizationOptions
also I am having the same error related to the UNUserNotificationCenter class.
I am using Swift 2.2 and Xcode 7.3.1
What is the cause of this error?
you need to import UserNotifications before calling those framework. And what Nirav D said is true, it is a new framework in iOS 10, should also remember to select the correct deployment target.
UserNotifications.framework is available from iOS 10 and you are working with Xcode 7.3 means with iOS 9 and lower, So there is no need for you to add that if #available(iOS 10.0, *) {, write only else part directly and register remote notifications.
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

Does Firebase 3.7.0 support in iOS 7 and 8

I am using Firebase in my app. I have installed via the Cocoapods. To make support for push notification in iOS 10 I have updated the cocoa pods with the latest version of Firebase 3.7.0.
Please let me know does it support in iOS 7 and iOS 8.
Registeration of push notification like this
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
authOptions,
completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
Thanks

Resources