3
votes

My ios app has stopped playing sound for push notification since quite long. We have azure notification hub as our backend for sending notifications.

As per our discussion with MS Azure team, they informed that in order to enable the sound, we would need to include "Sound" property as per new APIs. But using azure notification hub this API would become bad request as "Sound" property is not supported by them. There is nothing else can be performed from azure notification side and they suggested to reach out to APNS to seek any alternatives, if any.

They are still working to add support for Critical Alerts on APNS.

Is there any work around? Has anyone faced such issue? Any help would be greatly appreciaated.

Below is the code for registering for push notifications:

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound], completionHandler: {(_ granted: Bool, _ error: Error?) -> Void in
        if error == nil {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    })

To play the default system sound, create your sound object using the default method.

By default, a notification contains an alert message that is displayed to the user without playing a sound. If you want to play a sound when a notification arrives, Apple provides one “default” sound that you can specify.

{"aps":{"sound":"default"}}

References: https://developer.apple.com/documentation/usernotifications/unnotificationsound

1
Do you want to play a specific sound, and don't know how to specify it? Or are you struggling with getting any sound at all?Alex AIT
@AlexAIT no sound at all.... It was working fine before...shaqir saiyed
How are you building the notifications in the backend? Can you show some code? Any alert with "sound": "default" should trigger a sound.Alex AIT
@AlexAIT right but the thing is to enable the sound, we need to include "Sound" property in the pay load but the API would become bad request as "Sound" property is not supported in azure and there is nothing else can be performed from azure notification side. They are asking to reach to APNS to seek any alternatives, if any. They also have this open ticket on their forum : feedback.azure.com/forums/218849-notification-hubs/suggestions/…shaqir saiyed

1 Answers

2
votes

There is an option to add sound to your remote notification locally, by this way no need to depend on server for adding sound property,

You can also use a notification service app extension to add a sound file to a notification shortly before delivery. In your extension, create a UNNotificationSound object and add it to your notification content in the same way that you’d for a local notification.

For this need to create a new target, you may need create cer for the notification extension's bundle ID same as main app.

enter image description here

Sample UNNotificationServiceExtension code for your reference,

import UserNotifications

class NotificationService: UNNotificationServiceExtension {
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    
    // MARK:- Notification lifecycle
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        // -------------
        // To play default sound
        let defaultSound  = UNNotificationSound.default
        bestAttemptContent?.sound = defaultSound
        // -----OR------
        // To play custom sound
        let customSound  = UNNotificationSound(named: UNNotificationSoundName(rawValue: "somelocalfile"))
        bestAttemptContent?.sound = customSound
        // --------------
        contentHandler(bestAttemptContent!)
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your “best attempt” at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
            // If notification doesnt process in time, Increment badge counter. Default notification will be shown
            let customSound  = UNNotificationSound(named: UNNotificationSoundName(rawValue: "somelocalfile"))
            bestAttemptContent.sound = customSound
            contentHandler(bestAttemptContent)
        }
    }
}

NOTE: According to this doc sound should be sent as default or any custom sound file path. There is no mentioning on behaviour of ignoring "Sound" in payload, considering this as Sound is mandatory!!.

EDIT: @shaqir From Apple documentation here,

Include this key when you want the system to play a sound.

If the sound file cannot be found, or if you specify default for the value, the system plays the default alert sound.

Means if no sound key in payload, no sound will be played. It should be default or wrong audio path.