preface

After iOS12, apple prohibited the function of directly playing voice and synthesizing voice in the extension of push service, resulting in the failure of the previous implementation of voice playing. In this record through a variety of experiments after the offline broadcast method. (All need to be completed through Notification Service Extension)

  • Analyze the attachments carried by the push content, including the link to download the background voice file, and store the downloaded voice file in a special location for playing.
  • All decomposed voice packages are built into the app, push data through the background, synthesize them by themselves and then play them through local notification.
  • Voip scheme (only recorded here, this time there is no self-test reason temporarily omitted).

Notification Service Extension

The new solution mainly utilizes the Notification Service Extension(NSE for short) introduced by apple in iOS10. When the payload of apns carries a value of 1, the NSE code is entered. The biggest difference from the Voip solution is that NSE cannot wake up the main application, nor can it access the file space of the main application, and can only handle the corresponding logic in the Extension process. In THE NSE, developers can change the content of the notification, use offline synthesis or download from the background to generate the content to be broadcast, and customize the notification ring tone to achieve the purpose of voice notification. The NSE solution is also the solution recommended by apple in WWDC2019 Session707.

UNNotificationSound

In NSE, you can assign the Sound property in UNNotificationContent to play a piece of custom audio when the notification pops up.

// The sound file to be played for the notification. The sound must be in the Library/Sounds folder of the app’s data container or the Library/Sounds folder of an app group data container. If the file is not found in a container, the system will look in the app’s bundle.

The document clearly describes the audio file storage path, and read priority: The customized ringtones in the main bundle of the Library/Sounds folder in the AppGroups shared directory of the main app support the following sound formats: AIFF, WAV, and CAF. The length of ringtones must be shorter than 30s. Otherwise, the system plays the default ringtone.

And because it is a notification ring, the sound is the default with mute switch, do not need to use the same as before to judge mute switch black magic (black magic in different models occasionally misjudgment).

AppGroups

Since we are customizing the ring tone in NSE, we cannot access the file paths 1 and 3. You can only store synthesized or downloaded audio files in your Library/Sounds folder under AppGroups, which requires Capablities, Way to through NSFileManager containerURLForSecurityApplicationGroupIdentifier: visit AppGroups root directory.

Voice Synthesis Wechat’s receiving-to-account voice relies on our own powerful offline voice synthesis library. The payload of APNS carries the text content to be synthesized. After the WAV audio file is generated through the offline voice synthesis Library, the file is written to the Library/Sounds folder of AppGroups. Finally, change the UNNotificationSound property to make the notification broadcast a custom receipt-to-account speech.

If some small enterprises themselves do not have the ability to offline synthesis (see several powerful offline synthesis services on the market are required to charge), you can use online synthesis and then download through HTTP, Ifly.com and wechat have provided free services. The disadvantage of this scheme is that it depends on the background and the current network environment, which may lead to the problem of untimely news broadcast. If within 30 s and cannot be successful, now need in serviceExtensionTimeWillExpire method for processing, out of the best scheme is playing a default voice.

First, online download and play

1. Implementation of Notification Service Extension (simple here, you can refer to it by yourself)

The configuration of project push is as follows

1. App Groups is a sandbox for storing downloaded voice files

2. External Communication also needs to be checked, otherwise it can’t debug the extension

2. Add the corresponding code to NotificationService

DidReceiveNotificationRequest: adding corresponding code, modify the corresponding data, push here and download the corresponding file, if they are audio files, stored in the Library/contributor to AppGroups Shared directory folder. Then according to the corresponding audio file name changes corresponding to the self. The bestAttemptContent. Sound = [UNNotificationSound soundNamed: @ “XXX. Wav”]. The system will automatically broadcast according to the name

2. Add offline voice package in app, perform local analysis and combination of push data, and then play through local notification

1. Implementation of Notification Service Extension in the same scheme

2. Add offline voice package in APP project first

3. Add corresponding code to NotificationService

1, according to their own background send push data parsing corresponding voice field didReceiveNotificationRequest: add and modify the corresponding push field code

isSound; // Prevent broadcast overlay

2. Parse it into the name of the corresponding voice fragment in your APP package and press it into the array for subsequent recursive playback

3. Send local voice notifications recursively based on the voice array

Method of automatically obtaining audio duration

3. Voip scheme, which has not been implemented this time.

Supplement:

If there is a local fixed voice, the voice package can be directly put into the APP project. Then, add the corresponding voice name in the sound field of the notification to play the voice directly. This feature also allows for simple custom notification sounds.

Summary:

Offline speech synthesis simple summary record, the follow-up details have time to perfect.