【问题标题】:iOS Unable to play custom sound for remote notificationsiOS 无法为远程通知播放自定义声音
【发布时间】:2016-11-08 11:26:37
【问题描述】:

我一直在尝试为 iOS 上的通知实现自定义声音。

根据示例 3 here 中的 Apple 文档。我们需要做的就是确保推送通知负载应该是这样的:

{
"aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    }
}

而且 bingbong.aiff 必须放在应用程序包中。

此外,如果应用程序处于活动状态,建议我添加以下代码:

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateActive)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"bingbong" ofType:@"aiff"];
        theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];

        theAudio.delegate = self;
        [theAudio play];
    }
}

"bingbong.aiff" 放在"SupportingFiles"

我仍然没有收到远程通知的自定义声音。

我已经完成了与此类似的其他 SO 问题,但这些问题与我正在做的事情没有什么不同。

  1. How to play custom sound file when user get push notification?

  2. Custom sounds in remote notifications iOS 10, swift 3

  3. Change push notification sound

【问题讨论】:

  • 向你的脚本展示你是如何从服务器触发通知的。
  • 应用打开时调用didReceiveRemoteNotification函数并收到推送,打开应用时是否听到声音?
  • @iphonic 不,应用打开时我听不到任何声音。在后台播放默认声音。
  • @Ichthyocentaurs 尝试在didReceiveRemoteNotification 中设置断点,看看是否获得了正确的文件,可能是您的声音文件有问题?

标签: ios objective-c apple-push-notifications


【解决方案1】:

默认声音:

根据苹果文档和指南播放默认声音,您必须在声音文件中发送“默认”值。否则它也不会播放默认声音。

自定义声音

您需要在 php script** 中传递自定义声音文件名来代替 default

  1. 对于自定义声音文件必须存在于您的Xcode 项目中 资源。
  2. 确保声音文件的持续时间不超过 30 秒。除此以外 它将播放默认声音。
  3. 并且您必须添加以下密钥UILocalNotificationDefaultSoundName.plist 文件中。见下图:

【讨论】:

  • 这种情况下 .mp3 文件是否也可以使用,我尝试了上述方法但不起作用。
  • 这里支持我.waw 工作正常,或者你可以尝试一些苹果支持的扩展,可能.mp3 不支持,请仔细检查你的 php 脚本,这可能会导致问题。
  • 我在项目中放置文件的方式有问题吗?我刚刚添加了条目“UILocalNotificationDefaultSoundName”-字符串-“customsound.aiff”。我通过菜单选项“File”>“Add files to Project”将Xcode中的声音文件添加到文件夹Supporting Files
  • @Ichthyocentaurs 这里一切正常,但扩展名为.aiff 的声音文件看起来很可疑,只需使用.wav.caf(推荐)转换声音文件即可。
  • 让我试试咖啡馆
【解决方案2】:

删除didReceiveRemoteNotification 中的AVPlayer 内容。

确保:

  • 声音长度
  • 文件确实是其中一种受支持的格式(aiff、wav、caf)
  • 该文件确实存在于您的捆绑包中(复制捆绑包资源)
  • 您已注册推送通知:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().registerForRemoteNotifications()
    
        return true
    }
    
  • 名称与文件名完全匹配:

     {
        "aps" : {
            "alert" : "message",
            "badge" : 1,
            "sound" : "name.aiff"
        }
    }
    

【讨论】:

  • 我把AVPlayer的代码去掉了,已经在applicationDidFinishLaunching:有权限了,但是还是没有解决
  • 文件“目标会员”对我来说是缺失的部分,谢谢
【解决方案3】:

Swift 5 答案,您可以使用 AudioToolbox 做到这一点,非常适合短声音剪辑:

  1. 播放默认系统声音(这里是声音代码列表:http://iphonedevwiki.net/index.php/AudioServices

    导入音频工具箱

    AudioServicesPlaySystemSound(1103)



  1. 播放您之前拖入 Xcode 项目的声音文件:

    var soundID:SystemSoundID = 0

    let filePath = Bundle.main.path(forResource: "file_name", ofType: "wav") // 或者 mp3,这取决于你的声音文件的扩展名

    让 soundURL = URL(fileURLWithPath: filePath!)

    AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)

    AudioServicesPlaySystemSound(soundID)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多