I am trying to implement push notification via Firebase Cloud Messaging to my iOS app. I can set the firebase console and APN perfectly, I can get the notification that sent via Firebase console in my device.
but, when I get the notification, it just shows the alert, no sound, no number in the badge, even though I have stated UNAuthorizationOptions = [.alert, .badge, .sound]
here is the code I use in the app delegate
import UIKit
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
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 as? UNUserNotificationCenterDelegate
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()
Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
print("Firebase registration token: \(fcmToken)")
}
}
I also set "FirebaseAppDelegateProxyEnabled"
to YES in my Info.plist. and here is my podfile
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'Firebase Push Notification' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for Firebase Push Notification
pod 'Firebase/Core'
pod 'Firebase/Messaging'
end
so how to add the sound and the badge when I receive the notification?
You need to tell your service/backend owner to send payload for the notification similar to this one. Basically you need to have the badge
and sound
keys for it to work as you expect:
{
"aps":{
"alert":"This is a message",
"badge":1,
"sound":"default"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With