4

Im using swift 3. And im using Firebase for Push notifications in my app. Currently im getting push notification event app is on foreground , back ground or close the app. There i have configured a custom notification sound when a push notification comes. But it can not hear when the app is closed or screen is locked. But the notification is showing up.

Here is what i have done so far:

import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    //For FireBase Configs
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()


    FirebaseApp.configure()

    return true
}



func application(_ application: UIApplication,open url: URL,sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application,open: url as URL!,sourceApplication: sourceApplication,annotation: annotation)
}



//FireBase Notifications Begins
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {


    //Play alert sound
    utilityHelper.playAudio(fileName: "udio", fileExtenstion: "mp3")

    let json = JSON(userInfo)

    //show notification

    completionHandler(UIBackgroundFetchResult.newData)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    NSLog("show notification")
    completionHandler(UNNotificationPresentationOptions.alert)
}


func application(received remoteMessage: MessagingRemoteMessage) {


}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {



}


func application(_ application: UIApplication,
                         didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken as Data

    if let regToken = InstanceID.instanceID().token() {


    }
}
//FireBase Notifications End

}

If i enable sound from the firebase also it gives me a default sound not the custom one. How can i play the sound when app is close ?

Anushka Madushan
  • 681
  • 2
  • 12
  • 31

0 Answers0