iOS 无法为远程通知播放自定义声音

ich*_*urs 2 objective-c apple-push-notifications ios

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

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

{
"aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    }
}
Run Code Online (Sandbox Code Playgroud)

并且 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];
    }
}
Run Code Online (Sandbox Code Playgroud)

"bingbong.aiff" 被放置在 "SupportingFiles"

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

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

  1. 用户收到推送通知时如何播放自定义声音文件?

  2. 远程通知中的自定义声音 iOS 10, swift 3

  3. 更改推送通知声音

sha*_*ght 5

删除里面的AVPlayer东西didReceiveRemoteNotification

确保:

  • 声音长度 < 25 秒
  • 该文件确实是支持的格式之一(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
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 该名称与文件名完全匹配:

     {
        "aps" : {
            "alert" : "message",
            "badge" : 1,
            "sound" : "name.aiff"
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)


vai*_*hav 5

对于默认声音:

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

对于自定义声音

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

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

在此处输入图片说明