Register for remote notifications
For iOS 12 or higher display notification (sent via APNS)
I just need to add the below code, right?
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
No need if-else like the example for iOS 10:
https://github.com/firebase/quickstart-ios/blob/365e43642f2cc570df004cb16e098a76cde1e5b1/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
That’s right. I've tried this code and it works well.
For devices running iOS 10 and above, you must assign the UNUserNotificationCenter's delegate property and FIRMessaging's delegate property. For example, in an iOS app, assign it in the applicationWillFinishLaunchingWithOptions: or applicationDidFinishLaunchingWithOptions: method of the app delegate.
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.
I have this code to push app notifications.
It works well but I get this warning:
'MessagingRemoteMessage' is deprecated: FCM direct channel is deprecated, please use APNs for downstream message handling.
and this one 'appData' is deprecated
I made a research on Google but didn't find anything to fix this.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
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().delegate = self
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
// The callback to handle data message received via FCM for devices running iOS 10 or above.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
The question is whether you are simply sending display/alert notifications and background notifications? Or are you using direct channel to send some hidden data message for real time updates?
MessagingRemoteMessage is data message object sent and handled through direct channel. If you only want to push notifications, and you also don't seem to enable direct channel in your code above. You can use Apple's API UNUserNotificationCenterDelegate to handle alert message or AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler: to handle background notifications.
I'm facing this weird issue with a new project I've setup using firebase. Notifications are working, however there are two main issues I've noticed:
They never appear as a banner that pops-down when my phone is on/home screen, instead they only appear when viewing all my notifications in the notification center (swiping all the way left from home)
I can't get them to play a sound (adding a badge does indeed work).
Additionally, I tried this from both the firebase admin sdk (web, node js), and from their "Cloud Messaging" wizard - both with the same results, leading me to think its something I've set incorrectly in my project?
Here is how I register for notifications:
func requestNotificationsSystemAuth() {
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = appDelegate as UNUserNotificationCenterDelegate
Messaging.messaging().delegate = appDelegate as MessagingDelegate
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in })
UIApplication.shared.registerForRemoteNotifications()
}
Other apps are playing sound in notifications.
Any idea?? I'm losing hope!
For those who were wondering, my issue was device related weirdly enough.
I just restarted my phone, and it started working again.
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 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