Push Notifications: no sound

11,142

Solution 1

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))

you have to set the soundName also because the default is no sound :

For this property, specify the filename (including extension) of a sound resource in the app’s main bundle or UILocalNotificationDefaultSoundName to request the default system sound. When the system displays an alert for a local notification or badges an app icon, it plays this sound. The default value is nil (no sound). Sounds that last longer than 30 seconds are not supported. If you specify a file with a sound that plays over 30 seconds, the default sound is played instead.

yourNotification.soundName = UILocalNotificationDefaultSoundName

soundName

for remote notifications you need to use The Notification Payload

Solution 2

If you wish to play the IOS standard sound for your Notification, you need to set your content.sound to UNNotificationSound.default() You can do this in your schedule function like I did here:

func schdule(date:Date,repeats:Bool)->Date?{
    let content = UNMutableNotificationContent()
    content.sound = UNNotificationSound.default()
    content.title = "Title"
    content.body = "blablablabla"
    content.setValue("YES", forKeyPath:"shouldAlwaysAlertWhileAppIsForeground")

    let trigger = UNCalendarNotificationTrigger(dateMatching: yourDateComponents,repeats: repeats)
    let request = UNNotificationRequest(identifier: "TestNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) { (error:Error?) in
        if error == nil {
            print("Notification Schduled",trigger.nextTriggerDate()?.formattedDate ?? "Date nil")
        } else {
            print("Error schduling a notification",error?.localizedDescription ?? "")
        }
    }
    return trigger.nextTriggerDate()
}
Share:
11,142
ernestocattaneo
Author by

ernestocattaneo

Updated on June 24, 2022

Comments

  • ernestocattaneo
    ernestocattaneo almost 2 years

    I've implemented push notification system in my application using Parse.com and everything works great!

    My only problem is that when the notification arrives: it does not play any sound!

    I go to settings (in my tablet), under notifications, I see this:

    enter image description here

    As you see the "sound" and the "badge" are OFF. If I turn them on then when a push notification arrives: it plays the sound!

    So... I would like that when I install my application these 2 options are by default TRUE.

    Is this possible? How can I do that?

    I am working in Swift and this is my code so far:

    method didFinishLaunchingWithOptions

     var pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge , categories: nil)
        application.registerUserNotificationSettings(pushSettings)
        application.registerForRemoteNotifications()
    

    Thanks a lot for helping me

  • ernestocattaneo
    ernestocattaneo over 9 years
    Thanks for the info! now it makes sense.. but Excuse me but where do I place this code ? (yourNotification.soundName = UILocalNotificationDefaultSoundName) thanksss
  • Leo Dabus
    Leo Dabus over 9 years
  • ernestocattaneo
    ernestocattaneo over 9 years
    In my viewcontroller? Strange.. and Im talking about remoteNotification.. not local.. Infact there is no UIRemoteNotification... what you say?
  • Leo Dabus
    Leo Dabus over 9 years
    If you edit your question and post your code I can try to help you.
  • ernestocattaneo
    ernestocattaneo over 9 years
    Which code are you talking about? This is everything I've done concerning push notifications...
  • ernestocattaneo
    ernestocattaneo over 9 years
    In Swift is: application.registerForRemoteNotifications() and it does what you mean but does not change the situation :)
  • JAB
    JAB about 9 years
    You answered how he needs to set the sound for a notification, but that isn't what he asked. The sound is being set fine, but when registering user settings the "Sound" option is not being turned on.
  • Leo Dabus
    Leo Dabus about 9 years
    @BJHStudios Thats all he needs to do. I have just tested it here and if the user authorizes it, all switches are turned on at once.
  • JAB
    JAB about 9 years
    He already has that code in place - he put it as part of the question. The problem is that when the user says OK when being presented with the register settings alert, only one of the settings is actually being registered. The alert one, in this case...sound and badge are not going through.
  • Leo Dabus
    Leo Dabus about 9 years
    Sorry like I said it doesn't happen here. It activates all of them.
  • JAB
    JAB about 9 years
    I have the same issue on my simulator, which is why I found this question. Same code as above, works on device, but only approves alerts on the sim (no sound or badge). Haven't found any solution yet.
  • Void2g
    Void2g almost 7 years
    You need to add this to your NotificationManager Swift file if you created one. Otherwise I cant't help you