scenario

Play custom generated sounds in the message push

The solution

  1. After generating a custom sound file, it must be written to [/Library/Sounds/] to play it
- (void)writeMusicDataWithUrl:(NSString*)filePath callback:(void(^)(BOOL success,NSString  * fileName))blockCallback{ NSString *bundlePath = filePath; NSString *libPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Sounds/"]; NSFileManager *manager = [NSFileManager defaultManager]; if (! [manager fileExistsAtPath:libPath]) { NSError *error; [manager createDirectoryAtPath:libPath withIntermediateDirectories:YES attributes:nil error:&error]; } NSData *data = [NSData dataWithContentsOfFile:bundlePath]; BOOL flag = [data writeToFile:[libPath stringByAppendingString:[filePath lastPathComponent]] atomically:YES]; If (flag) {NSLog(@" write file successfully "); if (blockCallback) { blockCallback(YES,[filePath lastPathComponent]); }}else{NSLog(@" file write failed "); if (blockCallback) { blockCallback(NO,nil); }}}Copy the code
  1. In [UNMutableNotificationContent] [sound] parameters into the file name
/ / /!!!!!!!!!! : push speech UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; // title content.sound = [UNNotificationSound soundNamed:fileName]; Content.body = @" voice broadcast "; // The local push must have content, i.e. the body cannot be empty. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 if (@available(iOS 15.0, *)) { content.interruptionLevel = UNNotificationInterruptionLevelTimeSensitive; // The phone will light up and play a sound; May show under don't disturb mode (focus mode) / / @ "{\" aps \ ": {\" interruption - level \ ": \" time - sensitive \ "}} "; // @"{\"aps\":{\"interruption-level\":\"active\"}}"; Content.body = @" voice broadcast "; // The local push must have content, i.e. the body cannot be empty. } #endif // repeats, if repeats must be longer than 60s, Is not an error UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 0.01 repeats:NO]; /* */ / NSString * identifier = [[NSUUID UUID] UUIDString]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) { completed(); }];Copy the code

Reference: www.jianshu.com/p/a6eba8cfb… Blog.csdn.net/LANGZI77585…