Push Notifications: No Sound

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

My only problem is that when a notification arrives: it doesn't play sound!

I go to settings (on my tablet), with notifications I see this:

enter image description here

As you can see, the β€œsound” and β€œicon” are turned off. If I turn them on when the push notification arrives: it plays the sound!

So ... I would like these two options to default to TRUE when installing my application.

Is it possible? How can i do this?

I work in Swift, and so far this is my code:

didFinishLaunchingWithOptions method

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

Thank you for helping me.

+6
source share
4 answers
 UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil)) 

you need to set soundName also, because by default there is no sound:

For this property, specify the file name (including extension) of the sound resource in the main application bundle or UILocalNotificationDefaultSoundName to request the default sound system. When the system displays a warning for local notification or an application icon icon, it plays that sound. The default value is nil (no sound). Sounds lasting more than 30 seconds are not supported. If you specify a file with sound that plays for more than 30 seconds, the default sound is played instead.

 yourNotification.soundName = UILocalNotificationDefaultSoundName 

soundName

for remote notifications to be used Notification payload

+4
source

Similar to what you have for UIUserNotificationSettings, try it for RemoteNotification.

For example, in Objective-C:

  [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
0
source

I think this is a problem that you only encounter on this exact device. Try again and see if this is different.

0
source

If you want to play the standard iOS sound for your notification, you need to set content.sound to UNNotificationSound.default() You can do this in your schedule function, as 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() } 
0
source

Source: https://habr.com/ru/post/980636/


All Articles