6

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
  • 1,197
  • 5
  • 18
  • 30

4 Answers4

5
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

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thanks for the info! now it makes sense.. but Excuse me but where do I place this code ? (yourNotification.soundName = UILocalNotificationDefaultSoundName) thanksss – ernestocattaneo Jan 07 '15 at 09:56
  • http://stackoverflow.com/questions/27348209/user-set-time-for-notification-in-swift/27349421#27349421 – Leo Dabus Jan 07 '15 at 13:47
  • In my viewcontroller? Strange.. and Im talking about remoteNotification.. not local.. Infact there is no UIRemoteNotification... what you say? – ernestocattaneo Jan 07 '15 at 13:59
  • If you edit your question and post your code I can try to help you. – Leo Dabus Jan 07 '15 at 14:09
  • Which code are you talking about? This is everything I've done concerning push notifications... – ernestocattaneo Jan 07 '15 at 14:24
  • 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. – JAB Apr 05 '15 at 06:16
  • @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. – Leo Dabus Apr 05 '15 at 06:29
  • 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. – JAB Apr 05 '15 at 06:37
  • Sorry like I said it doesn't happen here. It activates all of them. – Leo Dabus Apr 05 '15 at 06:51
  • 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. – JAB Apr 05 '15 at 07:00
1

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()
}
Void2g
  • 35
  • 5
  • You need to add this to your NotificationManager Swift file if you created one. Otherwise I cant't help you – Void2g Jul 21 '17 at 07:29
0

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

For example, in Objective-C:

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
instaable
  • 3,449
  • 2
  • 24
  • 31
0

I guess that this is a problem you are facing only on that precise device. Try it on another one and see if it is different.

WiTHS4UC3
  • 260
  • 3
  • 11