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
Related
iOS app gets firebase push notification in iOS 14 but not receive in iOS 15.
this code is working for iOS 14 any new configuration about iOS 15:
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, error in
if let error = error {
print("requestAuthorization Error: \(error.localizedDescription)")
}
}
application.registerForRemoteNotifications()
You might have to re-upload the apns token .p8 in your firebase project cloud messaging configuration. It's the second time its happened to me.
iPhone device is not receiving device token from my application. The
didRegisterForRemoteNotificationsWithDeviceToken
method is not getting called though I have the following code in the
didFinishLaunch
method.
In didFinishLaunch Method:
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
Note:
notificationSettings method is getting called. but not didRegister or didFail methods called for Remote Notifications.
I am using a iPhone 6s with iOS 9.
What need to be checked to get the device token?
You are using UNUserNotificationCenter but it only work if your device with iOS 10 or 10+.
For iOS 9 you need below code for register push notification
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
You can add condition based on iOS version like #Kamalesh answer.
try this in didFinishLaunch
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
// ios 10
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound,.badge]) { (granted, error) in
// actions based on whether notifications were authorized or not
}
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
else
{
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
}
Swift 3 IOS 10: I've been looking for code for allowing pushing notification; finally, I could find a precisely useful one from www.codementor.io. However, I came into puzzle. I wonder if the following code will work with the lower or newer version of iOS. Since Apple will be releasing its version relentlessly, how the following code will be able to handle the changes? Is there any way out to deal with the problem I mentioned?
// iOS 10 support
if #available(iOS 10, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
application.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
Yes the code for iOS 10 will work for iOS 11 as well.
Basically the #available macro checks the minimum OS so it's also safe to merge iOS 8 and 9.
//iOS 10+
if #available(iOS 10, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
if (granted) { //check if authorization was granted before registering
application.registerForRemoteNotifications()
}
}
}
// iOS 8+ support
else if #available(iOS 8, *) {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {
application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
Try this:
func setUpPushNotification(application: UIApplication) {
if #available(iOS 10.0, *) {
let notificationTypes: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: notificationTypes,
completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
else{
let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound]
let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
}
application.registerForRemoteNotifications()
}
if #available(iOS 10, *)
reads as: if iOS from Version 10 or above is available...
So if you leave your code like that and iOS 11, 12, etc is coming along they will all go into this if branch.
So your code will work as long as the Push API is not changed.
If Apple changes the API in the future (say in iOS 12) you will have to add another if-branch and re-submit you app to the store...
(If someone with iOS 9 uses your app they will end up in the second if branch - because the first one does not apply. So the order of the ifs guarantees that lower iOS version get the right code.)
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){
}
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()