The introduction

Two days ago, the product manager hoped to realize the voice reminder function of order notification on the B-end App, which is similar to the receipt notification of wechat and Alipay. Therefore, I carried out research and finally realized this function based on Notification Service Extension after reading the articles of various leaders. Here is a record.

What is Notification Service Extension

Notification Service Extension is a system Extension for iOS 10. It provides interception and preprocessing of remote push notification messages.

Implementation approach

Create a Notification Service Extension that intercepts and parses the contents of remote Notification messages when they arrive, and differentiates the sound of notifications based on the order type. Thus achieve the purpose of playing different voice.

1. Create the Extension AppID

ExtensionIs a plugin that depends on the main App. Its life cycle is managed by the system, not by the master App. First we need to log in to the Apple developer account asPush to expandCreate its ownApp ID.

When I created the ID, I checked the App Group TAB in Capabilities. The App Group is used to create a shared data space between the Extension and the main App. It is convenient to store the voice file generated online in the future to broadcast the order number and amount. (This is not required at the time of writing, so you won’t need to set it for now.)

2. Create a Profile

Create a vm by following the normal processExtensionDescription file of.

3. Add Notification Extendsion to the project

3.1 Main engineering configuration

Master of engineeringBackground ModeCheck as follows:

3.2 Creating an Extension

Go to File -> New -> Target and create oneNotification Service Extension.

3.3 Configuring Extensions

After the creation, import the description file.

4. Generate a voice file

After the above steps, our extension has been configured. Next, continue to generate the voice file. The prompt corresponding to the various states of the order in the current project is fixed. So directly use iFLYtek online speech synthesis, get different voice files.

For now, the voice files are in the Resource file directory of the main project:

5. The Extension coding

After Extendsion is configured and voice files are ready. We can go into the coding part. The interception of remote push is centralized in the notificationService.m file.

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; / / / message parsing push NSMutableDictionary * extras = [NSMutableDictionary dictionaryWithDictionary: self.bestAttemptContent.userInfo]; NSNumber *msgType = extras[@"msgType"]; / / /, depending on the type of order to get the corresponding audio file name nsstrings * soundName = [self soundNameByNotifyType: msgType. IntegerValue]; / / / differentiation set push sound self. BestAttemptContent. Sound = ISNOTEMPTY_STRING (soundName)? [UNNotificationSound soundNamed:soundName] : [UNNotificationSound defaultSound]; NSLog(@"(%@)%@", soundName, msgType); self.contentHandler(self.bestAttemptContent); } -(NSString *)soundNameByNotifyType:(MDBShopMallOrderType)type{ NSString *soundName = @""; Switch (type) {case MDBShopMallOrderTypeNew: soundName = @" new order.mp3 "; break; Case MDBShopMallOrderTypeWriteOff: soundName = @ "cancel after verification. Mp3"; break; Case MDBShopMallOrderTypeError: soundName = @ "abnormal distribution. Mp3"; break; Case MDBShopMallOrderTypeRefund: soundName = @ "refund. Mp3"; break; default: soundName = @""; break; } return soundName; }Copy the code

Thank you

IOS15 for local notification and voice broadcast exploration

IOS13 wechat receivables to account voice reminder development summary