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:
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
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.
Tap Notifications. Tap Sounds. Tap either Alarm Events, Silent Alarm Events, or Non-Alarm Events.
From a push notification: Pull down on the notification to expand it to see a "Mute" button, then tap it.
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
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With