iOS Enable Sound for Push Notification Sound FCM











up vote
1
down vote

favorite












I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.



I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
index.js Code:



var notification = {
notification: {
title: conversation.conversationName,
body: user.username + ': ' + message.text,
sound: 'default'
},
topic: topic
}


App Delegate Code: Function called in didFinishLaunchingWithOptions.



import UIKit
import Firebase
import UserNotifications

private func attemptRegisterForNotifications(application: UIApplication) {

Messaging.messaging().delegate = self

UNUserNotificationCenter.current().delegate = self

let options: UNAuthorizationOptions = [.alert, .badge, .sound]

UNUserNotificationCenter.current().getNotificationSettings { (settings) in

if settings.authorizationStatus == .authorized {
// Push notifications already authorized, do nothing
print ("push notifications authorized")
} else if settings.authorizationStatus == .notDetermined {
// User hasn't specified notification status
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}

guard granted else {return}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
})
} else if settings.authorizationStatus == .denied {
// User has denied notifications
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in

if let error = error {
print ("Failed to request authorization:", error)
return
}

let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
alertController.preferredAction = settingsAction
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
})
}
}

}









share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.



    I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
    index.js Code:



    var notification = {
    notification: {
    title: conversation.conversationName,
    body: user.username + ': ' + message.text,
    sound: 'default'
    },
    topic: topic
    }


    App Delegate Code: Function called in didFinishLaunchingWithOptions.



    import UIKit
    import Firebase
    import UserNotifications

    private func attemptRegisterForNotifications(application: UIApplication) {

    Messaging.messaging().delegate = self

    UNUserNotificationCenter.current().delegate = self

    let options: UNAuthorizationOptions = [.alert, .badge, .sound]

    UNUserNotificationCenter.current().getNotificationSettings { (settings) in

    if settings.authorizationStatus == .authorized {
    // Push notifications already authorized, do nothing
    print ("push notifications authorized")
    } else if settings.authorizationStatus == .notDetermined {
    // User hasn't specified notification status
    UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
    if let error = error {
    print ("Failed to request authorization:", error)
    return
    }

    guard granted else {return}
    DispatchQueue.main.async {
    application.registerForRemoteNotifications()
    }
    })
    } else if settings.authorizationStatus == .denied {
    // User has denied notifications
    UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in

    if let error = error {
    print ("Failed to request authorization:", error)
    return
    }

    let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
    guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
    return
    }
    if UIApplication.shared.canOpenURL(settingsUrl) {
    UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
    })
    }
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(settingsAction)
    alertController.preferredAction = settingsAction
    DispatchQueue.main.async {
    self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
    }
    })
    }
    }

    }









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.



      I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
      index.js Code:



      var notification = {
      notification: {
      title: conversation.conversationName,
      body: user.username + ': ' + message.text,
      sound: 'default'
      },
      topic: topic
      }


      App Delegate Code: Function called in didFinishLaunchingWithOptions.



      import UIKit
      import Firebase
      import UserNotifications

      private func attemptRegisterForNotifications(application: UIApplication) {

      Messaging.messaging().delegate = self

      UNUserNotificationCenter.current().delegate = self

      let options: UNAuthorizationOptions = [.alert, .badge, .sound]

      UNUserNotificationCenter.current().getNotificationSettings { (settings) in

      if settings.authorizationStatus == .authorized {
      // Push notifications already authorized, do nothing
      print ("push notifications authorized")
      } else if settings.authorizationStatus == .notDetermined {
      // User hasn't specified notification status
      UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
      if let error = error {
      print ("Failed to request authorization:", error)
      return
      }

      guard granted else {return}
      DispatchQueue.main.async {
      application.registerForRemoteNotifications()
      }
      })
      } else if settings.authorizationStatus == .denied {
      // User has denied notifications
      UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in

      if let error = error {
      print ("Failed to request authorization:", error)
      return
      }

      let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
      let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
      guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
      return
      }
      if UIApplication.shared.canOpenURL(settingsUrl) {
      UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
      })
      }
      }
      let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
      alertController.addAction(cancelAction)
      alertController.addAction(settingsAction)
      alertController.preferredAction = settingsAction
      DispatchQueue.main.async {
      self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
      }
      })
      }
      }

      }









      share|improve this question















      I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.



      I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
      index.js Code:



      var notification = {
      notification: {
      title: conversation.conversationName,
      body: user.username + ': ' + message.text,
      sound: 'default'
      },
      topic: topic
      }


      App Delegate Code: Function called in didFinishLaunchingWithOptions.



      import UIKit
      import Firebase
      import UserNotifications

      private func attemptRegisterForNotifications(application: UIApplication) {

      Messaging.messaging().delegate = self

      UNUserNotificationCenter.current().delegate = self

      let options: UNAuthorizationOptions = [.alert, .badge, .sound]

      UNUserNotificationCenter.current().getNotificationSettings { (settings) in

      if settings.authorizationStatus == .authorized {
      // Push notifications already authorized, do nothing
      print ("push notifications authorized")
      } else if settings.authorizationStatus == .notDetermined {
      // User hasn't specified notification status
      UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
      if let error = error {
      print ("Failed to request authorization:", error)
      return
      }

      guard granted else {return}
      DispatchQueue.main.async {
      application.registerForRemoteNotifications()
      }
      })
      } else if settings.authorizationStatus == .denied {
      // User has denied notifications
      UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in

      if let error = error {
      print ("Failed to request authorization:", error)
      return
      }

      let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
      let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
      guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
      return
      }
      if UIApplication.shared.canOpenURL(settingsUrl) {
      UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
      })
      }
      }
      let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
      alertController.addAction(cancelAction)
      alertController.addAction(settingsAction)
      alertController.preferredAction = settingsAction
      DispatchQueue.main.async {
      self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
      }
      })
      }
      }

      }






      javascript ios firebase firebase-cloud-messaging






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 1:13









      Frank van Puffelen

      220k25361387




      220k25361387










      asked Nov 11 at 0:26









      Eric

      4617




      4617





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244743%2fios-enable-sound-for-push-notification-sound-fcm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244743%2fios-enable-sound-for-push-notification-sound-fcm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation