简体   繁体   English

iOS 推送通知 自定义声音

[英]iOS Push notification Custom sound

I need to customize push notification sound.我需要自定义推送通知声音。 I don't want to create and include to Bundle.我不想创建并包含到 Bundle 中。 I need to implement like whatsapp notification sounds or please give a list of Apple provided sounds(like default sound).我需要实现类似 whatsapp 通知声音或请提供 Apple 提供的声音列表(如默认声音)。 Any help would be highly appreciated.任何帮助将不胜感激。

Thanks谢谢

After digging this issue for more than 3 weeks, and writing back and forth with apple this is the only solution change payload to something like在挖掘这个问题超过 3 周后,并与苹果来回写作,这是唯一的解决方案,将有效负载更改为类似

 sound = "custom"

now you need to show all your system sounds in a table view (if you don't know how to do it, look on github).现在您需要在表格视图中显示您的所有系统声音(如果您不知道如何操作,请查看 github)。 once the user mark his sound notification you need to copy the file to library/sound and give it the name of custom (or whatever the exact name in the payload).一旦用户标记了他的声音通知,您需要将文件复制到 library/sound 并为其指定自定义名称(或有效负载中的任何确切名称)。 that way you never change the server side code and you keep it on custom, and on the other hand you just overwrite the custom file with a new sound the user has been chosen.这样,您永远不会更改服务器端代码,而是将其保留为自定义,另一方面,您只需使用用户选择的新声音覆盖自定义文件。

*SIDE NOTE: on version 9.2.1 there's a bug which cause the notification not to work on the second time, or at all, it's should be fixed according to Apple in the next version 9.3 ! *旁注:在版本 9.2.1 上有一个错误,导致通知无法第二次工作,或者根本不工作,应该根据 Apple 在下一个版本 9.3 中修复它!

in the breath I wish the solution I could override the payload like we can do in Android, Apple makes push notification a lot harder on the developers.一口气我希望解决方案可以像我们在 Android 中那样覆盖有效负载,Apple 使推送通知对开发人员来说更加困难。

in you bundle add a sound file named "pushSound.caf".在你的包中添加一个名为“pushSound.caf”的声音文件。

//write your payload this way //以这种方式编写您的有效负载

{
 aps =
 {
    alert = "message";
    sound = "pushSound.caf";//this file will have to your bundle
   };
}

Preparing Custom Alert Sounds准备自定义警报声音

For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app.对于 iOS 中的远程通知,您可以指定 iOS 在为应用程序显示本地或远程通知时播放的自定义声音。 The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app's data container.声音文件可以位于客户端应用程序的主包中,也可以位于应用程序数据容器的 Library/Sounds 文件夹中。

Custom alert sounds are played by the iOS system-sound facility, so they must be in one of the following audio data formats:自定义警报声音由 iOS 系统声音工具播放,因此它们必须采用以下音频数据格式之一:

  • Linear PCM线性PCM
  • MA4 (IMA/ADPCM) MA4 (IMA/ADPCM)
  • µLaw
  • aLaw一条法律

You can package the audio data in an aiff, wav, or caf file.您可以将音频数据打包到 aiff、wav 或 caf 文件中。 Then, in Xcode, add the sound file to your project as a nonlocalized resource of the app bundle or to the Library/Sounds folder of your data container.然后,在 Xcode 中,将声音文件作为应用程序包的非本地化资源添加到您的项目或数据容器的 Library/Sounds 文件夹中。

You can use the afconvert tool to convert sounds.您可以使用 afconvert 工具来转换声音。 For example, to convert the 16-bit linear PCM system sound Submarine.aiff to IMA4 audio in a CAF file, use the following command in the Terminal app:例如,要将 16 位线性 PCM 系统声音 Submarine.aiff 转换为 CAF 文件中的 IMA4 音频,请在终端应用程序中使用以下命令:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v

You can inspect a sound to determine its data format by opening it in QuickTime Player and choosing Show Movie Inspector from the Movie menu.您可以通过在 QuickTime Player 中打开声音并从“电影”菜单中选择“显示电影检查器”来检查声音以确定其数据格式。

Custom sounds must be under 30 seconds when played.播放时自定义声音必须在 30 秒以下。 If a custom sound is over that limit, the default system sound is played instead.如果自定义声音超过该限制,则改为播放默认系统声音。

To use the default sound for a notification为通知使用默认声音

let content = UNMutableNotificationContent()

/// Set up content ...

content.sound = UNNotificationSound.default()

To use a custom sound, the sound file has to be stored in the app's main bundle OR download it and store it in the Library/Sounds subdirectory of the app's container directory.要使用自定义声音,声音文件必须存储在应用程序的主包中,或者下载它并将其存储在应用程序容器目录的Library/Sounds子目录中。

The "main bundle" approach can only be used with a new application release, the "downloading the sound file" approach is more flexible and makes shipping new sounds without a new version release. “主捆绑”方法只能用于新的应用程序版本,“下载声音文件”方法更灵活,无需新版本发布即可发布新声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM