ALL BUSINESS
COMIDA
DIRECTORIES
EDUCATIONAL
ENTERTAINMENT
FASHION TIPS
FINER THINGS
FREE CREATOR TOOLS
HEALTH
MARKETPLACE
MEMBER's ONLY
MONEY MATTER$
MOTIVATIONAL
NEWS & WEATHER
TECHNOLOGIA
TELEVISION NETWORKS
USA VOTES 2024
VIDEOS
INVESTOR RELATIONS
IN DEVELOPMENT
Posted by - Latinos MediaSyndication -
on - August 3, 2023 -
Filed in - Technology -
-
379 Views - 0 Comments - 0 Likes - 0 Reviews
I am trying to send a notification to an iOS app using Firebase Cloud Massaging. But I have problems making it play the notification sound on an iOS 16 device when the app is in background. Here is the payload I send for my notification
const message: Message = { apns: { headers: { "apns-priority": "10", }, payload: { "aps": { "alert" : { "title" : "Rise and Shine", "body" : "Time to wake up! Get up and move!" }, contentAvailable: true, sound: "default" } } }, data: { value: '850', }, // Get token from db token: await getFCMToken() }
In my iOS project I implemented MessagingDelegate and UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication .LaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() configureFCMPushNotification(application) return true } } extension AppDelegate: MessagingDelegate { func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { let token = Messaging.messaging().fcmToken print("FCM token: \(token ?? "")") if let token = token { sendTokenToBackend(token: token) } } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken } func configureFCMPushNotification(_ application: UIApplication) { 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) { granted, error in print(granted) print(error) } Messaging.messaging().delegate = self } else { let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) } application.registerForRemoteNotifications() } } @available(iOS 10.0, *) extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("log") completionHandler() } func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print("3") completionHandler([.alert, .sound, .badge]) } }
On my iOS16 device, when the app is in foreground and I send a notification all is well, the notification arrives and the default sound is played. But when the app is in background and I send a notification I don't get any sound for the notification.
I have enabled Push Notification and the required background modes (bg fetch, remote notification and bg processing)
I understand that willPresent is called only when app is in foreground. And since in that method I specify I want sound for notification it makes sense that when the app is in foreground it works.
But I couldn't find anything similar for when the app is in bg. I also double checked with a known working project and I don't know what I am missing here.