Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Notifications: no sound

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

like image 881
ernestocattaneo Avatar asked Jan 07 '15 09:01

ernestocattaneo


People also ask

Why are my notifications making no sound?

Try these steps: Go to Settings > Sound & Notification > App Notifications. Select the app, and make sure that Notifications are turned on and set to Normal. Make sure that Do Not Disturb is turned off.

Can push notifications have sound?

Tap Notifications. Tap Sounds. Tap either Alarm Events, Silent Alarm Events, or Non-Alarm Events.

How do I unmute push notifications?

From a push notification: Pull down on the notification to expand it to see a "Mute" button, then tap it.


2 Answers

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

like image 159
Leo Dabus Avatar answered Sep 24 '22 01:09

Leo Dabus


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()
}
like image 24
Void2g Avatar answered Sep 23 '22 01:09

Void2g