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.
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.
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.
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 using Firebase to handle notifications and once I have the notification I create a local notification (ios 9) or notification request (ios 10). All works well when I Build with Xcode and then Run, app receives notifications and it's shown.
But the problem is when I stop running the app through Xcode and I open, the notifications seems that never arrives in ios 10 version (but in ios 9 notifications are received as the first case without any problem).
Push notifications in Capabilities enabled, background modes too.
UserNotifications framework added
In didFinishLaunchingWithOptions I initialized all well:
/* NOTFICATIONS */
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
let authOptions: UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(authOptions, completionHandler: { (_, _) in })
// 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()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification(_:)), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
I have UserNotification protocols implemented.
And I receive notifications here (in ios9 and ios10 i think, This could be where I'm wrong so maybe the notifications in ios10 are not handled directly here. But in this case how it's possible that I receive the notifications when app is running through xcode):
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
if #available(iOS 10.0, *) {
UNUserNotificationCenter.currentNotificationCenter().delegate = self
} else {
// Fallback on earlier versions
}
// Print message ID.
if let messageID = userInfo["gcmMessageIDKey"] {
print("Message ID: \(messageID)")
}
let notificationJSON = JSON(userInfo)
self.presentNotification(withNotificationJSON: notificationJSON)
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.NewData)
}
By the other hand I have my file .entitlements but with development (maybe need a release ones? Or it change by herself when compile?)
Thank you so much i'm a bit frustrated, any further information you need just let me know.
EDIT1:
Maybe the problem could be in the parameters?
let notificationsParameters:[String:AnyObject] = [
"to": "iphoneID",
"content-available":true,
"priority":"high",
"notification" : [
"sound" : " "
],
"data" : [
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
]
]
I'm trying to implement push notifications into my application with Firebase, however when following their documentation I have to add the following line of code:
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in})
UNUserNotificationCenter.current().delegate = self
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
Upon adding this code, the application will not compile, and it will throw an error at me letting me know that I
cannot assign value of type AppDelegate to type
FirMessagingDelegate
and that I
cannot assign value of type AppDelegate to type
UNUserNotificationCenterDelegate.
So naturally, I add the FIRMessagingDelegate and UNUserNotificationCenterDelegate to my application, however the FIRMessagingDelegate does not allow the application to compile saying that
type AppDelegate does not conform to protocol 'FirMessagingDelegate'.
Any ideas? I haven't managed to find any other situations in which others have encountered this error, and in Google's documentation, these two delegates aren't even added.
You are on the right track, 1 more thing to do is making your AppDelegate class conform to the 'FirMessagingDelegate' protocol, by implementing the applicationReceivedRemoteMessage function somewhere is your AppDelegate class as it is required by the 'FirMessagingDelegate' protocol described here Firebase Doc